dsiy62758 2013-06-18 21:38
浏览 126

从javascript传递参数到PHP

I've done this before but for some reason the parameters are being passed oddly.

I have a javascript function that I've used to pass parameters, I've ran some tests and in the function the variables are correct.

These are just a few snippets of the js that relate to the issue:

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);

I ran a web console and this is what is actually getting passed...

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

I'm not sure what's going on in between the xmlhttp.open and the GET method in php. None of these variables are getting passed.

  • 写回答

2条回答 默认 最新

  • dongtiao1817 2013-06-18 21:56
    关注

    Why not use jQuery - very straightforward format (I prefer 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()
    

    Notice how easy the success callback function is to write... anything returned by subTask.php will be available within that function, as seen by the alert() example.

    Just remember to include the jQuery library in the <head> tags:

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

    Also, add this line to the top of your subTask.php file, to see what is happening:

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

    The values of q= and w= will be returned to you in an alert box so that (at least) you can see what values they contained when received by subTask.php

    评论

报告相同问题?

悬赏问题

  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示