duanhuan2301 2017-07-27 06:30
浏览 36
已采纳

Ajax不向PHP发送变量

I need to send variable from JS and use it in PHP code. I wrote this function and downloaded it in PHP. It doesn't work, why?

$("#gen").on("click", function () {

    request = new XMLHttpRequest();

    var url = "file.php";

    var test = "data="+document.getElementById("test").value;


    request.open("POST",url,true);

    request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

    request.setRequestHeader("Content-length", test.length);


    request.onreadystatechange=checkResult;

    request.send(test);

    request.setRequestHeader("Connection", "close");

});

<form method="post" action="file.php">

I downloaded variable this way:

$test = $_POST['test'];

Here is the link of my code :- Pastebin link

  • 写回答

3条回答 默认 最新

  • duanmei1894 2017-07-27 06:46
    关注

    Edit as per this comment:

    You don't need any JavaScript to accomplish "post and redirect in one go".

    Simply use your <input type="submit" id="gen" value="Generate" /> as it is and it should POST all data in the form to your PHP file. If you need other variables to be passed to your server, make a few <input type="hidden"> fields on your page and make their values your JS variables (using $("#input-id").val(yourVariable); )


    This Code answers the initial question

    The Error in your Client-Side Code:

    You specifically used the type submit in this button <input type="submit" id="gen" value="Generate" />. What that does is tell the browser to stop everything and submit the nearest form. If you instead want to do something else with a button you have to use

    $("#gen").on("click", function (e) {
    
        e.preventDefault();
    
        request = new XMLHttpRequest();
    
        var url = "file.php";
    
        var test = "data="+document.getElementById("test").value;
    
    
        request.open("POST",url,true);
    
        request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    
        request.setRequestHeader("Content-length", test.length);
    
    
        request.onreadystatechange=checkResult;
    
        request.send(test);
    
        request.setRequestHeader("Connection", "close");
    
    });
    

    e.preventDefault(); tells your browser not to do what it was instructed to do by the type="submit but rather run your js code.

    The others are right you can get your data using $test = $_POST["data"];. Here's why:

    When sending your Data you build a x-www-form-urlencoded string (this one: "data="+document.getElementById("test").value;).

    To you and to your connection, this is simply a String that gets appended to your request, but your Server (your file.php) interprets this as an associative array, meaning a Data Structure with a key (data) and a value (document.getElementById("test").value). When you submit a form your browser does this automatically, it appends all data in the form to your request and sends it to the server that way.

    Every request with added data follows this pattern. When you take a look at YouTube for Example, their video-urls all contian ?v=.... If you wanted to built YouTube in PHP you would get the variable using $video = $_GET["v"].

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

报告相同问题?

悬赏问题

  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路