Form field with decimal value shows as empty in $_POST - php

I have a form field that looks like this:
<input type="hidden" name="last_amount_paid" value="<?php echo $amount; ?>">
$amount is set to 100.00 and shows up fine if I change the form field from hidden to text.
$_POST['last_amount_paid '] is empty when I try and retrieve the value after submit.
I ran var_dump($_POST); and it's showing up as a string and empty
["last_amount_paid"]=> string(0) ""
Any ideas?

This should be working:
<?php
if(!empty($_POST["last_amount_paid"]))
{
echo $_POST["last_amount_paid"];
} else
{
$amount = 100.00;
?>
<form method="POST" action="">
<input type="hidden" name="last_amount_paid" value="<?=$amount; ?>" />
<input type="submit" value="Submit" />
</form>
<?php
}
?>

Related

PHP isset not accepting form name

I am having an issue trying to get my form with a hidden input field to be recognized when I check if it isset. Not sure if it is because I am running the form in a loop or what. I do know when I click submit for any of the fields from the loop the value of the hidden field is registering properly, it is just somehow the 'form1' is not registering as isset. Any ideas?
<?php
if(isset($_POST['form1']))
{
echo $_POST['cid'];
} else {echo "nope!";}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" name="form1">
// gets array
$listchars = $user->getCharacterList($_SESSION['user_id']);
// loops through array
foreach($listchars as $chardata){
echo $chardata['name']; ?>
<input type="hidden" name="cid" value="<?php echo $chardata['cid'];?>">
<input type="submit" name="submit" value="submit">
<?php } ?>
</form>
The form name is not submitted
Use
<input type="submit" name="form_name">
or
<input type="hidden" name="form_name">

values sent with POST/GET will not print as variables

I am working on a project to take two submitted variables and turn them into php variables (eventually they will multiply together) but as of now I cannot get the variables to be treated as such/echo.
I tried changing from POST to GET and the variables are sent through (appear in query line) but they don't print on the page
<?php
if (isset($_POST['submit'])) {
echo $_POST['length'];
echo $_POST['numPass'];
}
?>
<form method="post" action="">
<input type="number" name="length">
<input type="number" name="numPass">
<input type="submit">
</form>
I expect that the variables will be echoed as a regular statement.
ie. length=2 and numPass=4
24
You can get the $_GET and $_POST request using $_REQUEST
if (isset($_REQUEST['submit'])) {
echo $_REQUEST['length'];
echo $_REQUEST['numPass'];
}
<form method="post" action="">
<input type="number" name="length">
<input type="number" name="numPass">
<input type="submit" value="submit">
</form>
$POST['submit'] does not exist because your submit button does not have a name, it needs a name the same as your other inputs
It is better to check the input to avoid possible errors. You can try its example:
<?php
if( $_POST["length"] && $_POST["numPass"] ) {
echo "1: " . $_POST['length'] . "<br>";
echo "2: " . $_POST['numPass'] . "<br>";
echo $_POST['length'] * $_POST['numPass'];
}
?>
<form method="post" action = "<?php $_PHP_SELF ?>">
<input type="number" name="length">
<input type="number" name="numPass">
<input type="submit">
</form>

PHP: why variable does not get reloaded after a Submit?

