4

I have a problem with my PHP script. It should work like this: User fills in details for reservation -> sends details to preservation.php.

If the amount of persons is larger than six, it should prevent the data from entering the database. At the same time is should resend the filled in details to the original form, so the user can edit the details.

But what happens is that the header sends preservation.php back as a download (The content is in HTML, no PHP).

    $username = $_POST['username'];
    $name = $_POST['name'];
    $email = $_POST['email'];
    $tel = $_POST['tel'];
    $date = $_POST['date'];
    $time = $_POST['time'];
    $pers = $_POST['pers']; //Amount of persons
    $comment = $_POST['comment'];

    if($pers >= 7){


            $post_date = 'test=test'; //Just to test the script
            $content_length = strlen($post_data);

            header('POST reservation.php');

            header('Host: localhost');

            header('Connection: close');

            header('Content-type: application/x-www-form-urlencoded');

            header('Content-length: ' . $content_length);

            header('');

            header($post_data);

            header('Location: reservation.php');


    }
thomas479
  • 509
  • 1
  • 6
  • 17
  • 3
    Typo with `$post_data` as `$post_date`. – SquareCat Dec 12 '13 at 14:17
  • Do a `print_r()` on `$_POST` to see the value it's holding. Your `if()` condition looks correct. – ʰᵈˑ Dec 12 '13 at 14:18
  • Not sure how your other code looks like, but you might find it useful and probably a nicer solution to do a `require_once('reservation.php')` in `preservation.php` and having all the other problems solved right away, instead of routing the user around through multiple requests? – SquareCat Dec 12 '13 at 14:19
  • There was indeed a typo. But it was still broken. It works when I use the require_once from SquareCat. Thanks – thomas479 Dec 12 '13 at 14:31
  • @Thomas See [How do you POST to a page using the PHP header() function?](http://stackoverflow.com/questions/653090/how-do-you-post-to-a-page-using-the-php-header-function). What you are trying to do can not be done in the way you are trying to. – user555 Dec 12 '13 at 14:33

2 Answers2

1

First of all, you have a typo in your code:

$post_date = 'test=test';

Should be

$post_data = 'test=test';

Second, i am not entirely sure what is causing your issue because i never needed to post back data from an already posted request.

As i suggested in my comment, i recommend you to instead solve your problem like this:

  1. Send the form to your script
  2. Use your script to determine whether your form validates for submission (comparing the amount of persons, for instance)
  3. If it does not validate, just send the original form page back to the browser right away (by including the reservation.php file, as i mentioned in my other comment, or by any other means that make sense at this point).
SquareCat
  • 5,699
  • 9
  • 41
  • 75
1

The PHP header() function sends HTTP headers back to the user agent (e.g. the web browser). When you do header('Content-type: application/x-www-form-urlencoded'); you're effectively telling the users browser to treat the entire page as application/x-www-form-urlencoded which results in the web browser trying to download the page instead of parsing and displaying it.

You can't do a POST request back into a browser. What you are trying to do can not be done in the way you are trying to.

If you want to preserve form data you can either let the browser go back to the previous page or rebuilt the form using values collected when the form was first submitted by the user.

user555
  • 1,564
  • 2
  • 15
  • 29