A ternary operator can take care of both assigned values and unassigned default values.
Example:
$checkbox = isset($_POST['checkbox']) ? $_POST['checkbox'] : "Default value if unchecked";
This is equivalent to doing: (which you can also use).
if (isset($_POST['checkbox'])){
$checkbox=$_POST['checkbox'];
// do something, as in run a function
}
else{
$checkbox="Default value if unchecked";
// do something else, as in run a different function
}
You can also remove the default text and do ""
in the ternary to leave it empty.
Reference:
Footnotes:
Make sure the checkbox holds the name attribute.
I.e. name="checkbox"
as an example.
and your form has a POST method. Use the appropriate method accordingly.
- So in your case and as seen in comments from code you left there, change
checkbox
in the arrays to deliver
.
Your checkbox value can also use the following, as a ternary example:
<input type="checkbox" name="deliver" value="<?php echo isset($_POST['deliver']) ? $_POST['deliver'] : "Default value if unchecked"; ?>" />
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…