Here is a code that I've made:
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if(isset($_POST['name'])){
$sum+=1;
}
echo "sum = $sum";
?>
When I enter some text in the form and click Validate, the page display sum=1, but after this, when I enter nothing in the form and click Validate, the page STILL displays sum=1.
Why does the variable $sum is not reloaded between the two Validate ? Is there a way to escape it ?
Thanks
This will solve the issue
<?php
$sum=0;
if(isset($_POST['name']) && $_POST['name'] != ''){
$sum+=1;
}
echo "sum = $sum";
?>
This is because isset() checks for the existence of the $_POST variable. In your case, the $_POST variable exists and has an empty string value.
Your code will work if you change isset() to !empty() like so;
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if(!empty($_POST['name'])){
$sum+=1;
}
echo "sum = $sum";
?>
More about the empty() function here.
Another way would be to check the request that your client has made on your page. So that if it is a simple refresh (not with a form refresh), it is a GET request and so, the variable should not be incremented and if the form has been sent, then you can do whatever you want such as incrementing the data.
So if the client is sending the form with an input text filled, then you can increment the value. In all other cases, the value should remain a zero.
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['name']) && !empty($_POST['name']))
{
$sum++; /* strictly equivalent to: $sum += 1; */
}
?>
<samp>sum = <?php echo $sum; ?></samp>
Try this
<form method="post" action="test.php">
<input type="text" name="name" placeholder="name"/><br />
<input type="submit" name="submit" value="Validate" />
</form>
<?php
$sum=0;
if(isset($_POST['submit'])){
$sum+=1;
}
echo "sum = $sum";
?>
You can try below:
if(isset($_POST['name']) && strlen($_POST['name'])>0){
$sum+=1;
You have the code appending 1 to variable $sum
but your if statement is based on the name field being passed.
Not if the name field has any data in it.
So... you have made your code add 1 as long as name field is passed,
regardless if it has text input or not.
Also, you should reassign the varible to reset it.
+= should just be =
<form method="post" action="test.php">
//----------------------------- add empty value to input ------------
<input type="text" name="name" value="" placeholder="name"/><br />
<input type="submit" value="Validate" />
</form>
<?php
$sum=0;
if(isset($_POST['name'])){
$sum=1;
}
echo "sum = $sum";
?>

Getting a value from text input field, then displaying on POST

I'm trying to get a value from a form, then display it on posting of the form. I can get the value to appear in the second text field, once I have chosen an option using the Ajax Auto-Select, but how do I get that value shown stored into a variable for display on posting? This is what I have been trying -
if ($_POST['action'] == 'getentity') {
$value= $entity;
$content .= '<div>'.$value.' hello</div>';
}
<form method="post" action="?">
<input type="text" name="TownID_display" size="50" onkeyup="javascript:ajax_showOptions(this,\'getEntitiesByLetters\',event)">
<input type="text" name="TownID" id="TownID_display_hidden" value="'.$entity.'" />
<input type="hidden" name="action" value="getentity" />
<input type="submit" name="submit" value="Find"/>
Many thanks for any help.
Try
<input type="text" name="TownID" id="TownID_display_hidden" value="<?php $value = $entity; echo $entity; ?>" />
and its better to use like this
if ($_POST['action'] == 'getentity') {
$value= $_POST['TownID'];
$content .= '<div>'.$value.' hello</div>';
}
it should work.

Simple form submit to PHP function failing

I'm trying to submit two form field values to a PHP function, on the same page.
The function works perfect manually filling the two values.
<?PHP // Works as expected
echo "<br />"."<br />"."Write text file value: ".sav_newval_of(e, 45);
?>
On the form I must be missing something, or I have a syntax error. The web page doesn't fail to display. I found the below example here: A 1 Year old Stackoverflow post
<?php
if( isset($_GET['submit']) ) {
$val1 = htmlentities($_GET['val1']);
$val2 = htmlentities($_GET['val2']);
$result = sav_newval_of($val1, $val2);
}
?>
<?php if( isset($result) ) echo $result; //print the result of the form ?>
<form action="" method="get">
Input position a-s:
<input type="text" name="val1" id="val1"></input>
<br></br>
Input quantity value:
<input type="text" name="val2" id="val2"></input>
<br></br>
<input type="submit" value="send"></input>
</form>
Could it be the placement of my code on the form?
Any suggestions appreciated.
You need to name your submit button, or check for something else on your if(isset($_GET['submit'])) portion:
<form action="" method="get">
Input position a-s:
<input type="text" name="val1" id="val1" />
<br></br>
Input quantity value:
<input type="text" name="val2" id="val2" />
<br></br>
<input type="submit" name="submit" value="send" />
</form>
OR keep same form, but change php to:
<?php
if( isset($_GET['val1']) || isset($_GET['val2'])) {
$val1 = htmlentities($_GET['val1']);
$val2 = htmlentities($_GET['val2']);
$result = sav_newval_of($val1, $val2);
}
?>
You can you hidden field as:
<input type='hidden' name='action' value='add' >
And check on php by using isset function that form has been submitted.

Categories