简体   繁体   English

我想使用 jquery ajax 将变量从一个 php 页面发送到另一个页面

[英]i want to send variable from one php page to another using jquery ajax

Page 1 js:第 1 页 js:

  $(document).ready(function () {
    $(".btn").on("click", function () {
      $.ajax({
        type: "GET",
        url: "./page.php",
        data: { name: "peter" },
        success: function (data) {
          alert(data);
        },
      });
    });
  });

Page 2 php:第 2 页 php:

<?php
if(isset($_GET['name'])){
    $name =$_GET['name'];
    echo $name;
}
else{
    echo "data not recived";
}


?>

Here, at the first page it triggers the alert and displays the value of "name" but it doesn't return the value to the destination url and it executes the else statement.在这里,它在第一页触发警报并显示“name”的值,但它不会将值返回到目标 url 并执行 else 语句。 Thanks for your help!谢谢你的帮助!

Sorry, I don't have enough reputation to leave comment.抱歉,我没有足够的声誉来发表评论。 I tested your code, and your code is correct.我测试了你的代码,你的代码是正确的。 I don't know why you cannot get your expected result.我不知道为什么你不能得到你预期的结果。 But I think you can try the go to http://localhost/page.php?name=aaa in your browser to see it will show "aaa" or "data not recived"但我想你可以试试浏览器中的 go 到http://localhost/page.php?name=aaa看看它会显示“aaa”或“data not recived”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM