Selecting or deselecting checkboxes and running different queries in PHP

六月ゝ 毕业季﹏ 提交于 2019-12-08 05:52:34

问题


Edited:

So, I have multiple checkboxes. My goal is to insert checkbox value in MySQL if is checked, and to delete checkbox value from MySQL if is not checked. Everything works fine except deleting the value. PHP doesn't know which checkboxes are "unchecked". Any idea?

So far I have this:

if(isset($_POST['values']))
{
    foreach($_POST['values'] as $checked)
    {
        $query5 = ("
        INSERT INTO ecust_user_contract (fk_contract, fk_cust_user)
        VALUES ('".$checked."','".$username_u."')
        ");
        $result5 = mysqli_query($conn,$query5); 
    }
}
if(!isset($_POST['values']))
{
    foreach($_POST['values'] as $unchecked)
    {
        $query5 = ("
        INSERT INTO ecust_user_contract (fk_contract, fk_cust_user)
        VALUES ('".$unchecked."','".$username_u."')
        ");
        $result5 = mysqli_query($conn,$query5);
    }
}

回答1:


Assuming that you have used POST method for form submission, you can do something like this:

html:

<input type="checkbox" name="values[]" value="val1">
<input type="checkbox" name="values[]" value="val2">
<input type="checkbox" name="values[]" value="val3">

php:

if(isset($_POST['values'])) {
 foreach($_POST['values'] as $checked){
 mysqli_query("insert into tablename(value) values($checked)");
  }
 }else{
 // use Delete query
  }



回答2:


This is my checkbox:

<input type='checkbox' name='Event' value='CB1' />

Where you process the form, to read this you do:

$event = $_POST['Event'];

this gets the value of the checkbox:

echo $event;

But make sure its set or you will get a null value.

If you have several of them you can do this:

<input type='checkbox' name='Event[]' value='CB1' />
<input type='checkbox' name='Event[]' value='CB2' />

$event = array_values($_POST['Event']);

And to get the values for each one, put this in a for loop:

for ($val = 0; $val < count($event); $val++)
        {
            //do something
            echo $event[$val];
        }



回答3:


Try this code:

<input type='checkbox' name='Event[]' value='CB1' />
<input type='checkbox' name='Event[]' value='CB2' />

Use multiple checkboxes with array like this in your form.

after submit the form use the following to check whether checkbox(es) checked or not.

<?php
         if(isset($_REQUEST['Event'])) 
         {
                // do what you want for checked the boxes
         }
         else
         {
                // do what you want for not checked any box
         } 
?>

- Thanks




回答4:


I suppose you are submitting some html form to server side code in php. If you html page looks like:

<input type="checkbox" name="test" value="value1">

After submitting the form you can check it with:

isset($_POST['test'])
or
if ($_POST['test'] == 'value1')



回答5:


Suppose your HTML code for checkbox is like this:

<input type="checkbox" name="abc" value="yes" />

Then on php side:

<?php
    if( isset( $_POST['abc'] ) ) {
        --Connect to your database--
        --write your query for Insertion--
        }
        else {
        --Connect to your database--
        --write your query for Deletion--
        }
    ?>



回答6:


Work for me, save both check and uncheck value to mysql

HTML:

<form method="post" action="jawab25.php">
<input type="hidden" name="check_lista[]" value="<?php echo $r["id"]?>">
<input type="checkbox" name="check_listb[]" value="<?php echo $r["id"]?>">
<input value="Check" type="Submit">

jawab25.php :

    foreach($_POST['check_lista'] as $itema){
        $string="update trans set ck='N' where id='$itema'";
        $tampil=mysql_query($string);
    }

    foreach($_POST['check_listb'] as $itemb){
        $string="update trans set ck='Y' where id='$itemb'";
        $tampil=mysql_query($string);
    }


来源:https://stackoverflow.com/questions/19540781/selecting-or-deselecting-checkboxes-and-running-different-queries-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!