1

I am trying to make my page redirect to $PaymentPage when the Confirm button is clicked. For some reason when I click it nothing happens at all.

I have made the form method post, and I have checked that if submitted, direct to $PaymentPage.

       <form method="post"></form> 
        <div class="col-sm-offset-2 col-sm-4">
            <button type="submit" class="btn btn-default">Confirm</button>
        </div>
        </form>

        <?php
        if ( isset( $_POST['submit'] ) ) {
            header("Location: $PaymentPage", false);
        }
        ?>
bandoy123
  • 253
  • 4
  • 12
  • Because `header()` doesn't work when there is output already. If you had put on error reporting you would've noticed. – Daan Oct 29 '15 at 14:08
  • So how would I fix this? I tried making the form action to direct to the page but it didn't work. What output is there already? – bandoy123 Oct 29 '15 at 14:09
  • there are a couple of things. Is the form submitting actually? Coz you have already closed the form tag. and the button is outside form element. Its a typo. Another point Even if the form submits, you are missing the name attribute for your button so it will never enter your condition – Nouphal.M Oct 29 '15 at 14:09
  • Put the php above you form maybe? – Daan Oct 29 '15 at 14:09
  • I'm getting an Cannot modify header information - headers already sent by (output started at /home/ubuntu/workspace/FinaliseOrder.php:159) in /home/ubuntu/workspace/FinaliseOrder.php on line 181 Call Stack: 0.0001 235656 1. {main}() /home/ubuntu/workspace/FinaliseOrder.php:0 0.2410 297360 2. header() /home/ubuntu/workspace/FinaliseOrder.php:181 – bandoy123 Oct 29 '15 at 14:16

3 Answers3

4

There are a few problems here. Firstly, you don't have a name attribute for your submit button to match the conditional statement, so that will never happen.

Plus, you're outputting before header with the HTML on top of your PHP.

Then, the extra </form> tag after <form method="post">; that needs to be removed.

Therefore, you need to modify your code to read as:

<?php
if ( isset( $_POST['submit'] ) ) {
    header("Location: $PaymentPage", false);
    exit; // added to stop execution if more code is below it
}
else{
   echo "It is not set.";
  }
?>

<form method="post">
<div class="col-sm-offset-2 col-sm-4">
    <button name="submit" type="submit" class="btn btn-default">Confirm</button>
</div>
</form>

Error reporting would have thrown you an undefined index submit notice.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

N.B.:

Now, $PaymentPage is undefined, so I have no idea where you've defined it or what that value should be.

Sidenote: Forms default to "self" when an action to another file isn't defined.


References:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

It should give you an error you may enable error_reporting, you need the header before any output so move it to the top of the file.

Also you close the form and this is the cause that your button does nothing

<form method="post"></form> 
Community
  • 1
  • 1
jmattheis
  • 10,494
  • 11
  • 46
  • 58
  • Even better: stop mixing view content (like html markup) and controller code (like logic) in a single file. – arkascha Oct 29 '15 at 14:11
  • that isnt the entire problem here and the header will never happen. had they changed it to `if(!isset( $_POST['submit'] ) ) {` **then** it would. Notice the logic here? – Funk Forty Niner Oct 29 '15 at 14:11
-3

You closed your form element too early on some other nesting problems, also append variables to strings like that:

"String". $variable ." more string";

HTML:

<form method="post">
    <div class="col-sm-offset-2 col-sm-4">
        <input type="submit" name="button" class="btn btn-default" value="Confirm">
    </div>
    </form>

PHP:

    <?php
    $page = "http://www.google.com/";
    if ( isset( $_POST['button'] ) ) {
        header("Location: ". $page,false);
    }
    ?>
Marcin
  • 1,488
  • 1
  • 13
  • 27