0

I want to redirect to another page after inserting values in the database, but it is not redirecting. It remains on the same page. No error/warning is shown.

My code:

<?php
if (isset($_POST['btn-submit'])) {
    $asth = $user->runQuery("SELECT MAX(Sr) as Sr FROM answer");
    $asth->execute();
    $aresult = $asth->fetch(PDO::FETCH_ASSOC);
    $atf = ($aresult['Sr']);
    $aI = "1";
    $acid = $atf + $aI;
    $arn = $fddate.$ydate.$mdate.$frandno.$acid;
    $count = $i;
    for ($j = 1; $j <= $count; $j++) { 
        $ufname = trim($_POST['radio'.$j]);
        $Qrn = trim($_POST['qid'.$j]); 
        $istmt = $user->runQuery("INSERT INTO answer(ARN,Ans,QRN,SRN) VALUES(:user_arn, :user_crn, :user_qrn, :user_srn)");
        $istmt->bindparam(":user_qrn",$Qrn);
        $istmt->bindparam(":user_crn",$ufname);
        $istmt->bindparam(":user_arn",$arn);
        $istmt->bindparam(":user_srn",$srn);
        $istmt->execute();  
    }

    return $istmt;

    $locationei = "result.php";
    $locatione = $web.$locationei;
    $user->redirect($locatione);
}
?>

In the starting of page:

require_once 'class.user.php';
$user = new USER();

In class.user.php:

public function redirect($url) {
    header("Location: $url");
}
Antti29
  • 2,953
  • 12
  • 34
  • 36

1 Answers1

1

Your problem might lie in here:

return $istmt;

$locationei = "result.php";
$locatione = $web.$locationei;
$user->redirect($locatione);

Once you return a value, function stops execution and skips the rest of the code...

Also, you should ALWAYS exit() after redirection, as the redirection doesnt stop script execution and has been a source of problems in the past.

public function redirect($url)
{
   header("Location: $url");
   exit();
}
vicbyte
  • 3,690
  • 1
  • 11
  • 20
  • So should I remove `return` and try? Or something else should i expect? – Akshay Mandale Oct 29 '17 at 08:32
  • What are you trying to do with that return anyways? – vicbyte Oct 29 '17 at 08:33
  • Nothing to do with return – Akshay Mandale Oct 29 '17 at 08:34
  • Then just remove it. You dont need to return anything, especially since you are redirecting. – vicbyte Oct 29 '17 at 08:35
  • After coding as described I got warning and it doesn't redirects – Akshay Mandale Oct 29 '17 at 08:35
  • `Warning: Cannot modify header information - headers already sent by (output started at C:\wamp64\www\quiz\quiz.php:184) in C:\wamp64\www\quiz\class.user.php on line 399` is the warning – Akshay Mandale Oct 29 '17 at 08:36
  • You are already outputting some text or other informations on the page. You cant do that if you redirect. It does point you to the right lines tho :) – vicbyte Oct 29 '17 at 08:36
  • Yes, some things are displayed using `PHP`, but I need redirect after submitting – Akshay Mandale Oct 29 '17 at 08:38
  • 2
    Lemme repeat myself, you CANT output if you plan to redirect. Whats the point of outputting anyways if the user cant see it (he's getting redirected from that page). – vicbyte Oct 29 '17 at 08:39
  • Should I provide you a code which will help you for better understanding? – Akshay Mandale Oct 29 '17 at 08:41
  • 1
    Theres nothing to understand really. If you try to ouput text before you redirect, theres no point. Redirect takes you to another page, which means anything you would ouput to that point would be erased anyways. – vicbyte Oct 29 '17 at 08:42
  • I am sure you will able to understand why I have some text and want redirect at submit – Akshay Mandale Oct 29 '17 at 08:46
  • 1
    I'm looking at it and you still fail to understand that redirect erases the text you previously outputted. Meaning none of the text you put BEFORE redirect will be visible on the new page. If you want to pass some output onto the page you are redirecting you need to pass it via variables and output on the new page. – vicbyte Oct 29 '17 at 08:47
  • So what should I code that goes to another page after submitting, It's a quiz script which displays question from database... – Akshay Mandale Oct 29 '17 at 08:49
  • So how to do for my script? – Akshay Mandale Oct 29 '17 at 08:51
  • https://stackoverflow.com/questions/9321071/php-pass-data-with-redirect This should answer your questions. – vicbyte Oct 29 '17 at 08:58
  • So, Where should I paste: `$_SESSION['message'] = "Post successfully posted.";` after query is done or some where else? – Akshay Mandale Oct 29 '17 at 09:04
  • You can store whatever output you might want to show on the new page in it. Thats not the top-of-the-line idea, but since youre learning this will be much simpler than alternate solutions. – vicbyte Oct 29 '17 at 09:05
  • But where to code it? At the end of PHP or any other line? – Akshay Mandale Oct 29 '17 at 09:06
  • `{ //if(isset($_POST['btn-submit'])) { $tufname = trim($_POST['tradio'.$tj]); $tQrn = trim($_POST['tqid'.$tj]); $tistmt = $user->runQuery("INSERT INTO answer(ARN,Ans,QRN,SRN) VALUES(:user_arn, :user_tcrn, :user_tqrn, :user_srn)"); $tistmt->bindparam(":user_tqrn",$tQrn); $tistmt->bindparam(":user_tcrn",$tufname); $tistmt->bindparam(":user_arn",$arn); $tistmt->bindparam(":user_srn",$srn); $tistmt->execute(); } $_SESSION['message'] = "Post successfully posted."; } } } } ?>` – Akshay Mandale Oct 29 '17 at 09:09
  • Its not. You should definetly stop whatever you are trying to do and learn to separate html from php. This will help you greatly in what you are trying to achieve and help you understand couple of things along the way. – vicbyte Oct 29 '17 at 09:41