How to put an if statement inside an input's 'min' attribute - php

I want to make an if condition inside the min="" attribute of an input element.
Here is my code:
<input type="number" id="qty" name="qty" min="1" max="{{ $product_info[0]->quantity - $sessionQty }}" step="1" value="1" readonly>
What I want is to be able to write a condition like:
min = "if($product_info[0]->quantity - $sessionQty == 0) {
echo 0;
} else {
echo 1;
}"
When my $product_info[0]->quantity - $sessionQty would be equal to zero, the minimum value should be set to 0. when the condition is not zero, the minimum value should be set to 1.
Is this possible?

I am assuming it is php code that you are putting inside min in that case this is the working code as below
min="<?php echo ($product_info[0]->quantity - $sessionQty) == 0 ? '0' : '1'; ?>"
if {{}} is from some templating framework then it should also work like below
min="{{ ($product_info[0]->quantity - $sessionQty) == 0 ? '0' : '1' }}"

Related

My HTML form won't post the default values to my sql query unless I manually change at least one item on the form (including an option select)? [duplicate]

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()

PHP: How can I set a default value for $_POST array?

I'm building an e-commerce webpage and working on the "add to cart" button on the checkout page. Here, when the first page is loaded, I want to show a default value "1". And after that, the customer can adjust the quantity. I'm using the POST method to save the quantity input, but I'm not sure how to set the default value "1". Below is my code:
<section>
<?php
if (!empty($_POST)) {
$error = false;
if (empty($_POST["quantityInput"])) {
echo "<p class='errorMessage'> Quantity must be entered. </p>";
$error = true;
}
}
?>
<td class="quantityColumn">
<form action="cart.php" method="post">
<input type="number" id="quantityInput" name="quantityInput" min="1" max="10"
<?php
if (isset($_POST["quantityInput"])) echo "value='" . $_POST["quantityInput"] . "'";
?>>
<input type="submit" id="updateButton" value="Update">
</form>
</td>
</section>
I tried setting $_POST['quantityInput'] = "1", but when I enter a new value and submit the form, it only shows "1", and not the new value... Is there a way to set a default value using PHP? What method/function should I look into?
You can use null coalescing operator if quantityInput is not defined
Eg.
$_POST["quantityInput"] = $_POST["quantityInput"] ?? 1;
// Or on the short form
$_POST["quantityInput"] ??= 1;
It will set $_POST["quantityInput"] to 1 if not exist, by mean default value
The input field can be written like this:
<input
type="number"
id="quantityInput"
name="quantityInput"
min="1"
max="10"
value="<?= isset($_POST["quantityInput"]) ? 1 : $_POST["quantityInput"] ?>"
>

$_POST not getting number zero

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.

Make form allow minimum number 10 or higher to be inputted

I have a form on my site, and I want to make the minimum number that's allowed to be inputted be 10 or higher.
Here's the code for that form:
<input type="number" id="amount" onchange="updateInput(this.value)" class="form-control" value="10" placeholder="1.00" autocomplete="off" required>
Can anyone help me get this done?
Thank you
Change value to min
<input type="number" id="amount" onchange="updateInput(this.value)" class="form-control" min="10" placeholder="1.00" autocomplete="off" required>
this question has the PHP tag. in first place you need to consider Jaime's answer for client side (and please add a name attribute for any form input. in this case we assume you set the name to "amount"), and at back-end PHP do this :
<?php
$error = '';
$amount = ''; // default value;
if (isset($_REQUEST['amount'])) {
$amount = intval($_REQUEST['amount']); // get the value as number
if ($amount < 10) $error = 'the amount is under 10 and not acceptable';
}
echo $error;
?>

Trying to set default checkbox value if not checked

Basically I can get the value that applies to when the box is ticked (= 1), but I can't get it to send the default value of 0 when not checked.
<input type="checkbox" name="post_friend" value="1">
I've searched around and someone suggested setting a hidden checkbox, but it's not working for me.
<input type="hidden" name="post_friend" value="0">
Could you not do something like this?
$checkbox = isset($_POST['post_friend']) ? $_POST['post_friend'] : 0 ;
So if the checkbox is checked, variable is 1. If not, variable assigned value of 0
The original method which was given by the OP should work, however, the value may very well be different.
For example,
<input type="checkbox" name="post_friend" value="1">
<input type="hidden" name="post_friend" value="0">
In this case ["post_friend"] = 0.
However, in this example:
<input type="checkbox" checked="checked" name="post_friend" value="1">
<input type="hidden" name="post_friend" value="0">
Most browsers will send ["post_friend"] = "1,0".
Multiple values to the same property will usually be concatenated in the http request.
For this you can use String.Contains to find the 1.
However, here, you should really assess whether your input should be a checkbox, or more suitably, radio input.
Try something like below
<div class="col-xs-5 drinkingLeft">
<input type="checkbox" name="beer" id="beer"class="require-one col-xs-1" value="0"/>
<input id='beerHidden' type='hidden' value='0' name='beer'>
<label for="beer" class="col-xs-10">Beer </label>
</div>
$('#beer').on('change', function () {
this.value = this.checked ? 1 : 0;
}).change();
$("#submit").click(function () {
if(document.getElementById("beer").checked){
document.getElementById('beerHidden').disabled = true;
}
});
HTML
<input type="checkbox" id="chkbox1">
Jquery
if( $("#chkbox1").is(':checked')){
value = "True";
}else{
value= "False";
}
the only possible solution is :
<?php
// if data is posted, set value to 1, else to 0
$check_0 = isset($_POST['check'][0]) ? 1 : 0;
$check_1 = isset($_POST['check'][1]) ? 1 : 0;
?>
If you want to check info on the same page then use JS and check NULL values...

Categories