John Davidson

php - How to automatically put the value in an input while hidden after I click submit?

0 comments
Message:


This is the input part:


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">                                   
<input type="hidden" name="ldbal" value="2000">
<input type="submit" name="btn-signup" class="btnreg" value="Register">
</form>

Now this is where I am going to submit the data and I want the value "2000" in the ldbal input to be inserted in the database while hidden.


<?php
if(isset($_POST['btn-signup']))
{
$Ldbal = $_POST['ldbal'];

$ins="INSERT INTO customers (Cus_Loadbal) VALUES ('$Ldbal')";
if($conn->query($ins)===TRUE)
{
<div class="alertinfo2">
<strong>Congrats!</strong>
</div>
}
else
{
?>
<div class="alertinfo2">
<strong>Sorry!</strong> Failed to insert. Please try again
</div>
<?php
}
}
?>

But when check the database, the value is always 0. Can you please help me?


PS: I removed parts of the code to reduce the length. I left what is essential.



My Answer:
It looks like the issue might be with how you are retrieving the value of ldbal from the form submission. Since ldbal is a hidden input field, it won't be included in the $_POST array when the form is submitted.

To fix this, you can set the value of ldbal directly in your PHP code before inserting it into the database. Here's an updated version of your code:

php
if(isset($_POST['btn-signup']))
{
$Ldbal = 2000; // Set the value of ldbal here

$ins = "INSERT INTO customers (Cus_Loadbal) VALUES ('$Ldbal')";
if($conn->query($ins) === TRUE)
{
echo '
Congrats!
';
}
else
{
echo '
Sorry! Failed to insert. Please try again
';
}
}
?>


By setting the value of ldbal directly in your PHP code, you ensure that it will always be inserted into the database as 2000.

Rate this post

3 of 5 based on 3251 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us