TheBlackBenzKid
TheBlackBenzKid

Reputation: 27087

Can I POST data in PHP and then use the Header command?

$bEmail=mysql_real_escape_string($_POST['bEmail']);
$bPassword=mysql_real_escape_string($_POST['bPassword']);
/webapp/wcs/stores/servlet/Do/LogonForm
header("Location: /cart/payment");

I want to POST the form /webapp/wcs/stores/servlet/Do/LogonForm in jQuery you can do:

$.post('/webapp/wcs/stores/servlet/Do/LogonForm', {
email: <?=$bEmail?>,
password: <?=$bPassword?>
},
window.location="cart/payment"

I want to do this in PHP, so PHP runs a script, this page sends a POST back and whilst the script is running at the end it uses the Header to redirect.

Upvotes: 0

Views: 146

Answers (2)

Samuel
Samuel

Reputation: 17171

You can call the header function whenever you would like as long as no output has been sent to the browser (i.e no HTML source or echo's can come before the header call).

Upvotes: 0

TigerTiger
TigerTiger

Reputation: 10806

Yes you can ....

$bEmail=mysql_real_escape_string($_POST['bEmail']);
$bPassword=mysql_real_escape_string($_POST['bPassword']);

//set POST variables
$url = 'http://domain.com//webapp/wcs/stores/servlet/Do/LogonForm';
$fields = array(                
            'bEmail'=>urlencode($bEmail),
            'bPassword'=>urlencode($bPassword)
        );

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

and then redirect

header("Location: /cart/payment");
exit();  

Upvotes: 2

Related Questions