Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.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 使用javascript和ajax插入mysql数据_Php_Javascript_Mysql_Ajax - Fatal编程技术网

Php 使用javascript和ajax插入mysql数据

Php 使用javascript和ajax插入mysql数据,php,javascript,mysql,ajax,Php,Javascript,Mysql,Ajax,基本上,我需要异步插入textarea的值,但是当我调用函数insertSQLData()时,它会显示页面的源代码,此外,我找不到其他错误。我省略了数据库代码和任何不相关的代码 <?php $q = $_GET["q"]; $username = $_COOKIE["user"]; ?> function insertSQLData(str){ if(str == 0){ document.getElementById("

基本上,我需要异步插入textarea的值,但是当我调用函数insertSQLData()时,它会显示页面的源代码,此外,我找不到其他错误。我省略了数据库代码和任何不相关的代码

 <?php 
     $q = $_GET["q"];
     $username = $_COOKIE["user"];
 ?>    
  function insertSQLData(str){
    if(str == 0){
        document.getElementById("holder").innerHTML="";
        return;
    }

    if(window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    } else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function(){
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
            document.getElementById("holder").innerHTML = xmlhttp.responseText;
        }
    }
xmlhttp.open("GET", "index-async.php?q=+str", true);
xmlhttp.send();
}

<form action="" method="get">
<textarea onblur="insertSQLData(this.value);" id="quick-post-form" name="quick-post-form"></textarea>
<input type="button" value="Submit" name="quick-post-btn" id="quick-post-submit">
<div id="holder"></div>

您必须在php文件启动之前加载它。

您所要做的就是编写一个控制器,并在任何地方使用它,而不是在index.php中使用它,(从DB加载这些内容后)将用户重定向到index.php页面,提供您需要的数据。

因此,您调用的是ajax调用所在的同一页面。。 显然,它返回代码

在另一个文件中编写php代码并尝试调用它。
它可以工作

您可以按照下面给出的条件语句编写代码

<?php
        if(isset($_GET["q"]))
        {
             $q = $_GET["q"];
             $username = $_COOKIE["user"];


             if(isset($username) && !empty($q)){
                mysql_query("INSERT INTO comments (comment,username) VALUES ('$q', '$username')");
            } elseif(!empty($q)) {
                mysql_query("INSERT INTO comments (comment,username) VALUES ('$q', 'Guest User')");
            }
        }
        else
        {
        ?>
        <script>
         function insertSQLData(str){
            if(str == 0){
                document.getElementById("holder").innerHTML="";
                return;
            }

            if(window.XMLHttpRequest){
                xmlhttp = new XMLHttpRequest();
            } else {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function(){
                if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                    document.getElementById("holder").innerHTML = xmlhttp.responseText;
                }
            }
        xmlhttp.open("GET", "index-async.php?q=+str", true);
        xmlhttp.send();
        }
        </script>
        <form action="" method="get">
    <textarea onblur="insertSQLData(this.value);" id="quick-post-form" name="quick-post-form"></textarea>
    <input type="button" value="Submit" name="quick-post-btn" id="quick-post-submit">
    <div id="holder"></div>
        <?php

        }
?>

它显示了页面的源代码no phpIm我不确定我是否理解你在说什么我想你必须看看
<?php
        if(isset($_GET["q"]))
        {
             $q = $_GET["q"];
             $username = $_COOKIE["user"];


             if(isset($username) && !empty($q)){
                mysql_query("INSERT INTO comments (comment,username) VALUES ('$q', '$username')");
            } elseif(!empty($q)) {
                mysql_query("INSERT INTO comments (comment,username) VALUES ('$q', 'Guest User')");
            }
        }
        else
        {
        ?>
        <script>
         function insertSQLData(str){
            if(str == 0){
                document.getElementById("holder").innerHTML="";
                return;
            }

            if(window.XMLHttpRequest){
                xmlhttp = new XMLHttpRequest();
            } else {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function(){
                if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
                    document.getElementById("holder").innerHTML = xmlhttp.responseText;
                }
            }
        xmlhttp.open("GET", "index-async.php?q=+str", true);
        xmlhttp.send();
        }
        </script>
        <form action="" method="get">
    <textarea onblur="insertSQLData(this.value);" id="quick-post-form" name="quick-post-form"></textarea>
    <input type="button" value="Submit" name="quick-post-btn" id="quick-post-submit">
    <div id="holder"></div>
        <?php

        }
?>