0

Alright, I have looked around for this problem, but I can only find references to JSON, which I am currently not using. The array value that has a number in it passes, and the DIV updates. However, when ever I try to pass in a string, nothing happens. Here is the code:

<php> 
$cont = array();
$cont[] = 'yo';
$cont[] = '2';
foreach($cont as $c){
  $statement .= '<button type=\"button\" onclick=\"nFunc('.$c.')\">'.$c.'</button>';
}
</php>
<script>

function nFunc(str)
{
var xmlhttp;
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("myDiv").innerHTML=xmlhttp.responseText;
    }
}
xmlhttp.open("POST","test.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("p="+str);
}
</script>
<div id="myDiv">Default</div>
{$statement}

Please note I am doing testing with AJAX via IP.Board/IP.Content, so the tags and variables in {} are parsed by the IP.C engine.

This code outputs two buttons labeled "yo" and "2". When "2" is clicked, the DIV updates correctly. When "yo" is clicked, nothing occurs.

The test.php file is very simple:

<?php
$hello = $_POST['p'];
echo $hello;
?>

Thanks for any help beforehand.

4

1 回答 1

0

HTML您的PHPStatement 的此输出无效:

<button type=\"button\" onclick=\"nFunc(yo)\">yo</button><button type=\"button\" onclick=\"nFunc(2)\">2</button>

注意 onclick=\"nFunc(yo)\">

请参阅您的字符串参数没有用引号引起来,并且那里也不需要那些反斜杠。这就是为什么您会看到该错误,这当然不会在数字的情况下发生。

$statement .= '<button type=\"button\" onclick=\"nFunc('.$c.')\">'.$c.'</button>';

应该

$statement .= '<button type="button" onclick="nFunc(\''.$c.'\')">'.$c.'</button>';

修复后它就像一个魅力,我刚刚测试过。

于 2013-03-18T08:11:18.677 回答