0

所以我试图在 AJAX 和 PHP 之间进行最简单的通信,并将一个变量发送到我的 PHP,它将被存储在那里,但似乎没有任何工作 u_u

这是我的 AJAX 代码

$(document).ready(function() {

 $("#ch1").click(function(){

  document.getElementById('stf').style.visibility="visible";
  document.getElementById('ch2').style.visibility="visible";
  document.getElementById('reto1').src="slides/reto1_completo.png";
  var counter = 1;
  var longform = $("input:text").serialize();
  $.ajax({
   type: GET,
   url: 'counter.php',
   data:   longform + "counter=<?php echo $counter; ?>",
  })
 })
})

这是我的PHP

session_start();

$db = mysql_connect("localhost","db","pass");
if(!$db) die("Error");
mysql_select_db("db",$db);

$counter = $_GET['counter'];

$insert = mysql_query("UPDATE usuarios SET retos='$counter' WHERE email = '$correo'") or die(mysql_error());

就是这样,我敢肯定这是最简单的错误,但我就是想不通。提前致谢

4

3 回答 3

0
  • 而不是 GET 你必须写“GET”,带引号。
  • 此外,你<?php echo $counter; ?>没有写任何东西,因为计数器没有在那个php代码中定义,所以你必须用另一种方法替换它来查找计数(或者SET retos=retos+1在mysql中使用,你想要的)。

选择代码的某些部分并更正它我实现了这一点,我建议你看一下,因为它运行良好(然后你添加其他部分):

<html>

<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $("#ch1").click(function(){
        alert("hey!");
        $.ajax({
            type: "GET",
            url: './counter.php',
            data: "counter=<?php echo '...'; ?>",
            success: function(data, text_status, jqXHR){
                alert(data);
            },
            error: function(XML_http_request, text_status, error_thrown){
                alert(error_thrown);
            }
        })
    });
})
</script>
</head>

<body>
<div id="ch1">hello world!</div>
</body>

</html>

在另一个 php 文件中:

<?php
# connect to the database...
# OK...

$counter = $_GET['counter'];

# change database's info...
# OK...

echo $counter;  #for debug purposes, to verify that in this code the php is receiving the string passed by the html.
?>
于 2013-07-17T17:07:29.673 回答
0

那这个呢?

$.get("url.php?"+longform+"&counter=1");

编辑:或

longform.counter = 1;
$.get("url.php",longform);
于 2013-07-17T16:13:18.433 回答
0

您正在构建错误的数据字符串。longform看起来像:

foo=bar&baz=qux

然后你直接附加 coutner 值:

foo=bar&baz=quxcounter=$counter

你需要一个&分隔符:

   data:   longform + "&counter=<?php echo $counter; ?>",
                       ^---here

所以counter将是它自己的键,而不是.baz末尾的值的一部分longform

于 2013-07-17T16:08:37.860 回答