-4

Hey i want to redirect to the same page on my if and else statement. How would i do it?

I tried the following code but didnt work:

if($ac == 'yes'){
    $message="Demo sold their product";
    header( "Location: delete/delete.product.php?product=<?php echo $encodeCodeProduct; ?>" ); die;
} else{
    $message="Demo did not sell their product";
    header( "Location: delete/delete.product.php?product=<?php echo $encodeCodeProduct; ?>" ); die;
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 1
    Did now work how? Also, if you redirect to the same page, how does it know that there was a redirect? How is it going to respond differently? Variables like `$ac` and `$message` are deleted at the end of the run, so the redirected page starts with a blank set of variables. You'd have to use a session to remember the selections made, or pass them in the url. – GolezTrol Aug 07 '17 at 12:15
  • 1
    "didn't work" *how*? What happens? In what way does it fail? Is there an error in the PHP logs? Is the redirect response returned to the client? What URL does it specify? What happens when that URL is requested? – David Aug 07 '17 at 12:15
  • Can PHP can evaluate the `` tags in your `header()` call? I hope not. Try stating something like `header("Location: delete/delete.product.php?product=".$encodeCodeProduct);` – Kyrre Aug 07 '17 at 12:18
  • Possible duplicate of [how to redirect to onother page after if condition is satified in php?](https://stackoverflow.com/questions/13231254/how-to-redirect-to-onother-page-after-if-condition-is-satified-in-php) –  Aug 07 '17 at 12:19
  • Possible duplicate of https://stackoverflow.com/a/8782153/8317956 –  Aug 07 '17 at 12:22

2 Answers2

2

Explanation: You can't Append PHP inside the PHP tag. Hence you need to Resolve the Error as below. You have to concatenate the PHP with the Header Location Redirect.

if($ac == "yes"){
    $message="Demo sold their product";
    header( "Location: delete/delete.product.php?product=".$encodeCodeProduct); die;
} else{
    $message="Demo did not sell their product";
    header( "Location: delete/delete.product.php?product=".$encodeCodeProduct); die;
}

Error Would Likely Occur: If you face the Header Already Sent Error you have to clear the outputs that has been sent already with the help of ob_start() before the header Redirection.

Redirection code with ob_start

if($ac == "yes"){
    $message="Demo sold their product";
    ob_start();
    header( "Location: delete/delete.product.php?product=".$encodeCodeProduct); die;
} else{
    $message="Demo did not sell their product";
    ob_start();
    header( "Location: delete/delete.product.php?product=".$encodeCodeProduct); die;
}

More Additional Reference from SO: How to fix "Headers already sent" error in PHP

Naresh Kumar P
  • 4,127
  • 2
  • 16
  • 33
1

You are already inside PHP tags, so in your location string, you can not reopen PHP tags when you want to write $encodeCodeProduct. Just write "...product=$encodeCodeProduct", or, "...product=" . $encodeCodeProduct.

Kévin Bibollet
  • 3,573
  • 1
  • 15
  • 33