繁体   English   中英

提交使用ajax和php并返回响应

[英]Submiting for using ajax and php and return response

我正在使用此php提交表单,并且现在正在工作,我想使用ajax将其发布到它,并返回我提交的内容而无需重新加载页面,但是没有发布到我的数据库,并且它没有显示我的错误。 我可以将其归档为哪种其他简单方法,或者任何人都可以告诉我在哪里做错了这是完整的程序<?php echo $_GET['postid'];?> <这是我的主博客文章的ID HTML STRUCTURE

<form id="add-comment" action="javascript:void(0);" style="font-size: 100%;">
<textarea placeholder="" name="comment" cols="68" rows="3" style="min-height:30px;" id="comment"></textarea>
<?php 
if(!isset($_SESSION['user_login'])) { 
echo "<label>Enter name</label><br>";
echo "<input placeholder='Enter a name' style='width:130px;height: inherit;' required='true' id='anony' type='text' name='name'/>";
}?>
<input tabindex="0" value="Add Comment" class="btnpostq" id="donedbtn" type="submit"/>
<input type="hidden" value="<?php echo $_GET['postid'];?>" name="rid"/>
</form> 

阿贾克斯

<script>
$(document).ready(function(){
    $('#add-comment').submit(function(){

        $('#response').html("<b>Loading response...</b>");
        $.ajax({
            type: 'POST',
            url: 'alter_reply.php', 
        data:'comment='+comment+'&name='+name+'&rid='+rid,
        })
        .done(function(data){

            $('#response').html(data);

        })
        .fail(function() {

            alert( "Posting failed." );

        });
        return false;

    });
});
</script>

alter_reply.php

<?php

if($_POST) {
$db_host = "localhost";
$db_user = "root";
$db_pass = "";
$db_name = "post";

     try {
     $db_conn = new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_pass);
     $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $stmt = $db_conn->prepare("INSERT INTO replys(rid,mesreplys,rtime,rusername) VALUES(:rid,:mesreplys,:rtime,:rusername)");


$stmt->bindParam(':rid', $rid); 
$stmt->bindParam(':mesreplys', $comment);
$stmt->bindParam(':rtime', $timedate); 
$stmt->bindParam(':rusername', $user); 



$form = $_POST;
$rid= $form['rid'];
$comment = $form['comment'];
$timedate = date("Y-m-d H:i:s");
if(isset($_SESSION['user_login'])){
$user = $_SESSION['user_login'];
}else{
$anony_user = $form['name'];
$user = $anony_user;    
}   

    $stmt->execute();

    }
catch(PDOException $e)
    {
    echo "Error:" . $e->getMessage();
    }
$db_conn = null;
}
?> 

您尚未收集变量值。 获取它们,然后传递给$.ajax ,如下所示:

var comment = $('#comment').val();
var name = $('#anony').val();
var rid = $('#rid').val();

确保输入中包含id

您的表格应如下所示:

<form id="add-comment" action="javascript:void(0);" style="font-size: 100%;">
<textarea placeholder="" name="comment" cols="68" rows="3" style="min-height:30px;" id="comment"></textarea>
<?php 
if(!isset($_SESSION['user_login'])) { 
echo "<label>Enter name</label><br>";
echo "<input placeholder='Enter a name' style='width:130px;height: inherit;' required='true' id='anony' type='text' name='name'/>";
}?>
<input tabindex="0" value="Add Comment" class="btnpostq" id="donedbtn" type="submit"/>
<input type="hidden" value="<?php echo $_GET['postid'];?>" id="rid" name="rid"/>
</form> 

并且脚本必须是:

<script>
$(document).ready(function(){
    $('#add-comment').submit(function()
    {
        var comment = $('#comment').val();
        var name = $('#anony').val();
        var rid = $('#rid').val();

        $('#response').html("<b>Loading response...</b>");
        $.ajax({
            type: 'POST',
            url: 'alter_reply.php', 
        data:'comment='+comment+'&name='+name+'&rid='+rid,
        })
        .done(function(data){

            $('#response').html(data);

        })
        .fail(function() {

            alert( "Posting failed." );

        });
        return false;

    });
});
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM