简体   繁体   中英

ajax request didn't retrieve value of textarea after posting data

i coded a comment system using jquery,but the problem is that the ajax request didn't show the posted data without loading page,although the data is added successfully in db

how can i show the posted data after clicking the submit button

js:

$(document).ready(function(){
$("#submit").click(function() {

    var ans_body = encodeURIComponent($("#ans_body").val());
    var q_id = $("#q_id").val();
    var ans_auth='vista-design';
    var dt='2-2-2012';
    var ans_rate='0';
    var ans_correct='no';
    var dataString = 'ans_body=' + ans_body + '&q_id=' + q_id + '&ans_auth=' + ans_auth + '&dt=' + dt + '&ans_rate=' + ans_rate + '&ans_correct=' + ans_correct;

    if(ans_body=='')
     {
    alert('برجاء كتابة الاجابة صحيحة');
     }
    else
    {
    $("#flash").show();
    $("#flash").fadeIn(400).html('<img src="js/loader.gif" align="absmiddle">&nbsp;<span class="loading">Loading answers...</span>');
$.ajax({
  type: "POST",
  url: "includes/submit_comment.php",
  contentType: "application/x-www-form-urlencoded;charset=utf-8",
   data: dataString,

  cache: false,
  success: function(html){

  $("ol#update").append(html);
  $("ol#update li:last").fadeIn("slow");
    $('#ans_body').val(ans_body);
    $("#ans_body").focus();

  $("#flash").hide();

  }
 });
}
return false;
    });
    })

php:

<?php
include_once('../../includes/config.php');
include_once('../includes/bbParser.php');
if($_POST){

    @$q_id=$db->real_escape_string($_POST['q_id']);
    $parser=new bbParser();
    @$ans_body=$parser->getHtml(htmlentities($_POST['ans_body']));
    $ans_auth="vista-design";//$_SESSION['username']
    $dt=date("h:i:s d-m-Y");
    $ans_rate=0;
    $ans_correct="no";  
    if(!empty($ans_body) ){

    $prep_answer=$db->prepare("insert into stack_ans(q_id,ans_body,ans_auth,ans_dt,ans_rate,ans_correct) values(?,?,?,?,?,?)"); 
    $bind_answer=$prep_answer->bind_param('ssssss',$q_id,$ans_body,$ans_auth,$dt,$ans_rate,$ans_correct);
    $prep_answer->execute();

    }//end empty


}


?>

html+php code to show comments:

    <ol  id="update" class="timeline">
       <?php
    while($ans_row=$get_ans->fetch_assoc()){
        $ans_body=$ans_row['ans_body'];
        $ans_auth=$ans_row['ans_auth'];
        $ans_dt=$ans_row['ans_dt'];
        $ans_rate=$ans_row['ans_rate'];
        $ans_correct=$ans_row['ans_correct'];
        $userid=$ans_row['userid'];
        $ans_id=$ans_row['ans_id'];
        $profile_pic=$ans_row['profile_pic'];

    ?>

    <li class="box">
    <div id="eachans_block">
        <div id="ans_corate">
    <p id="subans_rate">

    <a href="#" class="like_ans_btn" id="<?php echo $ans_id; ?>">
    <span class="on_img" id="avoid_font" > <?php echo $ans_rate; ?> </span> 
    </a>


    </p>
    <hr />
    <p id="subans_correct">
    <?php 
    $quest_orig_id=$ans_row['q_id'];
    $yes_status="hash".md5("yes");
    $no_status="hash".md5("no");

    //if click on non-correct answer->>make it correct
    if(isset($_GET['status']) && isset($_GET['ans_id']) && $_GET['status']==$yes_status){
    $get_ans_id=htmlspecialchars($_GET['ans_id']);
    //check if there is answer with correct one
    $get_topic_answers=$db->query("select * from stack_ans where q_id='$q_id' and ans_correct='yes'");
    $num_topic_answers=$get_topic_answers->num_rows;
    if($num_topic_answers>0){
    $db->query("update stack_ans set ans_correct='no' where q_id='$q_id'");
    $db->query("update stack_ans set ans_correct='yes' where ans_id='$get_ans_id'");
        }else{

    $db->query("update stack_ans set ans_correct='yes' where ans_id='$get_ans_id'");
        }

       header("Location:questions.php?id=".$q_id." ");

        }//end if isset

  //if click on correct answer->make it non-correct
    if(isset($_GET['status']) && isset($_GET['ans_id']) && $_GET['status']==$no_status){
    $get_ans_id=htmlspecialchars($_GET['ans_id']);
    $db->query("update stack_ans set ans_correct='no' where ans_id='$get_ans_id' ");
    header("Location:questions.php?id=".$q_id." "); 
    }//end if isset




    $get_quest_auth=$db->query("select * from stack_ask where id='$q_id'");
    $fetch_quest_auth=$get_quest_auth->fetch_assoc();
    $quest_author=$fetch_quest_auth['q_auth']; //vista-design
    $sess_username="vista-design";//$_SESSION['username']
    if($sess_username==$quest_author){
        if($ans_correct=='no'){
            ?>
            <a href="questions.php?id=<?php echo $q_id; ?>&ans_id=<?php echo $ans_id; ?>&status=hash<?php echo md5('yes'); ?>"> <i id="false_ans" class="fa fa-check fa-4x"></i></a>
            <?php
            }
        if($ans_correct=='yes'){
            ?>
            <a href="questions.php?id=<?php echo $q_id; ?>&ans_id=<?php echo $ans_id; ?>&status=hash<?php echo md5('no'); ?>">
            <i id="true_ans" class="fa fa-check fa-4x"></i>
            </a>
            <?php
            }   



        }//end if sess



    ?>

    </p>

    </div>

    <div id="ltansblock">
    <p id="subans_body"><?php echo stripslashes($ans_body); ?></p>

    <div id="ans_authblock">
        <p id="img_side">
        <?php $img=$profile_pic;?>
        <img width="60" height="60"  src="../hire/profile_pic/<?php echo $img; ?>" />
        </p>
    <p id="subans_auth">
    <a href="profile.php?uid=<?php echo $userid; ?>"><?php echo $ans_auth; ?></a>
    </p>
    <p id="subans_dt"><?php echo $ans_dt; ?></p>


    </div>
    </div>

    </div>
    </li>

    <?php   
        }
    ?>
     </ol>  

        <div id="flash" align="right"></div>
<div id="addCommentContainer">
        <form method="post" action="" id="postprojectans" >
        <input type="hidden" name="q_id" id="q_id" value="<?php echo  htmlspecialchars($_GET['id']); ?>"/>

 <textarea  name="ans_body"  id="ans_body"   rows="10" cols="60"></textarea>

   <fieldset id="actions">
        <input type="submit" class="button" name="sub_ans" id="submit" value="اضافة اجابتك">
    </fieldset>

</form>

        </div>

        </div>

You are appending the HTML you get back from submitting the comment:

success: function(html){ 
  $("ol#update").append(html);

… but the script you submit the comment to doesn't return any HTML, so you are appending a blank document.

Change that PHP program so it formats the newly acquired data into HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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