I have the following code inside a form whereby when press submit button, php side is grabbing the input value using $_POST['revisionCad'].
<form method="POST">
<input type="number" class="form-control" placeholder="Revision" name="revisionCad" id="revisionCad" min="0">
<button name="submit" class="btn btn-primary" id="submit" tabindex="5" value="Save" type="submit" style="width:200px;">Create/Change Revision</button>
</form>
My Issue
Everything working fine except when I gave number 0 as input, $_POST is getting no value. My error check telling, please enter a number.
Below is my error check code
if(isset($_POST['revisionCad'])){
$revisionCad = $_POST['revisionCad'];
}
if(empty($revisionCad)){
erDisplay('Please select a revision for Cadgui');
}
But any number greater than 0, $_POST['revisionCad'] is grabbing correctly. I tried to remove the min attribute as well as change to -1. But still whenever I enter number 0, $_POST['revisionCad'] not grabbing any value. Anyone knows why?
To explain why this is happening, you have to take a closer look at the PHP empty() function. From the documentation we see that (emphasis mine)...
Determine whether a variable is considered to be empty. A variable is considered empty if it does not exist or if its value equals FALSE.
In PHP, the value of zero is a falsy value. So you get the following output,
var_dump(false == 0); // Outputs "true"
var_dump(empty(0)); // Outputs "true"
So you need to alter your code to check if the variable is set and greater or equal to zero, which you can do by altering the snippet you showed to the following.
if (isset($_POST['revisionCad']) && $_POST['revisionCad'] >= 0) {
$revisionCad = $_POST['revisionCad'];
} else {
erDisplay('Please select a revision for Cadgui');
}
http://php.net/empty
Add a default value to your form or change it in your code dynamically as follow:
<form method= "POST">
<!-- <input type="number" class="form-control" placeholder="Revision" name="revisionCad" id="revisionCad" min="0"> -->
/** Assign default value or set it automatically in your code.**/
<input type="number" class="form-control" placeholder="Revision" name="revisionCad" id="revisionCad" min="0" value=0>
<button name="submit" class="btn btn-primary" id="submit" tabindex="5" value="Save" type="submit" style="width:200px;">Create/Change Revision</button>
</form>
Your PHP codes:
if(isset($_POST['revisionCad'])){
$revisionCad = $_POST['revisionCad'];
}
if(empty($revisionCad)){
erDisplay('Please select a revision for Cadgui');
}
Should be changed to :
// You can set a default value of 0 in the code or leave it at null;
$revisionCad = (isset($_POST['revisionCad']) && !empty( $_POST['revisionCad'] ) || $_POST['revisionCad'] >=0 ) ?
trim($_POST['revisionCad']) : null;
/* Check if data is set to default null by your script,
* Display error, since no data was received
*/
if(is_null($revisionCad))
{
erDisplay('Please select a revision for Cadgui');
}
// if no error, continue with the rest of the codes ...
I hope this helps.
You need to change the condition as below:
if(isset($revisionCad) && $revisionCad >=0){
erDisplay('Please select a revision for Cadgui');
}
Actually empty() function treats 0 as empty value so it gives this error.
you have to give 0 in value property as default value.
<input type="number" class="form-control" placeholder="Revision" name="revisionCad" id="revisionCad" min="0" value="0">
Try (!isset($revisonCad)) . I hope it works.
Since you have given empty() function, it treat 0 as empty value. Hence, no input is grabbed.
Related
This question already has answers here:
mySQL UPDATE query returns "0 rows affected"
(11 answers)
Closed 9 days ago.
I have a simple html 5-field (plus 1 dropdown of option words), form where some dollar amounts are entered. A submit button has an action of a php page that prepares the stmnt to execute, performs a mysql UPDATE, sends me an SMS, etc.
There are default values, zeroes actually, that fill the form to start it out. They're just standard $variables.
The html input fields all have "value" that is an echoed php variable.
If you do not change at least ONE value (not all 5), there are no affected rows and the UPDATE query fails.
THIS INCLUDES SIMPLY CHANGING THE DROPDOWN ITEM VALUE.
Not only must a value be changed, but it cannot be cleared and re-entered as the default value. It must be different.
The form has a test function that strips characters and such before posting.
The post uses ternary operators to use a 0 if left empty, otherwise use what was entered.
I have tried it removing "required" in the html form.
I have tried removing the ternary operator (since a required input field can't be empty anyway).
I have tried leaving a field empty.
I don't get anything posted to the error log, as it just as affected rows = 0.
The form fields look like this:
<form action="proc.php" method="post" >
<fieldset>
<h2>input form</h2>
<b>November: $</b>
<input type="number" step="0.01" name="november" size="7" value="<?php echo $nov; ?>" required>
<b>December: $</b>
<input type="number" step="0.01" name="december" size="7" value="<?php echo $dec; ?>" required>
<b>January: $</b>
<input type="number" step="0.01" name="january" size="7" value="<?php echo $jan; ?>" required>
<b >February: $</b>
<input type="number" step="0.01" name="february" size="7" value="<?php echo $feb; ?>" required>
<b>March: $</b>
<input type="number" step="0.01" name="march" size="7" value="<?php echo $mar; ?>" required>
<input id="makechanges" class="makechanges" type="submit" value="submit" size ="50" name="submit">
</form>
This is the action php page that prepares and makes the query.
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$novpil = test_input($_POST['november']);
$decpil = test_input($_POST['december']);
$janpil = test_input($_POST['january']);
$febpil = test_input($_POST['february']);
$marpil = test_input($_POST['march']);
}
$stmt = $connmember->prepare(" UPDATE smfqg_themes SET
`value` = CASE `variable`
WHEN 'cust_novpil' THEN ?
WHEN 'cust_decpil' THEN ?
WHEN 'cust_janpil' THEN ?
WHEN 'cust_febpil' THEN ?
WHEN 'cust_marpil' THEN ?
ELSE `value`
END
WHERE id_member = ? ");
$stmt->bind_param('sssssi', $novpil, $decpil, $janpil, $febpil, $marpil, $who);
$stmt->execute();
if ($stmt->affected_rows > 0) {
//echo 'GOOD' ;
// $greenbar = true;
sendsms($first, $last, $who);
header('Refresh: 3; URL=https://example.com/success.php');
} else {
echo 'something went wrong, standby:';
header('Refresh: 3; URL= https://example.com/return.php');
}
$stmt->close();
$connmember->close();
}
I have echoed the _post values on the action page to see what was brought over, and whether I leave the default values or use my own, they echo fine before the query runs.
I tried ternaries that tested for that default zero to be both a number or string. I also did a cast (to int and float) for the default values before they got put in the html form, since the default values come from a table as a string (even though just a 0). No luck.
It would seem that you must either change one field value OR simply change the dropdown to make the form work. Leaving all untouched won't work.
Empty(0) Evals as true, so no change is detected. You will have to either add another condition or use something else than empty()
This code is supposed to save a random number into a session varible when generateCode is pressed
It does generate it and store in the variable but when i try to acces is from the if(isset($_POST['confirmCode'])){ it is empty
<?php
session_start();
if(isset($_POST['generateCode'])){
$_SESSION["key"] = mt_rand(100000, 999999);
}
if(isset($_POST['confirmCode'])){
die($_SESSION["key"]);
}
?>
<form method="POST">
<input type="text" name="confcode" placeholder="Confirmation Code">
<input type="submit" name="confirmCode" value="Confirm">
<input type="submit" name="generateCode" value="Generate">
</div>
</div>
</form>
Does anybody know how i can make a variable that stays some time even after post request
Edit:
So to get this clear confirmCode is a button to submit the code entered in a <input type="text></input> But i decided to not include the code to get the content if confCode because this question is about how to keep a variable generated by mt_rand even after <input type="submit"> is pressed
The problem isn't the button, PHP actually puts the name and value in the parameters.
The problem is with the die function. It expects either a message (string) or a status code (integer). When you generate the random number it interprets the message as a number (status code) instead of a string (message). To make sure it interprets the key as a string you could wrap it in a strval() function like this:
die(strval($_SESSION["key"]));
I have the following html-php input field on a form page:
<input type="text" id="subnet" name="subnet" class="inputText70px" maxlength="3" value=
<?php
if (!empty($eqptCheck['subnet'])){
echo '"'.$ipAddressArr['subnet'].'">.';
}else{
echo '"">.';
}
?>
Whenever I have any value other than 0 for the subnet it is displays properly in the page's input field. But when the subnet value is 0 the page's field is just empty. I assume my php must be the problem. Can anyone see what the issue with my php is that causes the issue?
I think empty is true if for value == 0. So when your subnet value is 0, then the !empty condition is false and moves along to else that renders an empty "" value for the intput field.
If you instead use !is_null in place of !empty I think that should resolve your issue.
<input type="text" id="subnet" name="subnet" class="inputText70px" maxlength="3" value=
<?php
if (!is_null($eqptCheck['subnet'])){
echo '"'.$ipAddressArr['subnet'].'">.';
}else{
echo '"">.';
}
?>
I want to have a form in following way:
[-] [value] [+]
I don't know how to script the logic to get the value 1 higher or 1 lower in PHP.
I assumed something like following would work:
<?php
$i = 0;
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($_POST['plus']))
{
$i++;
}
if(isset($_POST['min']))
{
$i--;
}
}
?>
and my form is as following:
<form action="" method="post">
<input type="submit" name="min" value="-"> <input type="text" value=<?php echo $i ?> > <input type="submit" name="plus" value="+">
</form>
But I'm only getting either a 1 or a -1. Can someone show me how to do this in a good way?
Try this:
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
$i = $_POST['current_value'] || 1;
if(isset($_POST['plus']))
{
$i++;
}
if(isset($_POST['min']))
{
$i--;
}
}
?>
and this:
<form action="" method="post">
<input type="submit" name="min" value="-"> <input name="current_value" type="text" value=<?php echo $i ?> > <input type="submit" name="plus" value="+">
</form>
You need some way to get the current value to persist between requests - depending on your use case, you may need database storage as Rocket mentions, or this simple passing back of the current value may be enough.
PHP will simply load code on the server-side and run when the page is loaded. If you are looking to dynamically increment/decrement the value while remaining on the same page, I suggest you look into learning Javascript and jQuery.
Reference: https://developer.mozilla.org/en/JavaScript/
Reference: http://jQuery.com/
What is happening with your code is that you are incrementing a global variable that you set to 0 in the beginning. That is why the only values you get back or 1 or -1. This can be fixed by passing the variable you increment each time instead of using a global variable. That way it keeps the value between each plus and minus and doesn't keep resetting it.
I can't set a variable from a post array.
I have a simple form with a hidden field in it:
<input name="sid" type="hidden" id="sid" value="<?=$sid?>">
This hidden field gets sent off to a second file (exec.php) where I have the following code:
$sid = $_POST['sid'];
For some reason, when trying to set $sid, it gets a NULL value. For haha's, I ran the following:
foreach($_POST as $var => $value)
{
echo $var . ' : ' . $value . "<br>";
}
This provided a correct value of 1938 for sid. I've looked at this for 3 hours and can't find what is happening. I expect something extremely stupid...any thoughts?
Here is the form on enter.php
<form name="form1" method="post" action="exec.php">
<input name="sid" type="hidden" id="sid" value="<? echo($sid); ?>">
<input name="ticket_totals" type="hidden" id="ticket_totals" value="<?=$ticket_totals?>">
<input name="emp" type="hidden" id="emp" value="<?=$emp?>">
<input name="submit" type="submit" id="submit" value="Submit">
<input type="submit" name="submit" id="submit" value="Close">
</form>
Here is the POST output on exec.php:
type : Other
ticket_totals : 0
emp : 105
sid : 1939
submit : Submit
Okay - this was poor syntax on my part but now I'm curious as to why.
I left out quotation marks - the solution is as simple as this:
$sid = $_POST["sid"]
Now it works like a champ.
Any takers on why? I'd guess there is a setting in the php.ini that requires the quotes. Strangely enough, I have other variables called from the POST array that i'm not using quotes for and they're working fine...
Use Console in FireBug to inspect the POST request to see what is the sid value that is being sent.
If the sid value in request is ok, use var_dump($_POST["sid"]); to see the results on the server.
EDIT: it's considered good PHP style to use the quotes when accessing the associative array because quote-less keys are indistinguishable from constants:
define('myVar',3);
echo $array[myVar]; // retrieves $array[3], not $array['myVar'];
Try to echo the $sid instead of the <?=:
// Change that
<input name="sid" type="hidden" id="sid" value="<?=$sid?>">
// With that
<input name="sid" type="hidden" id="sid" value="<?php echo $sid; ?>">
also for the test time try to change the input type from hidden to text in order to be 100% sure the $sid contains a value.
Using quotes for associative array keys is mandatory, and while it may work without them, it's incorrect and erratic behavior is expected.
I had this same problem, trying to use $_POST[sid] as a variable. I'm am thinking that "sid" is a reserved or restricted variable name, because I changed my variable to $_POST[snid] and it worked just fine. This was my code
$sid = $_POST[sid];
$recipient = "($sid) ($_POST[sid])";
if ($_POST[sid] > 0)
{
$recipient = "It Worked";
}
print $recipient;
When I posted "&sid=15", the result was:
() (15)
Unbelievable. Impossible, right? All I did was change from using "sid" as the index to "snid", and it worked no problem.
So, don't ever use $_POST[sid].