AJAX功能将我的屏幕变为空白

时间:2013-11-15 09:58:15

标签: php mysql ajax

所以我刚刚发现了如何使用ajax POST到PHP文件。 我想要做的是将一个名字和死亡原因POST到一个PHP脚本,然后使用Mysql将用户添加到数据库中。 但是,当我按下提交按钮并运行脚本(我在页面中的其他位置,它工作正常)它将我的屏幕变为白色,并且没有任何内容添加到数据库。

该页面位于http://rhoiyds.com/deathnote/index.php 自己试试吧。使用箭头键滚动到书的最后一页以添加内容。 此外,我在搜索功能中使用相同类型的脚本,它工作,但添加功能不。 请帮忙? 我怀疑这行代码:

xmlhttp.send("name="+document.getElementById("nameadd").value+"&cod="+document.getElementById("codadd").value);

我应该有第二个+符号吗?&在引号内吗?我还是不认为会把屏幕发送空白......

编辑:添加代码:

<script>
function loadXMLDoc()
{
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("result").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST","check.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("name="+document.getElementById("name").value);
}
</script>
<script>
function write()
{
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("result").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST","write.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("name="+document.getElementById("nameadd").value+"&cod="+document.getElementById("codadd").value);
}
</script>
</head>

顶级脚本用于写入数据库 底部脚本用于检查数据库中的名称

以下是用户输入的表单(实际上不是表单)。

    <div class="content"><div class="name">Name: <br/> Cause Of Death: </div>
<div class="cod"><textarea type="text" name="nameadd" id="nameadd" maxlength="25"
 placeholder="Input Name" required>
</textarea> <br /> <textarea type="text" name="cod" id="codadd" maxlength="130" rows="4" placeholder="Cause of Death" required>
</textarea> <br />
<button type="button" onclick="write()" return false>Submit</button></div></div>';

用于检查名称的PHP脚本

    /* create a prepared statement */ if ($stmt = mysqli_prepare($link, "SELECT cod FROM deathnote WHERE victim=?")) {

        /* bind parameters for markers */
        mysqli_stmt_bind_param($stmt, "s",  $_POST['name']);

        /* execute query */
        mysqli_stmt_execute($stmt);

        /* bind result variables */
        mysqli_stmt_bind_result($stmt, $cod);

        /* fetch value */
        mysqli_stmt_fetch($stmt);

      if ($cod=="") {   $cod= $_POST['name'] . " is not in the deathnote.";   }

      echo $cod;

        /* close statement */
        mysqli_stmt_close($stmt); }

    /* close connection */ mysqli_close($link);

PHP SCRIPT FOR ADDING NAMES

$sql="INSERT INTO deathnote (victim, cod)
VALUES
('$_POST[name]','$_POST[cod]')";

if (!mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }


mysqli_close($con);

echo "success";

2 个答案:

答案 0 :(得分:0)

您需要停止实际提交表单。最简单的解决方法是改变这个:

<button type="button" onclick="loadXMLDoc()">Submit</button>

到此:

<button type="button" onclick="loadXMLDoc(); return false">Submit</button>

可能还有其他问题,但首先尝试

答案 1 :(得分:0)

问题不在于我的AJAX功能,问题是有人用SQL为我的数据库注入了CSS规则。 :(