使用if语句和查询

时间:2015-04-17 16:46:36

标签: javascript php jquery mysql ajax

我尽量简化这个。 当用户点击提交按钮时,会发生以下情况:

  1. 验证信用卡/失效日期/ cvc值是否有效
  2. 生成令牌作为输入
  3. 令牌的值在php代码中发布
  4. 查询运行并存储值并重新定位页面
  5. 我已经能够管理到2号。 我无法发布令牌的值,因此不会存储令牌值。

    约束:

    1. 如果找不到或没有令牌值,我就无法运行查询
    2. 在生成输入令牌之前,无法运行php脚本
    3. 执行完php查询后,需要销毁令牌输入
    4. 以下是生成令牌的js函数:

       <script type="text/javascript">
          // This identifies your website in the createToken call below
        Stripe.setPublishableKey('CODE');
      
        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;
              // Insert the token into the form so it gets submitted to the server
              $form.append($('<input type="text" name="stripeToken" />').val(token));
              // and re-submit
            }
          };
      
          jQuery(function($) {
            $('#payment-form').submit(function(e) {
              var $form = $(this);
      
              // Disable the submit button to prevent repeated clicks
              $form.find('button').prop('disabled', true);
      
              Stripe.card.createToken($form, stripeResponseHandler);
      
              // Prevent the form from submitting with the default action
              return false;
            });
          });
      
        </script>
      

      下面是php代码

       if(isset($_POST['paid'])){
                $course_token = $_POST['stripeToken'];
      
      if (!empty($course_token)) {
      
          $course_price_final = $_POST['course_price_final'];
          $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);
      
      ob_start();
      
      }
      }
      ?>
      

      提前致谢,如有任何澄清,请告诉我。

2 个答案:

答案 0 :(得分:1)

您似乎错过了stripeResponseHandler中的提交:

$form.get(0).submit();

如注释所示,提交函数中的返回false会阻止提交表单。这是允许Stripe处理请求所必需的。您为Stripe提供了stripeResponseHandler函数,一旦它有机会满足您的请求并返回有效令牌或失败并出现错误,它将调用该函数。不要删除

return false;

语句会干扰允许Stripe正确处理请求的工作流程。

您的stripeResponseHandler需要处理来自Stripe的任何错误,如果没有找到,则需要提交表单。我上面的代码是我在我的网站上用来提交表单的代码。

get(0)返回DOM元素,提交直接发出提交。

答案 1 :(得分:1)

在提交处理程序的末尾删除return false语句。它可以防止发布表单。

您可以在提交之前实际附加输入并稍后提交表单。

jQuery(function($) {
  $('#payment-form').click(function(e) {
    var $form = $(this);

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

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

    // Prevent the form from submitting with the default action
    $form.submit();
  });
});
相关问题