Javascript 在php中执行php函数

Javascript 在php中执行php函数,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,在下面的脚本中,一旦用户单击submit按钮并且不存在错误,就会生成一个包含令牌值的输入。然后我想在php页面中发布该值,以便在查询中使用它。这里输入值成功生成,但将其值发布到php页面并执行php页面是问题所在(我在下面详细说明) 以下是脚本: <script type="text/javascript"> // This identifies your website in the createToken call below Stripe.setPublishab

在下面的脚本中,一旦用户单击submit按钮并且不存在错误,就会生成一个包含令牌值的输入。然后我想在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>
我不希望php函数使用警报框执行,因为它不会运行。

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

?>


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

这种类型的链接胜过了使用ajax的目的感谢你的回复。然而,它似乎不能正常工作:它只是翻译世界文档并添加url%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20删除它的标题(“位置:$Location”);从您的文件中返回一个有效的url什么是有效的url?您必须返回
$location
我知道这是您的url这种节拍使用Ajax的目的感谢您的回复。然而,它似乎不能正常工作:它只是翻译世界文档并添加url%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20删除它的标题(“位置:$Location”);从您的文件中返回一个有效的url什么是有效的url?您必须返回
$location
我知道这是您的url我认为您过度使用了AJAX。创建动态页面需要AJAX,其中一些功能是在服务器上完成的,因此您不必刷新页面。为什么不创建一个表单并提交呢?这样,如果成功,请执行重定向。你的方法似乎太过分了。我知道它的各种复杂因素导致了这种情况。我可以详细说明为什么,但我只是想向前移动,所以你想显示重定向的响应?嗯,您可能可以将所有HTML插入到文档中,覆盖旧的HTML。如果您关心显示的url,您可以在同时支持history.pushState和responseURL属性的浏览器中使用history.pushState(null,null,xhr.responseURL),但我不知道如何使用jQuery获取responseURL属性(如果不可能,您必须使用vanilla JS)。或者像建议的答案一样,您可以使用
window.location=response
,但必须在PHP脚本中返回实际URL。此外,您可以将脚本重定向到的URL硬编码到
history.pushState
,而不是使用XHR对象responseURL。这只是一个想法,但这可能会使一切变得更加复杂。我认为您过度使用AJAX了。创建动态页面需要AJAX,其中一些功能是在服务器上完成的,因此您不必刷新页面。为什么不创建一个表单并提交呢?这样,如果成功,请执行重定向。你的方法似乎太过分了。我知道它的各种复杂因素导致了这种情况。我可以详细说明为什么,但我只是想向前移动,所以你想显示重定向的响应?嗯,您可能可以将所有HTML插入到文档中,覆盖旧的HTML。如果您关心显示的url,您可以在同时支持history.pushState和responseURL属性的浏览器中使用history.pushState(null,null,xhr.responseURL),但我不知道如何使用jQuery获取responseURL属性(如果不可能,您必须使用vanilla JS)。或者像建议的答案一样,您可以使用
window.location=response
,但必须在PHP脚本中返回实际URL。此外,您可以将脚本重定向到的URL硬编码到
history.pushState
,而不是使用XHR对象responseURL。这只是一个想法,但这可能会让一切变得更加复杂。
     <?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" );

?>