[英]php code in div id=“upload” of xhr.open(“POST”, $id(“upload”).action, true); not executing
[英]Working with forms: Executing the ID then the php action
我有以下表格
<form action="" method="POST" id="payment-form" class="form-horizontal">
当用户点击提交时,应该执行以下操作:JavaScript函数开始运行,一旦完成,然后运行php代码
但是,我的问题是,将ID添加到表单中并且仅运行JavaScript函数时,PHP代码无法执行。 我不希望同时执行两个命令,而是先执行javascript函数,然后执行php。
下面是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代码
<?php
if(isset($_POST['paid'])){
$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);
?>
因此,我的问题可以归结为:
为什么不按以下方式调整方法:
$("#payment-form").click(function(event){
event.preventDefault(); //prevent form submission or onclick event
event.find('button').prop('disabled', true);
var var1 = ""; //Value from form
var var2 = ""; //Value from form
phpCall(var1, var2);
});
function phpCall() {
$.ajax({
url: 'hello.php',
success: function (response) {//response is value returned from php (for your example it's "bye bye"
alert(response);
}
});
}
// ajax示例来自: 从jquery调用php函数?
在这里,替换整个jQuery(function($) {/**stuff**/});
阻止与此:
//wait for the DOM to load HTML tags
$(document).ready(function(){
//attach event listeners
$('#payment-form button').on({
//attach the click listener
click:function(e){
//stop javascript from calling other asynchronous click events
e.preventDefault();
//get form element from the DOM
var $form = $('#payment-form');
// Disable the submit button to prevent repeated clicks
$(this).prop('disabled', true);
//create the stripe token
Stripe.card.createToken($form, stripeResponseHandler);
//Submit the form
$form.submit();
} //click
}); //on()
}); //DOM Ready
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.