php - Can I assign values to a checkbox for checked and unchecked states? -


i have single checkbox in form want assign values both checked , unchecked states. values saved in mysql database. php retrieve values.

<?php // value = 10.00 // should checked value (initial state) function getdel1() { global $con; $get_del1 = "select * delivery"; $run_del1 = mysqli_query($con, $get_del1); while($row_del1=mysqli_fetch_array($run_del1)){ $leeds = $row_del1['delivery_leeds']; echo "$leeds"; } } ?>  <?php // value = 20.00 // should unchecked value (user interaction) function getdel2() { global $con; $get_del2 = "select * delivery"; $run_del2 = mysqli_query($con, $get_del2); while($row_del2=mysqli_fetch_array($run_del2)){ $leeds = $row_del2['delivery_other']; echo "$other"; } } ?> 

i can display values independently calling either function in div adjacent form checkbox don't know how assign values form checkbox , automatically update div depending on user interaction. suggestions?

a ternary operator can take care of both assigned values , unassigned default values.

example:

$checkbox = isset($_post['checkbox']) ? $_post['checkbox'] : "default value if unchecked"; 

this equivalent doing: (which can use).

if (isset($_post['checkbox'])){     $checkbox=$_post['checkbox'];  // something, in run function  } else{     $checkbox="default value if unchecked";  // else, in run different function  } 

you can remove default text , "" in ternary leave empty.

reference:

footnotes:

make sure checkbox holds name attribute.

i.e. name="checkbox" example.

and form has post method. use appropriate method accordingly.

  • so in case , seen in comments code left there, change checkbox in arrays deliver.

your checkbox value can use following, ternary example:

<input type="checkbox" name="deliver" value="<?php echo isset($_post['deliver']) ? $_post['deliver'] : "default value if unchecked"; ?>" /> 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -