正在数据库中创建新数据


Creating new data in database

这是我第一次尝试使用AJAX将数据发送到php,我认为我做错了什么,因为我在mysql中看不到任何新数据,以下是我尝试的内容。

这个脚本通过点击img调用ajax:

$s .= "'n't<td>";
$canEdit = getPermission('tasks', 'edit', $a['task_id']);
$canViewLog = getPermission('task_log', 'view', $a['task_id']);
if ($canEdit) {
    $s .= ("'n't't".'<a href="#">'
           . "'n't't't".'<img src="./images/icons/tick.png" alt="' . $AppUI->_('Check') 
           . '" border="0" width="12" height="12" onclick="javascript:insertData()" />' . "'n't't</a>");
}
$s .= "'n't</td>";
$currentTasken=$a['task_id'];
$currentUser=$AppUI->user_id;

这是我的ajax函数,它将数据发送到php文件:

?>
<script type="text/javascript">
    function insertData()
    {
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("POST","datafile.php",true);
xmlhttp.send("<?php echo $currentUser; ?>");
xmlhttp.send("<?php echo $currentTasken; ?>")
    }
</script>

这是从AJAX接收数据的PHP文件:

<?php
$currentUser = $_POST['$currentUser'];
$currentTasken = $_POST['$currentTasken'];
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
if(!$con)
    die('Could not connectzzz: ' . mysql_error());
mysql_select_db("foxi" , $con) or die ("could not load the database" . mysql_error());
$check = mysql_query("SELECT * FROM dotp_task_log");
$numrows = mysql_num_rows($check);
if($numrows == 0)
{
    $pass = md5($pass);
        $ins = mysql_query("INSERT INTO dotp_task_log (`task_log_creator`,`task_log_Task`) VALUES ('$currentUser' , '$currentTasken')" ) ;
   if($ins)
        die("Succesfully Created Log!");
    else
        die("ERROR");
}
else
{
    die("Log already exists!");
}
?>

更改如下:

<?php
$currentUser = $_POST['currentUser'];
$currentTasken = $_POST['currentTasken'];
$con = mysql_connect("localhost", "root", "") or die(mysql_error());
if(!$con)
    die('Could not connectzzz: ' . mysql_error());
mysql_select_db("foxi" , $con) or die ("could not load the database" . mysql_error());
$check = mysql_query("SELECT * FROM dotp_task_log");
$numrows = mysql_num_rows($check);
if($numrows == 0)
{
    $pass = md5($pass);
        $ins = mysql_query("INSERT INTO dotp_task_log (`task_log_creator`,`task_log_Task`) VALUES ('$currentUser' , '$currentTasken')" ) ;
   if($ins)
        die("Succesfully Created Log!");
    else
        die("ERROR");
}
else
{
    die("Log already exists!");
}
?>

还有你的脚本:

    <script type="text/javascript">
    function insertData()
    {
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.open("POST","datafile.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("currentUser=<?php echo $currentUser; ?>&currentTasken=<?php echo $currentTasken; ?>");
    }
    </script>

使用ajax需要不同的思维方式。我会给你一些概念(如果你还没有的话),这些概念不仅能帮助你解决这个问题,还能帮助你解决未来的问题,并节省时间:

  • 在编写服务器端和客户端逻辑之后,必须分别对其进行测试所以,首先测试服务器端(php部分):如何?制作一个简单的sily-static表单,只用于将数据发布到服务器端逻辑,以测试它是否正确地将数据保存在数据库中并返回所需的数据。

  • 在测试了服务器端之后,您可以确定,如果您的应用程序不工作,那么问题肯定出在客户端。所以,我们需要测试一下。你已经知道如何使用浏览器的控制台了吗?在Firefox中,MacOS是[Command]+[Option]+[k]在Windows中应该是[ctrl]+[shift]+[k]它会在浏览器底部打开,你应该标记[network]以保持记录网络请求,如果你的客户端正在工作,你可以通过ajax看到你请求的页面,它的http状态代码(应该是200)。

  • 如果客户端能够通过ajax调用服务器端,并且您确信它是这样做的,因为您在浏览器控制台上看到了post请求,但仍然没有将数据正确保存在数据库中,那么可能您在发送数据时遇到了问题,或者它不是通过ajax发送的。你也可以在浏览器的日志上测试它,用右键点击日志中显示的页面并标记该选项,也许英文会写为:"存储请求/响应内容"(我的浏览器是葡萄牙语)标记此选项后,再次提出请求,然后用左键单击请求的页面。您将看到通过ajax发送到服务器端的所有标题和数据。所以,检查一下是否没有问题

仅此而已但是,考虑使用jQuery,它并不难理解,而且ajax的工作方式更简单。看看这个例子:

var request = $.ajax({
  url: "script.php",
  method: "POST",
  data:$('#form-id').serialize(), // gets all form fields serialized creating a string fieldName1=value1&fieldName2=value2...
  dataType: "html"
}).done(function( msg ) { // after it's done
  $( "#log" ).html( msg );
  alert(msg)
}).fail(function( jqXHR, textStatus ) {
  alert( "Request failed: " + textStatus );
});

顺便说一句,如果你必须在html的任何部分(包括javascript部分)上回显一些php值,就像你在中所做的那样

xmlhttp.send("$currentUser");
xmlhttp.send("$currentTasken");

要获得变量值,您应该将其回显为:

xmlhttp.send("<?=$currentUser?>");
xmlhttp.send("<?=$currentTasken?>");

祝你好运!