【问题标题】:Ajax is not sending the data I set to phpAjax 没有发送我设置到 php 的数据
【发布时间】:2022-11-01 13:00:35
【问题描述】:

我正在尝试使用 ajax 将表单数据发送到 php,但它不起作用,我在没有解决任何解决方案的情况下搜索了很多 `

这是我 index.js 中的 ajax 代码 `

$("#reg_form").submit(function (event) {
    
    alert("clicked")
    var registerData = {
        ajxfname: document.getElementById("fnameInput").value,
        ajxlname: document.getElementById("lnameInput").value,
        ajxemail: document.getElementById("emailInput").value,
        test: "this is test text"
    };

    $.ajax({
        type: "POST",
        url: "server.php",
        data: registerData,
        dataType: "json",
        success: function (response) {
            alert("success");
        }
    });
     event.preventDefault();

});

`

这是我用来打印在 server.php 中获得的数据的代码

`

foreach ($_POST as $key => $value) {
    echo $key;
    echo "  : ";
    echo $value;
    echo "<br>";
}

`

它只是在 html 中以 (fname,lname,email) 的形式打印输入的名称和数据

这是html中的表格 `

 <form id="reg_form"  action="server.php" method="post" >

                <input  id="fnameInput" name="fname" placeholder="First name" type="text">

                <input  id="lnameInput" name="lname" placeholder="Last name" type="text">

                <input  id="emailInput" name="email" placeholder="E-mail" type="text">

                <button class="btn" type="submit" >Sign up</button>

`

提前致谢

【问题讨论】:

  • 尝试将您的$key$value 保存在一个数组中,然后echo json_encode($arrValue)

标签: php ajax


【解决方案1】:

试试下面,例如:

$("#reg_form").submit(function (event) {
    event.preventDefault();
    alert("clicked")

    var formData = {
      fname: $("#fnameInput").val(),
      lname: $("#lnameInput").val(),
      email: $("#emailInput").val(),
    };

    $.ajax({
        type: "POST",
        url: "server.php",
        data: formData,
        dataType: "json",
        success: function (response) {
            alert("success");
        }
    });
});

或者

$("#reg_form").submit(function (event) {
    event.preventDefault();
    alert("clicked")
    var form = $(this);
    $.ajax({
        type: "POST",
        url: "server.php",
        data: form.serialize(),
        dataType: "json",
        success: function (response) {
            alert("success");
        }
    });
});

在 PHP 中

extract($_POST);
    foreach ($_POST as $key => $value) {
        echo $key;
        echo "  : ";
        echo $value;
        echo "<br>";
    }

【讨论】:

    【解决方案2】:

    我认为 Jquery Ajax 已经过时了你可以使用 javascript Fetch尝试这个

    $("#reg_form").submit(function (event) {
         event.preventDefault();
    
        alert("clicked")
        const registerData = {
            ajxfname: document.getElementById("fnameInput").value,
            ajxlname: document.getElementById("lnameInput").value,
            ajxemail: document.getElementById("emailInput").value,
            test: "this is test text"
        };
    
    // POST request using fetch()
    fetch("/server.php", {
         
        // Adding method type
        method: "POST",
         
        // Adding body or contents to send
        body: JSON.stringify(registerData),
         
        // Adding headers to the request
        headers: {
            "Content-type": "application/json; charset=UTF-8"
        }
    })
     
    // Converting to JSON
    .then(response => response.json())
     
    // Displaying results to console
    .then(json => console.log(json));
        
    
    });
    

    server.php 文件

    <?php
    
     print_r($_REQUEST);
    die;
    ?>
    

    如果print_r 为空尝试这个

    <?php 
        $content_raw = file_get_contents("php://input"); 
        $decoded_data = json_decode($content_raw, true); 
    
        print_r(decoded_data);
        die;
    
    ?>
    

    【讨论】:

      猜你喜欢
      • 2014-01-23
      • 2016-10-17
      • 1970-01-01
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      相关资源
      最近更新 更多