user1323294 user1323294 - 2 years ago 187
Ajax Question

Update MYSQL with jQuery/AJAX

I'm trying to build a mobile application with PhoneGap and jQuery Mobile . In my app I have a page where is a link to PHP file which updates MYSQL and heads to next page. But with PhoneGap I need to have all the PHP files on external server so I can't my current solution on this application.

This is the PHP that I use to update MYSQL


$var = @$_GET['id'] ;

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

mysql_select_db("database", $con);

mysql_query("UPDATE table SET condition=true
WHERE ID= \"$var\" ");



So how can I run this PHP when user clicks a button? With jQuery/AJAX i suppose?

Answer Source

Lets say the above PHP code is in file, update.php. Then you can use the following code -

<script src="jquery.js"></script>
  function UpdateRecord(id)
       type: "POST",
       url: "update.php",
       data: 'id='+id,
       cache: false,
       success: function(response)
         alert("Record successfully updated");
<input type="button" id="button_id" value="Update" onClick="UpdateRecord(1);">

Just pass a valid id in the UpdateRecord function. Put your PHP code in update.php file. Just to be on a safer side, in your PHP code replace $var = @$_GET['id'] ; with $var = @$_POST['id'] ; and check if this works for you

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download