douhuang3740 2016-03-24 01:09
浏览 41

异步评论 - 使用ajax在php上

I've included the code that I'm working with. I'm trying to make comments appear asynchronously on my website that runs on php with ajax. Currently, I have a database that takes in the user input (which is what insert.php handles). I'm able to input the user input into my database and also print it out, however it does not print out the comment on the index.php site without reloading. The goal is to have it print out the user input automatically, without reloading the site. Right now what happens is, once I press the submit button, it takes me to a white page and once I open up the index.php page again, the comment shows up. Something is going wrong or not linking properly, not entirely sure what.

index.php

<html>
<head>

    <link href="csair.css" rel="stylesheet" type="text/css" media="all" />
    <script   src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

    <script>
        $(function() {
            $('#submitButton').click(function() {
                $.ajax({
                    type: "GET",
                    url: "insert.php",
                    data: {name: $('#userInput').val()},
                    , beforeSend: function(){
                }
                , complete: function(){
                }
                , success: function(html){
                    $("comment_part").html(html);
                }
            });
          });
       });
    </script>

</head>
<body>

<HR SIZE="6">

<form id="comment_form" action="insert.php" method="GET">
    Comments:
    <input type="text" class="text_cmt" name="field1_name" id="userInput"/>
    <input type="submit" name="submit" value="submit" id = "submitButton"/>
    <input type='hidden' name='parent_id' id='parent_id' value='0'/>
</form>

<!--connects to database and queries to print out on site-->
<div id='comment_part'>
<?php
    $link = mysqli_connect('localhost', 'x', '', 'comment_schema');
    $query="SELECT COMMENTS FROM csAirComment";
    $results = mysqli_query($link,$query);

    while ($row = mysqli_fetch_assoc($results)) {
        echo '<div class="comment" >';
        $output= $row["COMMENTS"];
        //protects against cross site scripting
        echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8');
        echo '</div>';
    }
?>
</div>

</body>
</html>

insert.php

$userInput= $_GET["field1_name"];
if(!empty($userInput)) {
    $field1_name = mysqli_real_escape_string($link, $userInput);
    $field1_name_array = explode(" ",$field1_name);

    foreach($field1_name_array as $element){
        $query = "SELECT replaceWord FROM changeWord WHERE badWord = '" . $element . "' ";
        $query_link = mysqli_query($link,$query);
        if(mysqli_num_rows($query_link)>0){
            $row = mysqli_fetch_assoc($query_link);
            $goodWord = $row['replaceWord'];
            $element= $goodWord;
        }
        $newComment = $newComment." ".$element;
    }

    //Escape user inputs for security
    $sql = "INSERT INTO csAirComment (COMMENTS) VALUES ('$newComment')";
    $result = mysqli_query($link, $sql);
    //attempt insert query execution

    //header("Location:index.php");
    die();

    mysqli_close($link);
}
else{
    die('comment is not set or not containing valid value');
}


$link = mysqli_connect('localhost', 'x', '', 'comment_schema');
$query="SELECT COMMENTS FROM csAirComment";
$results = mysqli_query($link,$query);

while ($row = mysqli_fetch_assoc($results)) {
    echo '<div class="comment" >';
    $output= $row["COMMENTS"];
    //protects against cross site scripting
    echo htmlspecialchars($output ,ENT_QUOTES,'UTF-8');
    echo '</div>';
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法