如何使用 jQuery ajax 发送数据

发布于 2025-01-16 01:25:22 字数 926 浏览 0 评论 0原文

这是我的 html

<!-- <form action="view.php" method="post">  -->
        <input type="text" name="text" id="name">
        <input type="submit" value="submit">
    <!-- </form> -->

我不想使用表单发送数据,因为它只发送命名属性数据,这就是我通过 $.ajax() 方法发送数据的原因 这是脚本,

$(document).ready(function () {
    var details = {};
    $('input[type="submit"]')
        .click(function () {
            details = { name: $('input[type="text"]').val() };
            $.post({
                url: "view.php",
                method: "post",
                data: details,
                success: function (result) {
                    console.log("done")}
             });
           window.open('view.php','_self');
});});

毕竟我想检索 view.php 文件上的数据 但我无法在该文件上实现它,它显示数组为空,

<?php
var_dump( $_POST);
//the array is empty
?>

请给我一些建议。

this is my html

<!-- <form action="view.php" method="post">  -->
        <input type="text" name="text" id="name">
        <input type="submit" value="submit">
    <!-- </form> -->

I don't want to send the data by using form Couse it only send named attribute data that's why I am sending data by $.ajax() method
and this is script

$(document).ready(function () {
    var details = {};
    $('input[type="submit"]')
        .click(function () {
            details = { name: $('input[type="text"]').val() };
            $.post({
                url: "view.php",
                method: "post",
                data: details,
                success: function (result) {
                    console.log("done")}
             });
           window.open('view.php','_self');
});});

after all this thing i want to retrieve the data on view.php file
but i am fail to achieve it on that file it is showing the array is empty

<?php
var_dump( $_POST);
//the array is empty
?>

suggest me something please.

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

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

发布评论

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

评论(2

稳稳的幸福 2025-01-23 01:25:22

ajax 请求和 window.open 发出的请求是两个不同的请求。可以把它想象成你接到的两个不同的电话……并且对最后的谈话完全没有“记忆”。您需要一个数据库来存储“内存”,但它是别的东西。

要将一些数据发送到后端(以对其进行处理)并希望将结果返回到原始页面,请使用 success 回调。

原始页面中的 HTML:

<input type="text" name="text" id="name">
<input type="button" value="submit">
<div id="backendResult"></div>

原始页面中的 JS:

$(document).ready(function () {
  var details = {};
  $('input[type="submit"]').click(function () {
    details = { name: $('input[type="text"]').val() };
    $.post({
      url: "view.php",
      method: "post",
      data: details,
      success: function (result) {
        console.log("done")
        $('#backendResult').html(result)
      }
    });
  });
});

view.php:

<?php
$name = $_POST['name'];
echo 'Hello '.$name.'!';
?>

The ajax request and the request made from window.open are two different requests. Think of it like two different phone calls that you'd receive... And having absolutely no "memory" of the conversation at the end. You would need a database for that "memory" and it's something else.

To send some data to the backend (to process it) and want to have a result back into the original page, use the success callback.

HTML in original page:

<input type="text" name="text" id="name">
<input type="button" value="submit">
<div id="backendResult"></div>

JS in original page:

$(document).ready(function () {
  var details = {};
  $('input[type="submit"]').click(function () {
    details = { name: $('input[type="text"]').val() };
    $.post({
      url: "view.php",
      method: "post",
      data: details,
      success: function (result) {
        console.log("done")
        $('#backendResult').html(result)
      }
    });
  });
});

view.php:

<?php
$name = $_POST['name'];
echo 'Hello '.$name.'!';
?>
看透却不说透 2025-01-23 01:25:22

你可以这样做:

$(document).ready(function () {
    var details = {};
    $('input[type="submit"]')
        .click(function () {
            details = { name: $('input[type="text"]').val() };
            $.post({
                url: "view.php",
                method: "post",
                data: details,
                success: function (result) {  // {name : 'the text you typed'}
                    console.log("done")
                    $("body").load('view.php', function(){
                       $("#name").val(result.name);
                    });
                }
            });
        });
});

You can do like this:

$(document).ready(function () {
    var details = {};
    $('input[type="submit"]')
        .click(function () {
            details = { name: $('input[type="text"]').val() };
            $.post({
                url: "view.php",
                method: "post",
                data: details,
                success: function (result) {  // {name : 'the text you typed'}
                    console.log("done")
                    $("body").load('view.php', function(){
                       $("#name").val(result.name);
                    });
                }
            });
        });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文