PHP标头不重定向而是保持在同一页面上

时间:2015-03-28 21:03:15

标签: php header

我有一个电子商务页面,我正在使用条带API。付款成功后,我想在更新数据库后重定向到checkoutproc.php。但点击提交后,页面仍保留在同一页面上。当我在我的本地主机上尝试这个时,它工作得非常好但是在它的实时工作时不起作用。标题位于checkout.php文件的底部 -

<html>
<head>
  <title>UNSQ</title>
  <link rel="stylesheet" type="text/css" href="css/styling.css">
  <script type="text/javascript" src="https://js.stripe.com/v2/"></script>
  <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
</head>
<body>

  <div class="logo" >
    <img src=css/images/unsquaringlogo.png height="180" width="700">
  </div>

  <div class="container">
    <h2>Register</h2>

    <form  action="" method="POST" id="payment-form">

      <span class="payment-errors"></span>

        <div class="form-row">
          <label>
            <span>Card Number</span>
            <input type="text" size="20" data-stripe="number"/>
          </label>
        </div>

        <div class="form-row">
          <label>
            <span>CVC</span>
            <input type="text" size="4" data-stripe="cvc"/>
          </label>
        </div>

        <div class="form-row">
          <label>
            <span>Expiration (MM/YYYY)</span>
            <input type="text" size="2" data-stripe="exp-month"/>
          </label>
          <span> / </span>
          <input type="text" size="4" data-stripe="exp-year"/>
        </div>

        <button type="submit" >Submit Payment</button>
      </form> 
  </div>


<script type="text/javascript">
  // This identifies your website in the createToken call below
  Stripe.setPublishableKey('mykey');
  // ...
  jQuery(function($) {
    $('#payment-form').submit(function(event) {
      console.log("this is 1");
      var $form = $(this);
      console.log($form);
      // Disable the submit button to prevent repeated clicks
      $form.find('button').prop('disabled', true);
      console.log("this is 2");
      Stripe.card.createToken($form, stripeResponseHandler);
      console.log("this is 3");
      // Prevent the form from submitting with the default action
      return false;
    });
  });
</script>

<script>

    function stripeResponseHandler(status, response) {

      console.log("this is 5");
      var $form = $('#payment-form');

      if (response.error) {
        // Show the errors on the form
        $form.find('.payment-errors').text(response.error.message);
        $form.find('button').prop('disabled', false);
      } else {
        // response contains id and card, which contains additional card details
        $form.find('.payment-errors').text("success");
        var token = response.id;
        // Insert the token into the form so it gets submitted to the server
        $form.append($('<input type="hidden" name="stripeToken" />').val(token));
        // and submit
        $form.get(0).submit();


      }
    };

</script>

<?php 
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $errors = array();
    if (isset($_POST['stripeToken'])) {

      $token = $_POST['stripeToken'];

      require_once('stripe-php/init.php');
      \Stripe\Stripe::setApiKey("mykey");

      // Get the credit card details submitted by the form
      $token = $_POST['stripeToken'];

      // Create the charge on Stripe's servers - this will charge the user's card
      try {
        $charge = \Stripe\Charge::create(array(
          "amount" => 1000, // amount in cents, again
          "currency" => "usd",
          "source" => $token,
          "description" => "payinguser@example.com")
        );

        include('config.php');
        $sql="insert into login (email,name,password)values('$_GET[email]','$_GET[username]','$_GET[password]')";
        $result=mysqli_query($conn,$sql) or die(mysqli_error());

// ***************THIS IS HEADER WHERe I REDIRECT*******
            header('Location:checkoutproc.php');

      } catch(\Stripe\Error\Card $e) {
          // The card has been declined
      }
    }
} else {
  $errors['token'] = 'The order cannot be processed. You have not been charged. Please confirm that you have JavaScript enabled and try again.';
}

?>
</body>


</html>

这就是*checkoutproc.php*看起来

的方式
<?php
            print "<h1>you have registered sucessfully</h1>";
            print "<a href='index.php'>go to login page</a>";
?>

这就是config.php的样子

<?php
$servername = "localhost";
$username = "*****";
$password = ****;
$dbname='unsq';



// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully !!";
?>

0 个答案:

没有答案