脚本成功执行后运行php代码

时间:2015-04-17 12:52:23

标签: javascript php jquery mysql ajax

我有以下脚本

<script type="text/javascript">
    // This identifies your website in the createToken call below
  Stripe.setPublishableKey('');

    var stripeResponseHandler = function(status, response) {
      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 {
        // token contains id, last4, and card type
        var token = response.id;
        var appendedStripeToken = false;
        // Insert the token into the form so it gets submitted to the server
        $form.append($('<input type="text" name="stripeToken" />').val(token);  
        function handleCall() {
            if (!appendedStripeToken) {
                appendedStripeToken = true;
                phpCall();
            } 
         }      // and re-submit
      }
    };

function onSubmit() {
    var $form = $('#'+id_from_form);

    // Disable the submit button to prevent repeated clicks
    $form.find('input').prop('disabled', true);

    Stripe.card.createToken($form, stripeResponseHandler);

}

function phpCall() {
   $.ajax({
      url: 'paymentEmail.php',
      success: function (response) {//response is value returned from php (for    your example it's "bye bye"
        alert(response);
      }
   });
}
  </script>

基本上phpCall()应该只在

之后执行
$form.append($('<input type="text" name="stripeToken" />').val(token);  

要再次执行,用户必须刷新或登陆页面,然后再次点击提交按钮。

这里的问题是,当用户点击提交时,PHP代码就会被执行,这很好但是当页面刷新或用户在页面上关联时,无论是否单击了提交按钮,都会执行php代码。

下面是php代码,我想存储此输入的值并将其发布在php页面上

   <input type="text" name="stripeToken" />

php页面:

 <?php


 $course_price_final = $_POST['course_price_final'];

 $course_token = $_POST['stripeToken'];
 $course_provider = $_POST['course_provider'];
 $user_email = $_POST['user_email'];
 $course_delivery = $_POST['course_delivery'];

 $order_date = date("Y-m-d");



 $insert_c = "insert into orders (course_title,course_price_final,course_provider,user_email,course_date,course_delivery,order_date,course_token) 
         values ('$crs_title','$course_price_final','$course_provider','$user_email','$course_date1','$course_delivery','$order_date','$course_token')";

 $run_c = mysqli_query($con, $insert_c); 

更新

 <script type="text/javascript">
    // This identifies your website in the createToken call below
  Stripe.setPublishableKey('CODE');

    var appendedStripeToken = false;

var stripeResponseHandler = function(status, response) {
    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 {
        // token contains id, last4, and card type
        var token = response.id;
        handleCall(token);
    }
};

function handleCall(token) { 
    if (!appendedStripeToken) { 
        // Insert the token into the form so it gets submitted to the server
        $form.append($('<input type="text" name="stripeToken" />').val(token); 
        appendedStripeToken = true; 
        phpCall(); 
    } 
}

function onSubmit() {
    var $form = $('#payment-form'); // TODO: give your html-form-tag an "id" attribute and type this id in this line. IMPORTANT: Don't replace the '#'!

    // Disable the submit button to prevent repeated clicks
    $('#paymentSubmit').prop('disabled', true); // TODO: give your html-submit-input-tag an "id" attribute

    Stripe.card.createToken($form, stripeResponseHandler);
}

function phpCall() {
    $.ajax({
        url: 'paymentEmail.php',
        success: function (response) { // response is value returned from php (for your example it's "bye bye")
            alert(response);
        }
    });
}
  </script>

</head>

<body>



<form action="" method="POST" id="payment-form" class="form-horizontal">
  <div class="row row-centered">
  <div class="col-md-4 col-md-offset-4">


  <div class="alert alert-danger" id="a_x200" style="display: none;"> <strong>Error!</strong> <span class="payment-errors"></span> </div>
  <span class="payment-success">
  <? $success ?>
  <? $error ?>
  </span>
  <fieldset>

    <div class="form-group">
    <label class="col-sm-4 control-label" for="textinput">Choose Start Date</label>
    <div class="col-sm-6">
      <select name="course_date" class="address form-control" required>
      <option><?php 

      if(isset($_GET['crs_id'])){

      $course_id = $_GET['crs_id'];

      $get_crs = "select * from courses where course_id='$course_id'";
  $run_crs = mysqli_query($con, $get_crs);

 while($row_crs=mysqli_fetch_array($run_crs)){

        $course_date1  = $row_crs['course_date1'];


        echo $course_date1 ;

  }
}
  ?></option>
  <option value=<?php 

      if(isset($_GET['crs_id'])){

      $course_id = $_GET['crs_id'];

      $get_crs = "select * from courses where course_id='$course_id'";
  $run_crs = mysqli_query($con, $get_crs);

 while($row_crs=mysqli_fetch_array($run_crs)){

        $course_provider = $row_crs['course_provider'];

        $course_date2 = $row_crs['course_date2'];

                $course_price = $row_crs['course_price'];
                                $course_title = $row_crs['course_title'];



        $course_priceFinal = $row_crs['course_priceFinal'];
   $dig = explode(".", $row_crs['course_tax']);
    $course_tax = $dig[1];



        echo $course_date2 ;

  }
}
  ?>/>
  </select>
    </div>
  </div>
<input type="hidden" name="course_provider" value="<?php echo $course_provider; ?>" >
<input type="hidden" name="course_title" value="<?php echo $course_title; ?>" >

    <div class="form-group">
    <label class="col-sm-4 control-label" for="textinput">Course Delivery</label>
    <div class="col-sm-6">
      <select name="course_delivery" class="address form-control" required>
    <option value="classroom">Classroom</option>
  </select>
    </div>
  </div>


  <div class="form-group">
    <label class="col-sm-4 control-label" for="textinput">Seats</label>
    <div class="col-sm-6">
      <select name="course_seats" class="address form-control" required>
      <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>
    </div>
  </div>

  <!-- Form Name -->
  <legend>Billing Details</legend>

  <!-- Street -->
  <div class="form-group">
    <label class="col-sm-4 control-label" for="textinput">Billing Street</label>
    <div class="col-sm-6">
      <input type="text" name="street" placeholder="Street" class="address form-control" required>
    </div>
  </div>

  <!-- City -->
  <div class="form-group">
    <label class="col-sm-4 control-label" for="textinput">Billing City</label>
    <div class="col-sm-6">
      <input type="text" name="city" placeholder="City" class="city form-control" required>
    </div>
  </div>

  <!-- State -->
  <div class="form-group">
    <label class="col-sm-4 control-label" for="textinput">Billing Province</label>
    <div class="col-sm-6">
      <input type="text" name="province" maxlength="65" placeholder="Province" class="state form-control" required>
    </div>
  </div>

  <!-- Postcal Code -->
  <div class="form-group">
    <label class="col-sm-4 control-label" for="textinput">Postal Code</label>
    <div class="col-sm-6">
      <input type="text" name="postal" maxlength="9" placeholder="Postal Code" class="zip form-control" required>
    </div>
  </div>

  <!-- Country -->
  <div class="form-group">
    <label class="col-sm-4 control-label" for="textinput">Country</label>
    <div class="col-sm-6"> 
      <input type="text" name="country" placeholder="Country" class="country form-control">
      <div class="country bfh-selectbox bfh-countries" name="country" placeholder="Select Country" data-flags="true" data-filter="true"> </div>
    </div>
  </div>

  <!-- Email -->

  <?php 


  $email = $_GET['user_email'];
// Note the (int). This is how you cast a variable.
$coupon = isset($_GET['crs_coupon']) ? (int)$_GET['crs_coupon'] : '';
if(is_int($coupon)){
    $course_priceFinalAll = $course_priceFinal - ($course_priceFinal * ($coupon/100));
    $coupon_deduction = $course_priceFinal * ($coupon/100);

};

  ?>
  <div class="form-group">
    <label class="col-sm-4 control-label" for="textinput">Email</label>
    <div class="col-sm-6">
      <input type="text" name="user_email" value=<?php echo $email; ?> class="email form-control" required>
            <input type="hidden" name="course_title" value=<?php echo $course_title; ?> class="email form-control">
                        <input type="hidden" id="box1"  name="course_price" value=<?php echo $course_priceFinal; ?> class="email form-control">


    </div>
  </div><br>
    <legend>Purchase Details</legend>

    <div class="form-group">
    <label class="col-sm-4 control-label">Coupon Code</label>
    <div class="col-sm-6">
      <input type="text"  style="text-align:left; float:left; border:none; width:100px;"  name="name" class="email form-control" placeholder="Coupon Code" value="<?php echo $coupon; ?>%" readonly>

    </div>
  </div>


    <div class="form-group">
    <label class="col-sm-4 control-label">Want to replace the current coupon code?</label>
    <div class="col-sm-6">
      <input type="text"  name="name" class="email form-control" placeholder="Please enter another coupon code" value="">

    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-4 control-label" style="color:#FF6400; font-weight:normal;">Tax</label>
    <div class="col-sm-6">
      <input type="text" class="email form-control"  name="name"style="text-align:left; float:left; border:none; width:100px;" placeholder="Please enter another coupon code" value=" <?php echo $course_tax; ?>%" readonly>

    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-4 control-label" style="color:#FF6400;font-weight:normal;">Price before Tax</label>
    <div class="col-sm-6">
      <input type="text" style="text-align:left; float:left; border:none; width:100px;" name="course_price_before_tax" class="email form-control"  value=" $<?php echo $course_price; ?>" readonly>

    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-4 control-label" style="color:#FF6400; font-weight:normal;">Price After Tax</label>
    <div class="col-sm-6">
      <input type="text" style="text-align:left; float:left; border:none; width:100px;" name="course_price_after_tax" class="email form-control"  value=" $<?php echo $course_priceFinal; ?>" readonly>

    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-4 control-label" style="color:#FF6400; font-weight:normal;">Coupon Deduction</label>
    <div class="col-sm-6">
      <input type="text" style="text-align:left; float:left; border:none; width:100px;" name="course_deduction" class="email form-control"  value=" -$<?php echo $coupon_deduction; ?>" readonly>

    </div>
  </div>

   <div class="form-group">
    <label class="col-sm-4 control-label" style="color:#FF6400"><b>Final Price</b></label>
    <div class="col-sm-6">
<input type="text" style="text-align:left; font-weight:bold; float:left; border:none; width:100px;" name="course_price_final" class="email form-control" placeholder="Course Price Final" value="$<?php echo $course_priceFinalAll; ?>" readonly>

    </div>
  </div>

  <!-- Coupon Code-->
<input type="hidden" name="coupon_code" class="email form-control" placeholder="Coupon Code" value=<?php echo $coupon; ?> readonly>

<!-- Price Final -->
  <br>
  <fieldset>
    <legend>Card Details</legend>
        <span class="payment-errors"></span>

    <!-- Card Holder Name -->
    <div class="form-group">
      <label class="col-sm-4 control-label"  for="textinput">Card Holder's Name</label>
      <div class="col-sm-6">
        <input type="text" name="cardholdername" maxlength="70"  placeholder="Card Holder Name" class="card-holder-name form-control" required>
      </div>
    </div>

    <!-- Card Number -->
    <div class="form-group">
      <label class="col-sm-4 control-label" for="textinput">Card Number</label>
      <div class="col-sm-6">
        <input type="text" id="cardnumber" maxlength="19" data-stripe="number" placeholder="Card Number" class="card-number form-control" required>
      </div>
    </div>


    <div class="form-row">
      <label class="col-sm-4 control-label">CVC</label>
              <div class="col-sm-6">

        <input type="text" size="4" class="email form-control" data-stripe="cvc" required/>
    </div>
  </div>
 <br>
    <div class="form-row"><br><br>
      <label class="col-sm-4 control-label">Expiration (MM/YYYY)</label>
              <div class="col-sm-6">
 <div class="form-inline">
          <select name="select2" data-stripe="exp-month" class="card-expiry-month stripe-sensitive required form-control" required>
            <option value="01" selected="selected">01</option>
            <option value="02">02</option>
            <option value="03">03</option>
            <option value="04">04</option>
            <option value="05">05</option>
            <option value="06">06</option>
            <option value="07">07</option>
            <option value="08">08</option>
            <option value="09">09</option>
            <option value="10">10</option>
            <option value="11">11</option>
            <option value="12">12</option>
          </select>



        </div>
      <input type="text" size="4" class="email form-control" data-stripe="exp-year" required/>
    </div>
 </div>


  <br>

    <!-- Submit -->
    <div class="control-group">
      <div class="controls">
        <center><br>
       <input id="paymentSubmit" class="btn btn-danger"  name="paid"  onClick="onSubmit()" type="submit" value="Pay Now" class="btn btn-success"></button>
        </center>
      </div>
    </div>
  </fieldset>
</form>

更新2 enter image description here

两个小问题:单击后按钮被禁用,如果例如返回错误,则不允许再次单击,如上所示。它应该只在输入释放后禁用它

$ form.append($(&#39;&#39;。)VAL(令牌));

2 个答案:

答案 0 :(得分:2)

尝试通过POST将变量发送到PHP:

function phpCall() {
   $.ajax({
      type: "POST",
      data: {run: true},
      url: 'paymentEmail.php',
      success: function (response) {//response is value returned from php (for    your example it's "bye bye"
        alert(response);
      }
   });
}

然后在你的php中:

if ($_POST['run']) {

    $course_price_final = $_POST['course_price_final'];
    $course_token = $_POST['stripeToken'];
    $course_provider = $_POST['course_provider'];
    $user_email = $_POST['user_email'];
    $course_delivery = $_POST['course_delivery'];
    $order_date = date("Y-m-d");
    $insert_c = "insert into orders (course_title,course_price_final,course_provider,user_email,course_date,course_delivery,order_date,course_token) 
             values ('$crs_title','$course_price_final','$course_provider','$user_email','$course_date1','$course_delivery','$order_date','$course_token')";
    $run_c = mysqli_query($con, $insert_c);
}

答案 1 :(得分:1)

它可能会对您有所帮助:

function phpCall() {
 if( appendedStripeToken === true ){
   $.ajax({
    type: "POST",
    data: {run: true},
    url: 'paymentEmail.php',
    success: function (response) {//response is value returned from php (for    your example it's "bye bye"
        alert(response);
    }
   });
 }
}