0

I wanna make header location like stackoverflow system has. After posting, it will load the page based on lastinsertid. I tried to use php, it works, but I wanna change it using ajax jquery because of its single page. Could I use ajax jquery in this case? how to make it work?

here's the code :

PHP

if($sumn>0 && $sump>=0) {
echo "banned";
 }
else if($sumn==0 && $sump<=3) {
echo "oot";
 }
else{
$insert=$db_con->prepare("INSERT INTO tb_post (id_user,title,description,created_date) VALUES(:id_user,:title,:description,NOW())");
$insert->bindParam(":id_user",$id_user);
$insert->bindParam(":title",$title);
$insert->bindParam(":description",$description);
$insert->execute();
$id_post=$db_con->lastInsertId();
if ($insert->rowCount()>0) {
  $post = $db_con->prepare("SELECT * FROM tb_post WHERE id_post=:id_post");
  $post->execute(array(":id_post"=>$id_post));
  $row=$post->fetch(PDO::FETCH_ASSOC);
  $title =  clean_url($row['title']);
  $detail ="../discuss/".$row['id_post']."/".$title;
  header("location:$detail");
   echo 'success';
}else {
  echo 'fail';
}

ajax jquery

$.ajax({
          url: 'create_process.php?page=send_post',
          type: 'post',
          data: 'title='+title+'&description='+description,
          success: function(msg){
            if(msg=='success') {
                window.location='/'; //This the problem.. need to know how to use header location based on lastinsertid
            }
             else if(msg=='banned') {
                 $("#dis-mes").html('<div class="allert alert-danger">Banned</div>');

            }else if(msg=='oot'){
                 $("#dis-mes").html('<div class="allert alert-danger">OOT</div>');
            }else {
              alert('failed');
            }
          }
        });
Ayuktia
  • 431
  • 3
  • 11
  • 1
    Possible duplicate of [jQuery - get AJAX response headers](http://stackoverflow.com/questions/11440918/jquery-get-ajax-response-headers) – ᴄʀᴏᴢᴇᴛ Jun 21 '16 at 14:46

1 Answers1

1

instead of using a header, you should return the redirect url in the response. the php response should be a json string like this :

{
    status: "success",
    url: "../discuss/".$row['id_post']."/".$title
}

in the javascript just check the content of msg.status and use msg.url in your window.location


If you still want to use headers, you can use this in your success function:

success: function(msg, textStatus, request){
    if (msg == 'success') {
        window.location = request.getResponseHeader('location'));
    }
},
ᴄʀᴏᴢᴇᴛ
  • 2,939
  • 26
  • 44
  • I prefer use header because I don't know about json. I've tried your code, but it gave the broken link like this http://localhost/forumdiskusi/user/null . – Ayuktia Jun 21 '16 at 15:20
  • which version of jquery are you using ? – ᴄʀᴏᴢᴇᴛ Jun 21 '16 at 15:35
  • I just tried with 2.2.4 and if worked fine. i see in your PHP code that there are echos before the header function, you can't modify headers after starting the output. – ᴄʀᴏᴢᴇᴛ Jun 21 '16 at 15:42
  • I've change jquery to that version and put the echo after starting the output. If I used these codes `$post = $db_con->prepare("SELECT * FROM tb_post WHERE id_post=:id_post"); $post->execute(array(":id_post"=>$id_post)); $row=$post->fetch(PDO::FETCH_ASSOC); $title = clean_url($row['title']); $detail ="../discuss/".$row['id_post']."/".$title; header("location:$detail");`. it will fail.. but If I removed them it will success. and now the problem not only the header location of ajax jquery.. anyway I will vote up for you. – Ayuktia Jun 21 '16 at 16:19
  • can you check if the headers are correctly sent with your response ? http://stackoverflow.com/questions/4423061/view-http-headers-in-google-chrome – ᴄʀᴏᴢᴇᴛ Jun 22 '16 at 09:35