如何在PHP的帮助下回声AJAX数据?

发布于 2025-01-25 07:37:32 字数 990 浏览 2 评论 0原文

我试图打印用户输入的数据,并通过Ajax传输并尝试在PHP代码的帮助下打印或回声,但我无法做到这一点。 <代码>在此处输入代码 这是我的代码:

<html>

  <head>
    <title>
      test ajax
    </title>
    <script src="jquery.js"></script>
  </head>

  <body>
    <h2>test button</h2><br>
    <form>
      <input type="text" id="a"><br>
      <input type="button" id="b" value="display">
    </form>
    <script>
      $(document).ready(function() {
        $('#b').click(function() {
          let a = $('#a').val();
          alert(a);
          $.ajax({
            type: "POST",
            url: "same_file.php",
            data: {
              c: a
            },
            success: function() {}

          });

        });
      });

    </script>
  </body>

</html>
<?php
    extract($_POST);
    if(isset($_POST["c"])) {
    echo $_POST["c"];
    }
 ?>

我认为,我有PHP代码的问题。请给我任何解决方案。

I trying to print the data which was entered by the user, and transfer that through ajax and trying to print or echo it with the help of php code, but I could not do that. enter code here
this is my code:

<html>

  <head>
    <title>
      test ajax
    </title>
    <script src="jquery.js"></script>
  </head>

  <body>
    <h2>test button</h2><br>
    <form>
      <input type="text" id="a"><br>
      <input type="button" id="b" value="display">
    </form>
    <script>
      $(document).ready(function() {
        $('#b').click(function() {
          let a = $('#a').val();
          alert(a);
          $.ajax({
            type: "POST",
            url: "same_file.php",
            data: {
              c: a
            },
            success: function() {}

          });

        });
      });

    </script>
  </body>

</html>
<?php
    extract($_POST);
    if(isset($_POST["c"])) {
    echo $_POST["c"];
    }
 ?>

I think that, I have the problem with the php code. please give me any solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救赎№ 2025-02-01 07:37:32

试试看,

这是您的html,

<html>
  <head>
    <title>
      test ajax
    </title>
  <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  </head>
  <body>
    <h2>test button</h2><br>
    <form>
      <input type = "text" id = "a" > <br>
      <input type = "button" id = "b"  value = "display" >
    </form>
  </body>
</html>

<script>
    $(document).ready(function(){
        $('#b').click(function() {
            let a = $('#a').val();
            //alert(a);
            $.ajax({
                type: "POST",
                url: "same_file.php",
                data: {c: a},
                success: function(response) {
                    alert(response);
                }

            });    

        });
    });
</script>

这是您的same_file.php

<?php
//extract($_POST); code works even without this

if(isset($_POST["c"])) {
  echo $_POST['c'];
}
?>

不确定您的jQuery库调用是否可以,所以我在网上打了一个。另外,您的成功:function()没有做任何事情,这就是为什么您什么都没有收到任何东西。

同样,请始终用一些凹痕格式化代码,以便更容易被他人阅读和理解。

Try it like this

This is your Html

<html>
  <head>
    <title>
      test ajax
    </title>
  <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  </head>
  <body>
    <h2>test button</h2><br>
    <form>
      <input type = "text" id = "a" > <br>
      <input type = "button" id = "b"  value = "display" >
    </form>
  </body>
</html>

<script>
    $(document).ready(function(){
        $('#b').click(function() {
            let a = $('#a').val();
            //alert(a);
            $.ajax({
                type: "POST",
                url: "same_file.php",
                data: {c: a},
                success: function(response) {
                    alert(response);
                }

            });    

        });
    });
</script>

This is your same_file.php

<?php
//extract($_POST); code works even without this

if(isset($_POST["c"])) {
  echo $_POST['c'];
}
?>

Not sure if your jquery library call is ok so I called the one on the web instead. Also your success:function() was not doing anything which is why you didn't receive anything.

Also always format your code with some indention so that it can be easier to read and comprehend by others.

月亮坠入山谷 2025-02-01 07:37:32

您可能想在渲染身体之前回声。
但是,除非您需要处理用户发送的数据,否则您根本不需要PHP。

实际上,您可能也不需要jQuery,fetch()效果很好。

$(document).ready(function() {
  $('#b').click(function() {
    let a = $('#a').val();
    alert(a);
    $.ajax({
      type: "POST",
      url: "same_file.php",
      data: {
        c: a
      },
      success: function() {}
    });
  });
});
<?php
    if (isset($_POST["c"]))
    {
      // print/echo user data send from form.
      echo $_POST["c"];

      // stop the script from rendering the html below.
      exit();
    }
 ?>
  <html>

  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  </head>

  <body>
    <h2>test button</h2><br>
    <form>
      <input type="text" id="a"><br>
      <input type="button" id="b" value="display">
    </form>
    <!-- add script element with js code here -->
  </body>

  </html>

You probably want to echo before the body is rendered.
But in true, unless you need to process the data the user sends, you don't need PHP at all.

And in truth, you probably don't need jQuery as well, fetch() works just fine.

$(document).ready(function() {
  $('#b').click(function() {
    let a = $('#a').val();
    alert(a);
    $.ajax({
      type: "POST",
      url: "same_file.php",
      data: {
        c: a
      },
      success: function() {}
    });
  });
});
<?php
    if (isset($_POST["c"]))
    {
      // print/echo user data send from form.
      echo $_POST["c"];

      // stop the script from rendering the html below.
      exit();
    }
 ?>
  <html>

  <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  </head>

  <body>
    <h2>test button</h2><br>
    <form>
      <input type="text" id="a"><br>
      <input type="button" id="b" value="display">
    </form>
    <!-- add script element with js code here -->
  </body>

  </html>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文