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?

4
  • 1
    What is your PHP doing? It doesn't output anything. Commented Jul 27, 2012 at 7:21
  • you might want to look at this answer too: stackoverflow.com/questions/901712/… Commented Jul 27, 2012 at 7:23
  • i added the html code too my php inserts the values into the db Commented Jul 27, 2012 at 7:27
  • i am getting the checked value its working fine just the php post dosent work. am i doing something wrong in ajax because i checked gotofile putting dumy values it works fine Commented Jul 27, 2012 at 7:31

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

thanx will see abt CSRF filter
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.

1 Comment

check in firebug whether ajax request is being sent or not?
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 
                    }
                });
         });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.