0

我有这个 AJAX 函数,它应该将数据传递给 php 代码,然后应该将它输入到正在发生的数据库中的数据中,但 AJAX 确实没有按应有的方式工作。

             <?php         
     session_start();
    include_once("../engine/database.php");
    if (isset($_POST["name"],$_POST["password"]) ) {      
      $uname  = htmlspecialchars(mysql_real_escape_string($_POST["name"]) );
      $passwd = htmlspecialchars(mysql_real_escape_string($_POST["password"]) );
      $query = "SELECT * FROM users WHERE username ='".$uname."' AND password='".$passwd."' LIMIT 1";
      $query= mysql_query($query);
      if(mysql_num_rows($query) == 1) {        
        $_SESSION['admin'] = "Welcome to the Admin Panel {$_POST['name']} ";
        $_SESSION['onOff'] = 'on';
       ?>        
     <!DOCTYPE html>
      <html>
      <head>
          <meta charset="utf-8">
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.3.0/pure-min.css">
          <script type="text/javascript" src="//tinymce.cachefly.net/4.0/tinymce.min.js"></script> 
          <script>
          tinymce.init({
              selector: "textarea#postCont",
              plugins: [
                  "advlist autolink lists link image charmap print preview anchor",
                  "searchreplace visualblocks code fullscreen",
                  "insertdatetime media table contextmenu paste"
              ],
              toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
          });
        </script>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript" charset="utf-8" async defer></script>

          <title>Admin Panel</title>     
      </head>
      <body>        
        <div class="pure-g-r" style="letter-spacing:0em; padding-left:2em; padding-right:2em;">   
           <div class="pure-u-5-10">                                
              <form class="pure-form pure-form-stacked" action='../engine/engine.php' method='post'>
                <fieldset>         
                  <legend><?php echo  $_SESSION['admin']; ?></legend>
                  <div style="margin:0 auto;">                      
                      <input type='text' name='postTitle' class="pure-input-1" placeholder='Article Title'/>                                
                  </div>
                  <textarea  class="pure-input-1" name='postDes' name='postDes' cols='102' placeholder='Article Summary'></textarea>                  
                  <textarea  id="postCont" name='postCont' cols='60' rows='10'></textarea>                  
                  <br />
                  <button type="submit" name="blogsubmit" class="pure-button pure-input-1 pure-button-primary">Submit</button>                  
                </fieldset>                
              </form>
          </div>
          <div class="pure-u-1-10">

          </div>
          <div class="pure-u-4-10">                             
                  <h2 style="margin:0 auto;">Posted articles</h2>
                  <dl>
                      <dt>How to create a Text editor</dt>
                      <dd><a href=""><small><a href="">Edit</a></small> | <small><a href=""> Delete</a></small></a>
                      <dt>How to create a Text editor</dt>
                      <dd><a href=""><small><a href="">Edit</a></small> | <small><a href="">Delete</a></small></a>                                 
                  </dl>                                
          </div>                  
        </div>
        <script type="text/javascript">
                function submit() {
                      var xhr = new XMLHttpRequest();
                      try {
                        xhr = new new ActiveXObject("Msxml2.XMLHTTP");
                      }
                      catch (e) {
                        try {
                          xhr = new ActiveXObject("Microsoft.XMLHTTP");
                        }
                        catch (e) {
                          alert(" Please upgrade your broswer");  
                          return false;
                        }
                      }

                     xhr.onreadystatechange = function () {
                          if (xhr.readyState === 4) {
                                if (xhr.status === 200) {
                                     alert(xhr.responseText);                                                 
                                  } else {
                                            alert("dang!!!");
                                            return false;
                                      }
                          } else {
                                alert("dang!!!! 2 buahahaha");
                                return false;
                          } 
                       var title = document.getElementById('postTitle').value;
                       var postDes = document.getElementById('postDes').value;                                        
                       var postCont = document.getElementById('postCont').value;                                           
                       var data = "title=" + title + "&summary=" + postDes + "&content=" + postCont;                                            
                       xhr.open("POST", "../engine/engine.php",true );
                       xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                       xhr.send(data);                    
                     }                      

                }

          </script>  
      </body>
    </html>         

<?php   }
      else {
          echo "username or password is incorrect";
          die(mysql_error());

      }
    }  

?>

它应该将其传递给此代码

                 <?php
include_once('database.php');
if (isset($_POST['blogsubmit'])  ) {
    $title   =  mysql_real_escape_string($_POST['postTitle']);
    $summary =  mysql_real_escape_string($_POST['postDes']);
    $content =  mysql_real_escape_string($_POST['postCont']);
    insertBlogPost($title,$summary,$content);
}

function insertBlogPost($title,$summary,$content) { 
    $query = "INSERT INTO `blogposts` SET
            `title`     = '$title',
            `summary`   = '$summary',               
            `content`   = '$content' ";                                     
    mysql_query($query) or die(mysql_error());
    echo "Yaaaay";      
}
?>

问题是我得到一个打印 Yaaay 部分而不是警报的页面。我已经尝试了我所知道的任何方法来解决问题,所以我非常感谢您的帮助。我也尝试在 POST 的真假之间切换,但没有帮助。

PS:很抱歉使用 mysql_* ,我仍然不习惯使用 PDO。提前谢谢了。

4

2 回答 2

3

您实际上在哪里调用此 AJAX 函数?目前,如果你去掉所有的杂物,你有这个:

<form action='../engine/engine.php' method='post'>
    <button type="submit" name="blogsubmit">Submit</button>
</form>
<script type="text/javascript">
    function submit() {
    }
</script>

您没有使用该功能,只是定义它。表单是提交普通POST请求的普通表单,与AJAX无关。在它提交 POST 请求后,它会显示响应中给出的唯一输出:

echo "Yaaaay";

防止表单提交的一种快速方法是使按钮不再是submit按钮:

<button name="blogsubmit">Submit</button>

然后,您只需将点击处理程序附加到它即可调用您的函数。像这样的东西:

<script type="text/javascript">
    function submit() {
        // the rest of your function
    }

    document.getElementsByName('blogsubmit')[0].onclick = submit;
</script>

还有各种其他方法可以附加单击事件处理程序。

于 2013-10-06T19:03:31.930 回答
1

从我所见,您永远不会将您的提交功能绑定到表单的提交事件。目前,您的函数从未被调用,并且该表单正在向 ../engine/engine.php 页面定期发布帖子,这就是您在新页面上看到其结果的原因。

您应该将以下属性添加到您的表单标签:

onsubmit="return submit()"

并且为了覆盖常规表单帖子,以便您的函数可以完成工作,您还必须从函数中返回 false

于 2013-10-06T19:02:29.123 回答