doudi2229 2013-06-20 17:19
浏览 57
已采纳

在PHP中捕获Ajax数据变量?

I'm trying to get the data used below to be caught in my alives.php page. Essentially, alives.php requires a variable $passcode.

How do I pass the content of data below as the variable $passcode through a POST request?

<script>
  $(document).ready(function() {

    $('#alive').click(function () {
      var data = '<?php  $row['code']; ?>';

      $.ajax({
        type:"GET",
        cache:false,
        url:"alives.php",
        data:data,    // multiple data sent using ajax
        success: function (html) {
        }
      });
      return false;
    });
  });
</script>

alives.php

<?php 
require("database.php");

$checkvote = "SELECT code FROM votes WHERE code = '$passcode'";
$updatealive = "UPDATE votes SET alive = +1 WHERE code = '$passcode'";
$addvote = "INSERT INTO votes (code, alive) VALUES ('$passcode',+1 )";

$checkvoterlt = mysqli_query($con, $checkvote); 

if(mysqli_num_rows($checkvoterlt) > 0) {
   $result = mysqli_query($con, $updatealive) or die(mysqli_error());
} else {
     $result = mysqli_query($con, $addvote) or die(mysqli_error());
}


mysqli_close($con);
?>
  • 写回答

2条回答 默认 最新

  • doudeng8691 2013-06-20 17:23
    关注

    So much is wrong.

    Problem 1: You are specifying a GET request: $.ajax({ type:"GET",. If you want it to be POST:

    $.ajax({
        type:"POST",
    

    Problem 2: Your javascript data variable should be key: value pairs like:

    var data = { 'passcode' : code };
    

    Then in PHP get the data with $_POST['passcode']

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