简体   繁体   中英

PHP Form Not Redirecting to Paypal

I'm trying to add paypal payment method to my form but when I click submit button, page is not redirecting to paypal form, but giving a 404 error on the same page, my code to paypal is

<?php
session_start();
include('paypal.class.php');
if(isset($_REQUEST['trainButton'])) {

$pakage_name    =   $_REQUEST['trainTitle'];
$price          =   str_replace("£","",$_REQUEST['trainingFee']);

$p = new paypal_class();             
$p->paypal_url = 'https://www.paypal.com/cgi-bin/webscr';
$rand   =   rand(1209,45210);
$this_script = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
$success_script = 'http://www.icifb.org/?page_id=1385&uid='.$rand;
$cancel_script  = 'http://www.icifb.org/?page_id=1373&uid='.$rand;

$p->add_field('business', 'payments@icifb.org');
$p->add_field('return', $success_script);
$p->add_field('currency_code','GBP');
$p->add_field('cancel_return', $cancel_script);
$p->add_field('notify_url', $this_script.'?action=ipn');
$p->add_field('item_name', "Training Title: ".$pakage_name);
$p->add_field('amount', $price);

//$p->dump_fields();
$p->submit_paypal_post();

$_SESSION['trainSemail'] =  $_REQUEST['email'];
}
?>
   // function is here
  function submit_paypal_post() {

  echo "<body onLoad=\"document.forms['paypal_form'].submit();\">\n";
  echo "<form method=\"post\" name=\"paypal_form\" ";
  echo "action=\"".$this->paypal_url."\">\n";

  foreach ($this->fields as $name => $value) {
     echo "<input type=\"hidden\" name=\"$name\" value=\"$value\"/>\n";
  }   
  }

You're using a clunky system to do your redirect. You're setting up a form and then trying to use JS to force a submit and that is your redirect. You should use something more precise like header

header('Location: ' $this->paypal_url);
exit; // terminate the script

If that's not feasible then you should use a direct JS redirect

echo '<body onLoad="location=\'' . $this->paypal_url . '\'">';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM