从javascript传递参数到PHP

时间:2013-06-18 21:38:17

标签: php javascript html ajax

我以前做过这个,但由于某种原因,参数被奇怪地传递了。

我有一个用于传递参数的javascript函数,我已经运行了一些测试,并且在函数中变量是正确的。

这些只是与问题相关的js的几个片段:

var tdes = document.getElementById("taskDescription1").value;
var tnam = document.getElementById("taskName1").value;
var shif = document.getElementById("shift1").value;
var ttyp = document.getElementById("taskType1").value;
var date = document.getElementById("datepicker").value;
var ooc = document.getElementById("ooc1").value;
var dateSplit = date.split('/');
var deadlineDate = "";



for( var i = 0; i < dateSplit.length; i++){
deadlineDate = deadlineDate + dateSplit[i]; 
}
xmlhttp.open("GET","subTask.php?q="+ encodeURIComponent(tdes) + "&w=" + encodeURIComponent(tnam) +"&e=" +encodeURIComponent(shif) + "&y=" + encodeURIComponent(ttyp) + "&b=" + encodeURIComponent(deadlineDate) + "&u=" + encodeURIComponent(ooc),true);

我运行了一个Web控制台,这就是实际传递的内容......

http://***************/****/********/subTask.php?taskName1=test+taskname+works&taskDescription1=test+des&shift1=All&ooc1=Open&taskType1=normal&datepicker=06%2F28%2F2013

我不确定xmlhttp.open和php中的GET方法之间发生了什么。这些变量都没有通过。

2 个答案:

答案 0 :(得分:0)

为什么不使用jQuery - 非常简单的格式(我更喜欢POST ...):

$(document).ready(function() {
    var tdes = $("#taskDescription1").val();
    var tnam = $("#taskName1").val();
    var shif = $("#shift1").val();
    var ttyp = $("#taskType1").val();
    var date = $("#datepicker").val();
    var ooc = $("#ooc1").val();
    var dateSplit = date.split('/');
    var deadlineDate = "";

    for( var i = 0; i < dateSplit.length; i++){
        deadlineDate = deadlineDate + dateSplit[i]; 
    }

    $.ajax({
        type: "POST",
        url: "subTask.php",
        data: "q="+ encodeURIComponent(tdes) + "&w=" + encodeURIComponent(tnam) +"&e=" +encodeURIComponent(shif) + "&y=" + encodeURIComponent(ttyp) + "&b=" + encodeURIComponent(deadlineDate) + "&u=" + encodeURIComponent(ooc),true),
        success: function(whatigot) {
            alert('Server-side response: ' + whatigot);
        } //END success fn
    }); //END $.ajax

}); //END document.ready()

注意success回调函数写入是多么容易... subTask.php返回的任何内容都将在该函数中可用,如alert()示例所示。

请记住在<head>标记中包含jQuery库:

<head>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
</head>

此外,将此行添加到subTask.php文件的顶部,以查看发生的情况:

<?php
    $q = $_POST["q"];
    $w = $_POST["w"];
    die("Value of Q is: " .$q. " and value of W is: " .$w);

q=w=的值将在警告框中返回给您,以便(至少)您可以看到subTask.php收到它们时包含的值

答案 1 :(得分:0)

以下脚本应该有所帮助:

function ajaxObj( meth, url ) 
{   
var x = false;
if(window.XMLHttpRequest)
    x = new XMLHttpRequest();
else if (window.ActiveXObject) 
    x = new ActiveXObject("Microsoft.XMLHTTP");  
x.open( meth, url, true );
x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
return x;
}
function ajaxReturn(x){
    if(x.readyState == 4 && x.status == 200){
        return true;    
    }
}

    var ajax = ajaxObj("POST", "subTask.php");
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
           console.log( ajax.responseText )
        }
    }
    ajax.send("u="+tdes+"&e="+tnam+ ...... pass all the other 'n' data );