$_POST only works from html to php but not php to php.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

$_POST only works from html to php but not php to php.

Post by ashebrian »

~pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


When i use the following code in my [color=#FF0000]voucher.html[/color] the [color=#FF0000]$_POST[/color] works when its sent to [color=#FF0000]voucherProcess.php[/color]. But the in voucherProcess.php as the [color=#FF0000]quantity[/color] value selected will not bring u to the correct page. It brings u to the default one.

[color=#FF0000]voucher.html[/color]

Code: Select all

 
<form name='availablevouchers' action='voucherProcess.php' method='post'>
<input name="vouchertype" type="hidden" value="You Choose The Amount" />
<select name='price' id='price'>
<option value='Price'>Choose Amount</option>
<option value='50'>50 EURO</option>
<option value='100'>100 EURO</option>
<option value='150'>150 EURO</option>
<option value='200'>200 EURO</option>
<option value='250'>250 EURO</option>
<option value='300'>300 EURO</option>
<option value='350'>350 EURO</option>
<option value='400'>400 EURO</option>
<option value='450'>450 EURO</option>
<option value='500'>500 EURO</option>
</select>
 
<select name='quantity' id='quantity'>
<option value='0'>None</option>
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
</select>
voucherProcess.php

Code: Select all

<?php
 // alway filter inputs for security, now this must be an integer
 $quantity = intval($_POST['quantity']);
 // now check if it is in the range
 if (($quantity >= 1) && ($quantity <= 6))
 {
    header('Location: voucherLoaded' . $quantity . '.php');
 } else {
    echo header('Location: error.php');
}
?>
However, as i tried to change the html file to a php file the quantity vaule brought me to the correct page. BUT the $_POST did not work.

Changes made in the new php file voucher.php:

Code: Select all

<form name='availablevouchers' action='voucherProcess.php' method='post'>
<input name="vouchertype" type="hidden" id="vouchertype" value="You Choose The Amount" />
****ADDED****
<input type="hidden" name="price" id="price" value="<?php echo intval($_POST['price']); ?>"/>
<input type="hidden" name="quantity" id="quantity" value="<?php echo intval($_POST['quantity']); ?>"/>
<input type="hidden" name="deliverymethod" id="deliverymethod" value="<?php echo intval($_POST['deliverymethod']); ?>"/>
Am stuck on this......any hints?


~pickle | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: $_POST only works from html to php but not php to php.

Post by Christopher »

You might need the full URL in the redirect:

Code: Select all

header('Location: http://www.mysite.com/voucherLoaded' . $quantity . '.php');
(#10850)
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: $_POST only works from html to php but not php to php.

Post by Sekka »

Why are you using $_POST in voucher.php? Does another form submit to this page and it's action is voucher.php?

$_POST contains information from the previous page's form if it's method is set to 'post'. Once the action page has finished executing (a redirect classes as a finish of execution), the $_POST is cleared and will no longer exist.

Any of that help? :(
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

Re: $_POST only works from html to php but not php to php.

Post by ashebrian »

I see... so if a forms info is not posted to voucher.php then there's no need for $_post. I got rid of it. But once i go to the next page the correct page comes up BUT the results for the of the previous form is 0 for all my $_POST. Why is this?
User avatar
thomas777neo
Forum Contributor
Posts: 214
Joined: Mon Mar 10, 2003 6:12 am
Location: Johannesburg,South Africa

Re: $_POST only works from html to php but not php to php.

Post by thomas777neo »

On the voucherProcess.php, type the following:

Code: Select all

<?php
var_dump($_POST);
?>
Then copy the output on this forum so that we can see what is going on. Also send the version of PHP that you are using. You might be using an older version that does not use $_POST but the older command.
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

Re: $_POST only works from html to php but not php to php.

Post by ashebrian »

Output:

Code: Select all

Voucher:    0
Total Amount:   € 0
How Many?   0
Voucher Delivery:   0
 
i'm using:
php 5.2.5
Still having same prob.
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

Re: $_POST only works from html to php but not php to php.

Post by ashebrian »

Actually.....the above output was on my localhost but online the output error is:
array(4) { ["vouchertype"]=> string(21) "You Choose The Amount" ["quantity"]=> string(1) "1" ["price"]=> string(3) "200" ["deliverymethod"]=> string(16) "offline_customer" }
Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\vhosts\voucherProcess.php:2) in C:\Inetpub\vhosts\voucherProcess.php on line 8
Do you understand this?
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

Re: $_POST only works from html to php but not php to php.

Post by ashebrian »

does anyone know where this error comes from? or how to solve it?
User avatar
markusn00b
Forum Contributor
Posts: 298
Joined: Sat Oct 20, 2007 2:16 pm
Location: York, England

Re: $_POST only works from html to php but not php to php.

Post by markusn00b »

it should be

Code: Select all

 
header("Location: ... ");
// not!
echo header("Location: ... ");
 
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

Re: $_POST only works from html to php but not php to php.

Post by ashebrian »

cheers, i changed that but it didnt change the error msg. if i change my form action to one of the pages i wanted loaded (like voucherLoaded1.php) and NOT voucherProcess.php then the $_POST works without a bother. BUT i want to be moved to a page after an option was selected in the first page. My only option is to do what i'm doing at the moment but can't sort this error. I Believe that maybe $_POST will not work once i use voucherProcess.php, and prob will never work. Any way around this?
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

Re: $_POST only works from html to php but not php to php.

Post by ashebrian »

cheers, i solved my prob now. i used a javascript instead of voucherProcess.php. It does the job.
kryles
Forum Contributor
Posts: 114
Joined: Fri Feb 01, 2008 7:52 am

Re: $_POST only works from html to php but not php to php.

Post by kryles »

until users disable JavaScript.
ashebrian
Forum Contributor
Posts: 103
Joined: Sat Feb 02, 2008 8:01 pm

Re: $_POST only works from html to php but not php to php.

Post by ashebrian »

yeah.....everybody knows that. To be honest i wanted to fix the PHP script coz of that but PHP didn't work and i didn't seem to find a way around it. If you know otherwise. Please post it here.

Cheers
Post Reply