0

基本的に、テキストエリアの値を非同期で挿入する必要がありますが、関数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>

 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')");
}
4

3 に答える 3

0

PHPファイルが起動する前にロードする必要があります。
コントローラーを作成し、index.php の代わりに任意の場所で使用し、(DB からこれらのものをロードした後) ユーザーを index.php ページにリダイレクトして、必要なデータを提供するだけです。

于 2012-07-09T04:44:52.577 に答える
0

したがって、ajax呼び出しと同じページを呼び出しています..明らかにコードを返します。

別のファイルにphpコードを書き、それを呼び出してみてください。できます

于 2012-07-09T09:43:33.360 に答える
0

以下に示すように、条件文にコードを記述します。

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

        }
?>
于 2012-07-09T09:55:21.533 に答える