使用jquery将数据发布到php文件并获得回复

时间:2014-06-24 06:11:04

标签: php jquery ajax

在这里,我向php文件发布了三个值,并在div中使用类show显示回复。

它既不显示来自php的“hi或success”消息,也不会将数据插入表中。 我错过了什么吗?

    $( "#submit" ).click(function() {
            var cat = $("#cats option:selected").html();
//          alert(test);
            var post = document.getElementById("post").value;
            var tag = document.getElementById("tags").value;
            dataInsert(cat,post,tag);
            });
    function dataInsert(cat,post,tag)
    {
        $.ajax({
              url       : "koove_insertpost_db.php",
              type      : "POST",
              data      : {"cat" : cat, "post" : post, "tag" : tag},              
              success   : function(data) {
                 $(".show").html(data);
              }
          });       
    }

PHP:

echo "hi";
$con = mysqli_connect('127.0.0.1', 'root', '', 'mysql');                
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    return;
}
$post = mysqli_real_escape_string($con,$_POST['post']);
$category = mysqli_real_escape_string($con,$_POST['cat']);
$tag = mysqli_real_escape_string($con,$_POST[`tag`]);
insertData($category, $post, $tag); 
function insertData($category, $post, $tag){
            $con = mysqli_connect('127.0.0.1', 'root', '', 'mysql');                
            if (mysqli_connect_errno())
            {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
                return;
            }
            $date = date("Y-m-d H:i:s");
            $hash = md5($post);
            $insertQuery1 = "INSERT INTO koove_insert(`hash`, `post`,`tag`,`cat`) VALUES ('".$hash."','".$post."','".$tag."','".$category."')";

            if (!mysqli_query($con,$insertQuery1))
                {
        //          die('Error: ' . mysqli_error($con));
                    //echo "This url already inserted ...";
                    return;
                }
}

1 个答案:

答案 0 :(得分:0)

添加回音“SucessFully Added”;函数“insert Data”之后的语句,相同的消息将显示在HTML DOM中。