0

I have the following code in my change.php page:

$con = mysql_connect("localhost","root","root");
 if (!$con)
   {
    die('Could not connect: ' . mysql_error());
    }

  mysql_select_db("FACT", $con);


  if($_POST['isChecked']==1)  // SEE THIS LINE PLEASE
  {
  mysql_query("UPDATE Orc SET CON = 'CHECKED'
  WHERE UID = '1'");
   }
  else
   {
   mysql_query("UPDATE Orc SET CON = 'NO'
   WHERE UID = '1'");
   } 

  mysql_close($con);

This page is called from my index.php which uses jQuery and:

  $.post("test.php", { isChecked:$('#checkbox_id').attr('checked') } );

It works almost fine for deselecting the checkbox I still need to manually refresh the page for the change take place in the DB and I can't change the box back to selected...

I'm stumped on this one, please help.

2
  • Are you sure that this .post is called when user clicks the checkbox? Commented Nov 17, 2009 at 10:46
  • It must be since the DB is updating from 'Checked' to 'NO' if the JS wasnt working I never called the change.php page. Still I dont know how to debug js. Commented Nov 17, 2009 at 11:00

2 Answers 2

1

In order to update the page without refreshing you will need to provide a callback function to the ajax call (code to run when the call has been successfully completed). From jQuery Docs:

 $.post("test.php", { name: "John", time: "2pm" },
  function(data){
    alert("Data Loaded: " + data);
  });

So you can use jQueries DOM manipulation to change parts of the page, get the php page to return new data to update the page with, or automatically refresh if you rely on data from the DB.

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

Comments

0

Adam Heath solution would work as well, but the way i would do it is the following:

  $.ajax({
    type : 'post',
    url : 'your url',
    data: {isChecked : $('#checkbox_id').attr('checked')},
    success: function(resultData) {
      //in your php script do all server side scripting and then
      //if your methods executed successfully return true otherwise return false
      if (resultData == true) {
         $('#checkbox_id').attr('checked', true);
      } else {
         $('#checkbox_id').attr('checked', false);
      }
    }
  });

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.