NerdsRaisedHand
NerdsRaisedHand

Reputation: 331

button redirect using php

I have this simple code that I was reviewing to figure out what is making nothing happen. Before you tell me to just use onclick=""; or to put an action in the form, I need you to know that I understand that I can do it those ways. But that is not my goal. My goal is to figure out why this way is not working. So please keep those kinds of comment to yourselves! That being said here is the code in question:

<form method="POST" action=""><button type="submit" id="submit" name="submit">CheckOut</button></form>
<?php
    if(isset($_POST['submit'])){
        $result = true;
        if($result){
            header("Refresh: 2; url='checkout.php'");
        }
    }
?>

Now when the button is clicked, nothing happens. Is there a simple reason when this code doesn't execute how I think it should be? (That is redirect the user to the checkout.php page?)

Upvotes: 0

Views: 1096

Answers (4)

m4gix1
m4gix1

Reputation: 336

Fixed: I believe you have a typo when closing php tag, ">?" not "?>"

Edit:

The original code worked fine for me, you can also try:

<?php
if(isset($_POST['submit'])){
    header("Location: checkout.php");      
}
?>
<form method="POST" action=""><input type="submit" id="submit" value="CheckOut" name="submit"></form>

If it makes any difference...

Upvotes: 1

Yisus
Yisus

Reputation: 425

There can be no output before the header(), so the HTML code you have placed before the header() is causing it not to work. php manual: header

Upvotes: 0

Krish R
Krish R

Reputation: 22711

Try using input type as below. And also PHP tag not closed properly.

     <input type="submit"  name="submit"  value="Submit" >

Upvotes: 1

Replace

header("Refresh: 2; url='checkout.php'");

with

header("location:checkout.php");
exit;

Upvotes: 0

Related Questions