无法在ajax中发送多个变量

时间:2015-11-03 08:43:32

标签: javascript php ajax

我有一个按钮,当点击时我需要通过get请求发送两个变量。如果我只有一个变量它可以正常工作,但只要我添加第二个(str2)(称为q)它就不会做任何事情。

脚本:

<script name="editresults">
    function editresults(str,str2) {
    if (str == "") {
        document.getElementById("mainpart").innerHTML="";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("mainpart").innerHTML=xmlhttp.responseText;
            }
            $(document).ready(function() { $("#datepicker").datepicker({ dateFormat: 'dd-mm-yy', changeMonth: true,
            changeYear: true, yearRange: '1930:2015'}); });
        }
        xmlhttp.open("GET","editemployeedetailsform.php?id="+str+"&q="+str2,true);
               $('#editemployeedetailsform form').submit(function(){
          var data=$(this).serialize();
          // post data
          $.post('submit.php', data , function(returnData){
                      $('mainpart').html( returnData)
          })

          return false; // stops browser from doing default submit process
    });
        xmlhttp.send();
    }
} 
</script>

按钮代码:

<td><input type='button' value='Edit' onclick='editresults(<?php echo $surnames[$k]["employeeid"] ?>,<?php echo $q ?>)'></td>

1 个答案:

答案 0 :(得分:0)

看起来这不是ajax调用的问题,而是混合了JavaScript和PHP。 我猜$surnames[$k]["employeeid"]是一个整数,它没有引号[&#34;],但第二个参数<?php echo $q ?>可能是一个字符串,需要用引号[&#34;] < / p>

尝试使用此按钮代码:

<td><input type='button' value='Edit' onclick='editresults(<?php echo $surnames[$k]["employeeid"] ?>,"<?php echo $q ?>")'></td>

更多信息: 如果php解释器添加了一个字符串,例如&#39; test&#39; javascript解释器的结果是onclick='editresults(1, test);' 这意味着测试将被视为变量。

相关问题