无法在ajax中传递值

时间:2012-08-13 20:46:22

标签: php

我试图通过以下代码传递价值,但无法取悦任何人检查可能我在某处错误

<input type="button" name="vote" value="Yes" width="100" onclick="getVote('; echo "y".$info['autoid']; echo')"/>

问题出在以下行“y”。$ info ['autoid']; 如果我删除“y”。它的工作正常。但我想传递y和产品ID。

基本上我试图将y与int联合起来。

这里是完整代码。

 <script type="text/javascript">

  function getVote(int)
  {
  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("poll").innerHTML=xmlhttp.responseText;
           }
    }
 xmlhttp.open("GET","../poll/"+int,true);
xmlhttp.send();
}
</script>


<div id="poll">
 <form>
poll - :
<input type="button" name="vote" value="Yes" width="100" onclick="getVote('; echo     "y".$info['autoid']; echo')"/>
<input type="button" name="vote" value="No" width="100" onclick="getVote('; echo   "n".$info['autoid']; echo')"/>
</form>
 </div>

谢谢你们......我找到了另一种方式,我们可以通过“ - ”表示“是”,空白表示“n”

<input type="button" name="vote" value="Yes" width="100" onclick="getVote('; echo "-".$info['autoid']; echo')"/>
<input type="button" name="vote" value="No" width="100" onclick="getVote('; echo $info['autoid']; echo')"/>

3 个答案:

答案 0 :(得分:1)

你试图在双引号中包含双引号,并且不清楚你的php包装器在哪里?

我将onclick属性重写为:

onclick="getvote( <?php echo "y{$info['autoid']}"?> "

答案 1 :(得分:0)

int是javascript中的保留字,你也应该使用像JQuery这样的东西来处理你的ajax调用。

答案 2 :(得分:0)

你的代码在问题出现的地方有点混乱。

使用此:

<input type="button" name="vote" value="Yes" width="100" onclick="getVote('y<?php echo $info['autoid']; ?>');"/>