使用jQuery / AJAX更新MYSQLjQuery、AJAX、MYSQL

2023-09-10 16:03:32 作者:再不下课我掀桌

我试图建立一个移动应用程序的PhoneGap和jQuery Mobile的。在我的应用程序我有一个页面,是一个链接到其更新MYSQL和头下页PHP文件。但随着PhoneGap的,我需要有外部的服务器上的所有PHP文件,所以我不能我对这个应用程序当前的解决方案。

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.

这是我用来更新MYSQL的PHP​​

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);
?> 

所以,我怎么能运行在用户点击一个按钮,这个PHP?使用jQuery / AJAX我想?

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

推荐答案

可以说,上面的PHP code在文件中,update.php。然后你可以用下面的code -

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>

只是通过有效身份证件在UpdateRecord功能。把你的PHP code。在update.php文件。只要是在一个更安全的一面,你的PHP code替换 $ VAR = @ $ _ GET ['身份证']; $ VAR = @ $ _ POST ['身份证']; 和检查,如果这对你的作品

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