在php中执行php函数

时间:2015-04-17 21:19:03

标签: javascript php jquery mysql ajax

在下面的脚本中,一旦用户单击“提交”按钮并且没有错误,就会生成一个包含标记值的输入。然后我想在php页面中发布该值,以便我可以在我的查询中使用它。这里输入值成功生成但是将其值发布到php页面并执行php页面是问题(我在下面详细说明) 下面是剧本:

 <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) { 
   var $form = $('#payment-form');
    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
  // TODO: give your html-submit-input-tag an "id" attribute

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


function phpCall() {
 if( appendedStripeToken === true ){
   $.ajax({
    type: "POST",
    data: {run: true},
    url: 'functions/paymentEmail.php',
    success: function (response) {//response is value returned from php (for    your example it's "bye bye"
                  $('#payment-form').prop('disabled', true); // TODO: give your html-submit-input-tag an "id" attribute

        alert(response);
    }
   });
 }
} 
  </script>

现在特别看看这部分

url: 'functions/paymentEmail.php',
    success: function (response) {//response is value returned from php (for    your example it's "bye bye"
                  $('#payment-form').prop('disabled', true); // TODO: give your html-submit-input-tag an "id" attribute

        alert(response);

我不希望使用警告框执行php函数,因为它不会运行。 enter image description here

下面是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);

        $location = "../paymentConfirmed.php";


    header( "Location: $location" );

?>

1 个答案:

答案 0 :(得分:4)

如果你想使用ajax进行重定向,你可以返回一个网址,而不是window.location = response