Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/422.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php AJAX POST请求接受数字,但不接受字符串_Php_Javascript_Html_Ajax - Fatal编程技术网

Php AJAX POST请求接受数字,但不接受字符串

Php AJAX POST请求接受数字,但不接受字符串,php,javascript,html,ajax,Php,Javascript,Html,Ajax,好的,我已经到处寻找这个问题,但是我只能找到对JSON的引用,而我目前没有使用JSON。其中包含数字的数组值通过,DIV更新。然而,当我试图传递字符串时,什么也没发生。代码如下: <php> $cont = array(); $cont[] = 'yo'; $cont[] = '2'; foreach($cont as $c){ $statement .= '<button type=\"button\" onclick=\"nFunc('.$c.')\">'.$c

好的,我已经到处寻找这个问题,但是我只能找到对JSON的引用,而我目前没有使用JSON。其中包含数字的数组值通过,DIV更新。然而,当我试图传递字符串时,什么也没发生。代码如下:

<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}

$cont=数组();
$cont[]=“yo”;
$cont[]=“2”;
foreach(续为$c){
$statement.=''.$c.';
}
函数nFunc(str)
{
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status==200)
{
document.getElementById(“myDiv”).innerHTML=xmlhttp.responseText;
}
}
open(“POST”,“test.php”,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded”);
xmlhttp.send(“p=“+str”);
}
违约
{$statement}
请注意,我正在通过IP.Board/IP.Content使用AJAX进行测试,因此{}中的标记和变量由IP.C引擎解析

此代码输出两个标记为“yo”和“2”的按钮。单击“2”时,DIV将正确更新。单击“yo”时,不会发生任何事情

test.php文件非常简单:

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


感谢您事先提供的帮助。

您的
PHP
语句中的
HTML
输出无效:

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

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

我刚刚测试了一下,修复后它就像一个魔咒一样工作。

使用
var\u dump
而不是
echo
。它将提供有关变量的详细信息。您使用的是哪个版本的html?@PLB'var_dump'没有帮助,它还提供了我不想要的数据位置。@DinoBabu这是HTML5,但是IP.B处理tags@DeathDream你需要调试你的代码,这样你就需要更多关于传递什么的信息来更快地发现问题。我考虑过这一点,并尝试过,但是我通过IP.B IP.Board消息得到了一个编译器错误。你提交的内容在编译成适当的PHP代码时会产生一个解析器错误。请验证任何PHP代码、模板标记和模板插件标记(如解析块)并重新提交您的数据。抱歉,我不知道
B IP、Board Message
或其格式。但就PHP和HTML语法而言,问题在于上面提到了什么
$statement .= '<button type="button" onclick="nFunc(\''.$c.'\')">'.$c.'</button>';