Home » Php » php – Update MYSQL with jQuery/AJAX

php – Update MYSQL with jQuery/AJAX

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

<?php

$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\" ");

header('Location: http://1.2.3.4/test');

mysql_close($con);
?> 

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

How to&Answers:

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

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

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

Answer:

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

Those codes suggested by Sachyn Kosare does work, the only thing is that the line data: 'id='+id has to be on top of url: "update.php"

And, you can use $var = $_POST['id']; for your PHP.

Answer:

say your button has an id clickme you bind an event handler on it as follows, I am usnig the on selector to avoid cases where the code is executed when the button does not exist on the page.. you can very well bind a click handler

$(document).on("click","#clickme",function(){
    $.ajax({

        type:"POST", //GET - update query should be POST
        url: my_endpoint.php,  //your php end point
        data: {jsonKey1:jsonValue1},
        success: function(data){ //if success
          //do necessary things with data
        }

    })
});

Is this what you are looking for?

Answer:

Do something in your success function

success: function(response)
   {
       window.location = response;
   }

and you html should contain

And in php do something like this

mysql_query('blah blah');
mysql_close($con);

echo "http://1.2.3.4/test";