-3

i can update mysql database by one checkbox value 0/1 by ajax, but i don't know how to do this with multiple checkbox,

my code: index.php

<?php
$query=mysql_connect("localhost","root","root");
mysql_select_db("gpio",$query);
?>

<!DOCTYPE html>
<html>
<head>
<title>Checkbox Switches DevGrow.com</title>
<script type="text/javascript"src="jquery.min.js">

</script>

    <script type="text/javascript">
        $(document).ready(function(){
            $('#switch1').click(function(){
                var myonoffswitch=$('#switch1').val();
                if ($("#switch1:checked").length == 0)
                {
                    var a="1";
                }
                else
                {
                    var a="0";
                }

                $.ajax({
                    type: "POST",
                    url: "ajax.php",
                    data: "value="+a ,
                    success: function(html){
                        $("#display").html(html).show();
                    }
                });
            });
        });
    </script>
</head>

<body>
    <input type="checkbox" name="switch1" id="switch1"
        <?php
            $query3=mysql_query("select pinDescription from pindescription where pinID=1");
            $query4=mysql_fetch_array($query3);
            if($query4['pinDescription']=="0")
            {
                echo "checked";
            }
        ?> >
</body>
</html>

ajax.php

<?php
$query=mysql_connect("localhost","root","root");
mysql_select_db("gpio",$query);
if(isset($_POST['value']))
{
$value=$_POST['value'];
mysql_query("update pindescription set pinDescription='$value' where pinID='1'");
}
?>

above code work only for one checkbox, what to do for 8 or 10 checkbox.

<input type="checkbox" name="switch1" id="switch1"
        <?php
            $query3=mysql_query("select pinDescription from pindescription where pinID=1");
            $query4=mysql_fetch_array($query3);
            if($query4['pinDescription']=="0")
            {
                echo "checked";
            }
        ?> >

<input type="checkbox" name="switch2" id="switch2"
        <?php
            $query3=mysql_query("select pinDescription from pindescription where pinID=2");
            $query4=mysql_fetch_array($query3);
            if($query4['pinDescription']=="0")
            {
                echo "checked";
            }
        ?> >

<input type="checkbox" name="switch3" id="switch3"
        <?php
            $query3=mysql_query("select pinDescription from pindescription where pinID=3");
            $query4=mysql_fetch_array($query3);
            if($query4['pinDescription']=="0")
            {
                echo "checked";
            }
        ?> >

where to make change in script for switch1 switch2 switch3.

nish
  • 3
  • 2
  • 1
    Your sql is vulnerable for mySql injections. Use PDO. – B001ᛦ Apr 26 '16 at 12:05
  • not to mention old syntax – madalinivascu Apr 26 '16 at 12:07
  • @bub , can you show me how to do with PDO? – nish Apr 26 '16 at 12:07
  • Have you tried anything at all? Where are you stuck? – David Apr 26 '16 at 12:08
  • 1
    [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Apr 26 '16 at 12:10
  • 1
    Please [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). [These extensions](http://php.net/manual/en/migration70.removed-exts-sapis.php) have been removed in PHP 7. Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php) and [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and consider using PDO, [it's really pretty easy](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard Apr 26 '16 at 12:10
  • In your last code sample... Why are you making the same exact SQL query multiple times? Also, since all of the `input` elements have the same `name` then only one value would be sent to the server. It's not clear what you're even *trying* to accomplish here. – David Apr 26 '16 at 12:23
  • i want to pass pinID and value 0/1 by checkbox to ajax.php – nish Apr 26 '16 at 12:30

1 Answers1

0

I have updated the answer:(I will suggest you to use mysqli_(), as mysql_() is depreciated. But I did not change anything regarding that.)

<!DOCTYPE html>
<html>
<head>
<title>Checkbox Switches DevGrow.com</title>
<script type="text/javascript"src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.js">

</script>

    <script type="text/javascript">

        $(document).ready(function(){

            $('input[name="switch"]').on("click", function() {
                if($(this).is(":checked")){

                    //alert("Checkbox is checked." + $(this).val() );

                    $.ajax({
                        type: "POST",
                        url: "ajax.php",
                        data: "value=1&id"+$(this).val() ,
                        success: function(html){
                            $("#display").html(html).show();
                        }
                    });

                }
/*below else block is not required but if user uncheck any checkbox & you want to update the description in db you can call ajax & update DB from this else part */
                else if($(this).is(":not(:checked)")){

                   // alert("Checkbox is unchecked."+ $(this).val());

                }
            });


        });
    </script>
</head>

<body>
<?php

    /* If you are confirmed that how many checkbox you need to show, you can do it from database.For the time being i have used a for loop to create checkbox dynamically.
 $query=mysql_query("select pinDescription,pinID from pindescription where pinID between 1 an 10");
    ;
    while($rs=mysql_fetch_array($query))
    {
        $ret[$rs["pinID"]] = $rs['pinDescription'];
    }
            ?> >
    */
    $ret = array(1=>0,2=>1,3=>1,4=>1,5=>0);
    for($cnt=1;$cnt<5;$cnt++)
    {
    ?>
        Switch<?php echo $cnt?><input type="checkbox" name="switch" id="switch<?php echo $cnt?>" <?php echo ($ret[$cnt] == 0) ? "checked" : "" ?> 
                                      value='<?php echo $cnt?>'>

    <?php
    }
    ?>
    </body>
    </html>

I have changed your index page only, your ajax.php should be the same.

Dipanwita Kundu
  • 1,637
  • 1
  • 9
  • 14