3

i am trying to create a form submit when checkbox is changed my code is given below. . my problem is nothing happens on

gotofile.php

file but //dosomething on the sucess function is executed

the jquery:

$("#container input[type=checkbox]").change(function(e){
                if($(this).attr('checked')) 
                {

                    var cnType=$(this).attr("id");

                    $.ajax({
                        type: "POST",
                        url: "gotofile.php",
                        data: "typID="+cnType ,
                        cache: false,
                        success: function(){ 
                            //do something 
                        }
                    });
                }
            });

the php:

include '../dbconnection/dbconfig.php';


$typeID=$_POST['typID'];
$qryConnections="INSERT INTO ...";
$rslt1 = mysql_query($qryConnections);

the html

<form id="cnct" method="POST">
                            <div id="container" style="">
                                <ul style="list-style: none;">
                                   <li><input type="checkbox" id="1" />A</li>
                <li><input type="checkbox" id="2" />B</li>

                                </ul>
                            </div></form>

Can any one help me what i am doing wrong?

rzShrestha
  • 128
  • 1
  • 6

3 Answers3

2

A couple of security issues

Always keep in mind that your JS is viewable to anyone that navigates to your site. Using:

data : "typID="+cnType

Would make me think that typID is the field in your SQL. You have no CSRF filter, therefore I could write an ajax script to spoof valid requests and update all of your fields from an external location. Something to keep in mind, I recommend you read up on CSRF or Cross Site Request Forgery.

Why doesnt your script work

If the success function is firing, then the script has run. Debug it by outputing the value of $_POST['typID'] in your PHP. You will see the variables value in the console if it sent correctly.

As well as this it's always good to have your PHP echo out a JSON response for your success function to validate that all went well.

echo json_encode(array('response' => 'success'));

or ('response' => 'failed') or whatever you need. You can then evaluate the JSON in your success function.

I hope this helps.

David Barker
  • 14,484
  • 3
  • 48
  • 77
0

The first thing is you should use click instead of change event for the checkbox in your Jquery code.

The second thing, you did not provide any value to the checkbox in your html code.

Kindly ask if it not worked for you.

Arun Jain
  • 5,476
  • 2
  • 31
  • 52
0

Try

        $("#container input[type='checkbox']").click(function(e){

                var cnType=$(this).attr("id");

                $.ajax({
                    type: "POST",
                    url: "gotofile.php",
                    data: "typID="+cnType ,
                    cache: false,
                    success: function(){ 
                        //do something 
                    }
                });
         });
asprin
  • 9,579
  • 12
  • 66
  • 119