Php 中断表单post,执行jquery$.post到
在我试图解决的问题上,我正在尝试一种不同的策略。我有一个表格,收集人口统计信息和付款金额。然后,我需要以浏览器不可见的方式计算哈希、序列和时间戳,然后用这些计算字段更新隐藏的表单值,然后提交给支付处理器 我目前正在尝试的是,在主页上显示我的表单,其中包含输入和隐藏字段,当单击submit时,一个jquery函数运行,向一个计算php发送一个ajax帖子,该php使用amount输入,这是散列的一部分。php计算这些项,然后返回一个json数组 然后我想解析返回,更新隐藏的表单值,然后提交 我将在下面附上我的代码,但现在,我正在中断这篇文章,将值传递给计算php并构建数组。在这一点上,我的过程停止了,似乎我并没有返回主页来更新值和发布 我做错了什么?另外,如果你有更好的解决方案,我很想听听。谢谢 主页:Php 中断表单post,执行jquery$.post到,php,jquery,ajax,json,forms,Php,Jquery,Ajax,Json,Forms,在我试图解决的问题上,我正在尝试一种不同的策略。我有一个表格,收集人口统计信息和付款金额。然后,我需要以浏览器不可见的方式计算哈希、序列和时间戳,然后用这些计算字段更新隐藏的表单值,然后提交给支付处理器 我目前正在尝试的是,在主页上显示我的表单,其中包含输入和隐藏字段,当单击submit时,一个jquery函数运行,向一个计算php发送一个ajax帖子,该php使用amount输入,这是散列的一部分。php计算这些项,然后返回一个json数组 然后我想解析返回,更新隐藏的表单值,然后提交 我将在
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(
function () {
$('#Waterform').submit(
function(event) {
//var txt = $("x_amount").val();
var formData = { xamount : $('#x_amount').val() };
$.ajax({
type: 'POST',
url: "calc_fp_etc_anet.php",
data: formData,
success: function(result,stat,xmlRegObj){
for (var key in result)
{
if (result.hasOwnProperty(key))
{
$('#returnmsg').val(result[key].type);
$('#x_fp_hash').val(result[key].fp_hash);
$('#x_fp_sequence').val(result[key].fp_sequence);
$('#x_fp_timestamp').val(result[key].fp_timestamp);
}
}
//return false;
$("#Waterform").submit();},
error: function (jqXHR, textStatus, errorThrown){
$('#returnmsg').val(textStatus);
return false;},
dataType: "json",
async:false
});
}
)
}
);
</script>
</head>
<body>
<form id="Waterform" name="adaWaterform" action = 'https://secure.authorize.net/gateway/transact.dll' method = 'post'>
<input type = 'hidden' id = 'x_show_form' name = 'x_show_form' value = 'PAYMENT_FORM' />
<input type = 'hidden' id = 'x_description' name = 'x_description' value = 'Online Bill Payment' />
<input type = 'hidden' id = 'x_login' name = 'x_login' value = 'xxx4NhDyy' />
<input type = 'input' id = 'x_fp_hash' name = 'x_fp_hash' value = '' />
<input type = 'input' id = 'x_fp_sequence' name = 'x_fp_sequence' value = '' />
<input type = 'input' id = 'x_fp_timestamp' name = 'x_fp_timestamp' value = '' />
<input type = 'input' id = 'returnmsg' name = 'returnmsg' value = '' />
<div id="m_divLineItems">
<h3>Service Information</h3>
<div id="ContentBodyPlaceholder_m_divTime"></div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAccountNumber" class="InfoLabel">
Account Number</label>
<input type = 'text' id = 'x_invoice_num' name = 'x_invoice_num' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblPhone" class="InfoLabel">
Phone</label>
<input type = 'text' id = 'x_phone' name = 'x_phone' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblEMail" class="InfoLabel">
e-mail</label>
<input type = 'text' id = 'x_email' name = 'x_email' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblServiceAddress" class="InfoLabel">
Service Address</label>
<input type = 'text' id = 'x_ship_to_address' name = 'x_ship_to_address' value = '' />
</div>
<h3>Billing Information</h3>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblFirstName" class="InfoLabel">
First Name</label>
<input type = 'text' id = 'x_first_name' name = 'x_first_name' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblLastName" class="InfoLabel">
Last Name</label>
<input type = 'text' id = 'x_last_name' name = 'x_last_name' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAddress" class="InfoLabel">
Address</label>
<input type = 'text' id = 'x_address' name = 'x_address' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblCity" class="InfoLabel">
City</label>
<input type = 'text' id = 'x_city' name = 'x_city' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblState" class="InfoLabel">
State</label>
<input type = 'text' id = 'x_state' name = 'x_state' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblZip" class="InfoLabel">
Zip</label>
<input type = 'text' id = 'x_zip' name = 'x_zip' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAmount" class="InfoLabel">
Amount</label>
<input type = 'text' id = 'x_amount' name = 'x_amount' value = '' />
<input type="submit" class="submit" id="submitbutton" name="submitbutton" value="Pay Bill >>"/>
</div>
</div>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(
function () {
$('#adaWaterform').submit(
function(event) {
event.preventDefault();
$.post("calc_fp_etc_anet.php",{ xamount : $('#x_amount').val() }, function(result){
//alert("Data: " + result);
var json_obj = $.parseJSON(result);
//alert( json_obj.fp_hash);
$('#x_fp_hash').val(json_obj.fp_hash);
$('#x_fp_sequence').val(json_obj.fp_sequence);
$('#x_fp_timestamp').val(json_obj.fp_timestamp);
$('#Waterform').unbind("submit").submit();
});//function result
}//function event
//$("#Waterform").submit();
)//submit
}//function
);//ready
</script>
</head>
<body>
<form id="Waterform" name="Waterform" action = 'https://secure.authorize.net/gateway/transact.dll' method = 'post'>
<input type = 'hidden' id = 'x_show_form' name = 'x_show_form' value = 'PAYMENT_FORM' />
<input type = 'hidden' id = 'x_description' name = 'x_description' value = Online Bill Payment' />
<input type = 'hidden' id = 'x_login' name = 'x_login' value = 'xxx4Nhyyy' />
<input type = 'hidden' id = 'x_fp_hash' name = 'x_fp_hash' value = '' />
<input type = 'hidden' id = 'x_fp_sequence' name = 'x_fp_sequence' value = '' />
<input type = 'hidden' id = 'x_fp_timestamp' name = 'x_fp_timestamp' value = '' />
<div id="m_divLineItems">
<h3>Service Information</h3>
<div id="ContentBodyPlaceholder_m_divTime"></div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAccountNumber" class="InfoLabel">
Account Number</label>
<input type = 'text' id = 'x_invoice_num' name = 'x_invoice_num' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblPhone" class="InfoLabel">
Phone</label>
<input type = 'text' id = 'x_phone' name = 'x_phone' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblEMail" class="InfoLabel">
e-mail</label>
<input type = 'text' id = 'x_email' name = 'x_email' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblServiceAddress" class="InfoLabel">
Service Address</label>
<input type = 'text' id = 'x_ship_to_address' name = 'x_ship_to_address' value = '' />
</div>
<h3>Billing Information</h3>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblFirstName" class="InfoLabel">
First Name</label>
<input type = 'text' id = 'x_first_name' name = 'x_first_name' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblLastName" class="InfoLabel">
Last Name</label>
<input type = 'text' id = 'x_last_name' name = 'x_last_name' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAddress" class="InfoLabel">
Address</label>
<input type = 'text' id = 'x_address' name = 'x_address' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblCity" class="InfoLabel">
City</label>
<input type = 'text' id = 'x_city' name = 'x_city' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblState" class="InfoLabel">
State</label>
<input type = 'text' id = 'x_state' name = 'x_state' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblZip" class="InfoLabel">
Zip</label>
<input type = 'text' id = 'x_zip' name = 'x_zip' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAmount" class="InfoLabel">
Amount</label>
<input type = 'text' id = 'x_amount' name = 'x_amount' value = '' />
<input type="submit" class="submit" id="submitbutton" name="submitbutton" value="Pay Water Bill >>"/>
</div>
</div>
</form>
</body>
</html>
$(文件)。准备好了吗(
函数(){
$(“#水形式”)。提交(
功能(事件){
//var txt=$(“x_金额”).val();
var formData={xamount:$('x#u amount').val();
$.ajax({
键入:“POST”,
url:“calc_fp_etc_anet.php”,
数据:formData,
成功:函数(结果、统计、xmlRegObj){
for(var输入结果)
{
if(result.hasOwnProperty(key))
{
$('#returnmsg').val(结果[key].type);
$('#x_fp_hash').val(结果[key].fp_hash);
$('x#fp_序列').val(结果[key].fp_序列);
$('x#fp_timestamp').val(结果[key].fp_timestamp);
}
}
//返回false;
$(“#水形式”).submit();},
错误:函数(jqXHR、textStatus、errorshown){
$('#returnmsg').val(textStatus);
返回false;},
数据类型:“json”,
异步:false
});
}
)
}
);
服务指南
帐号
电话
电子邮件
服务地址
计费信息
名字
姓
地址
城市
陈述
拉链
数量
PHP计算:
<?php
require_once (dirname(__FILE__).'./config.php');
$amount = $_POST['xamount'];
$loginID = AUTHORIZENET_API_LOGIN_ID;
$transactionKey = AUTHORIZENET_TRANSACTION_KEY;
// a sequence number is randomly generated
$sequence = rand(1, 10000);
// a timestamp is generated
$timeStamp = time();
$x_fp_hash = hash_hmac("md5", $loginID . "^" . $sequence . "^" . $timeStamp . "^" . $amount . "^", $transactionKey);
$x_fp_sequence = $sequence;
$x_fp_timestamp = $timeStamp;
$result = json_encode(array("type"=>"success","fp_hash"=>"$x_fp_hash","fp_sequence"=>"$x_fp_sequence","fp_sequence"=>"$x_fp_sequence","fp_timestamp"=>"$x_fp_timestamp"));
echo $result;
die();
?>
终于明白了!出于某种原因,.post不喜欢最后的“json”声明,即使返回的是json。然后我不得不使用parseJSON将值放入数组中。然后我更新了隐藏的值,解除了提交的绑定,并提交了表单。工作得很好。需要javascript…但它可以工作 主页:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(
function () {
$('#Waterform').submit(
function(event) {
//var txt = $("x_amount").val();
var formData = { xamount : $('#x_amount').val() };
$.ajax({
type: 'POST',
url: "calc_fp_etc_anet.php",
data: formData,
success: function(result,stat,xmlRegObj){
for (var key in result)
{
if (result.hasOwnProperty(key))
{
$('#returnmsg').val(result[key].type);
$('#x_fp_hash').val(result[key].fp_hash);
$('#x_fp_sequence').val(result[key].fp_sequence);
$('#x_fp_timestamp').val(result[key].fp_timestamp);
}
}
//return false;
$("#Waterform").submit();},
error: function (jqXHR, textStatus, errorThrown){
$('#returnmsg').val(textStatus);
return false;},
dataType: "json",
async:false
});
}
)
}
);
</script>
</head>
<body>
<form id="Waterform" name="adaWaterform" action = 'https://secure.authorize.net/gateway/transact.dll' method = 'post'>
<input type = 'hidden' id = 'x_show_form' name = 'x_show_form' value = 'PAYMENT_FORM' />
<input type = 'hidden' id = 'x_description' name = 'x_description' value = 'Online Bill Payment' />
<input type = 'hidden' id = 'x_login' name = 'x_login' value = 'xxx4NhDyy' />
<input type = 'input' id = 'x_fp_hash' name = 'x_fp_hash' value = '' />
<input type = 'input' id = 'x_fp_sequence' name = 'x_fp_sequence' value = '' />
<input type = 'input' id = 'x_fp_timestamp' name = 'x_fp_timestamp' value = '' />
<input type = 'input' id = 'returnmsg' name = 'returnmsg' value = '' />
<div id="m_divLineItems">
<h3>Service Information</h3>
<div id="ContentBodyPlaceholder_m_divTime"></div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAccountNumber" class="InfoLabel">
Account Number</label>
<input type = 'text' id = 'x_invoice_num' name = 'x_invoice_num' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblPhone" class="InfoLabel">
Phone</label>
<input type = 'text' id = 'x_phone' name = 'x_phone' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblEMail" class="InfoLabel">
e-mail</label>
<input type = 'text' id = 'x_email' name = 'x_email' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblServiceAddress" class="InfoLabel">
Service Address</label>
<input type = 'text' id = 'x_ship_to_address' name = 'x_ship_to_address' value = '' />
</div>
<h3>Billing Information</h3>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblFirstName" class="InfoLabel">
First Name</label>
<input type = 'text' id = 'x_first_name' name = 'x_first_name' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblLastName" class="InfoLabel">
Last Name</label>
<input type = 'text' id = 'x_last_name' name = 'x_last_name' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAddress" class="InfoLabel">
Address</label>
<input type = 'text' id = 'x_address' name = 'x_address' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblCity" class="InfoLabel">
City</label>
<input type = 'text' id = 'x_city' name = 'x_city' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblState" class="InfoLabel">
State</label>
<input type = 'text' id = 'x_state' name = 'x_state' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblZip" class="InfoLabel">
Zip</label>
<input type = 'text' id = 'x_zip' name = 'x_zip' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAmount" class="InfoLabel">
Amount</label>
<input type = 'text' id = 'x_amount' name = 'x_amount' value = '' />
<input type="submit" class="submit" id="submitbutton" name="submitbutton" value="Pay Bill >>"/>
</div>
</div>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(
function () {
$('#adaWaterform').submit(
function(event) {
event.preventDefault();
$.post("calc_fp_etc_anet.php",{ xamount : $('#x_amount').val() }, function(result){
//alert("Data: " + result);
var json_obj = $.parseJSON(result);
//alert( json_obj.fp_hash);
$('#x_fp_hash').val(json_obj.fp_hash);
$('#x_fp_sequence').val(json_obj.fp_sequence);
$('#x_fp_timestamp').val(json_obj.fp_timestamp);
$('#Waterform').unbind("submit").submit();
});//function result
}//function event
//$("#Waterform").submit();
)//submit
}//function
);//ready
</script>
</head>
<body>
<form id="Waterform" name="Waterform" action = 'https://secure.authorize.net/gateway/transact.dll' method = 'post'>
<input type = 'hidden' id = 'x_show_form' name = 'x_show_form' value = 'PAYMENT_FORM' />
<input type = 'hidden' id = 'x_description' name = 'x_description' value = Online Bill Payment' />
<input type = 'hidden' id = 'x_login' name = 'x_login' value = 'xxx4Nhyyy' />
<input type = 'hidden' id = 'x_fp_hash' name = 'x_fp_hash' value = '' />
<input type = 'hidden' id = 'x_fp_sequence' name = 'x_fp_sequence' value = '' />
<input type = 'hidden' id = 'x_fp_timestamp' name = 'x_fp_timestamp' value = '' />
<div id="m_divLineItems">
<h3>Service Information</h3>
<div id="ContentBodyPlaceholder_m_divTime"></div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAccountNumber" class="InfoLabel">
Account Number</label>
<input type = 'text' id = 'x_invoice_num' name = 'x_invoice_num' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblPhone" class="InfoLabel">
Phone</label>
<input type = 'text' id = 'x_phone' name = 'x_phone' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblEMail" class="InfoLabel">
e-mail</label>
<input type = 'text' id = 'x_email' name = 'x_email' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblServiceAddress" class="InfoLabel">
Service Address</label>
<input type = 'text' id = 'x_ship_to_address' name = 'x_ship_to_address' value = '' />
</div>
<h3>Billing Information</h3>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblFirstName" class="InfoLabel">
First Name</label>
<input type = 'text' id = 'x_first_name' name = 'x_first_name' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblLastName" class="InfoLabel">
Last Name</label>
<input type = 'text' id = 'x_last_name' name = 'x_last_name' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAddress" class="InfoLabel">
Address</label>
<input type = 'text' id = 'x_address' name = 'x_address' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblCity" class="InfoLabel">
City</label>
<input type = 'text' id = 'x_city' name = 'x_city' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblState" class="InfoLabel">
State</label>
<input type = 'text' id = 'x_state' name = 'x_state' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblZip" class="InfoLabel">
Zip</label>
<input type = 'text' id = 'x_zip' name = 'x_zip' value = '' />
</div>
<div class="LineItem">
<label id="ContentBodyPlaceholder_m_lblAmount" class="InfoLabel">
Amount</label>
<input type = 'text' id = 'x_amount' name = 'x_amount' value = '' />
<input type="submit" class="submit" id="submitbutton" name="submitbutton" value="Pay Water Bill >>"/>
</div>
</div>
</form>
</body>
</html>
$(文件)。准备好了吗(
函数(){
$(#adaWaterform')。提交(
功能(事件){
event.preventDefault();
$.post(“calc#u fp_etc_anet.php”,{xamount:$('#x#u amount').val(),函数(结果){
//警报(“数据:+结果”);
var json_obj=$.parseJSON(结果);
//警报(json_obj.fp_散列);
$('x#u fp_hash').val(json_obj.fp_hash);
$('x#u fp_序列').val(json#u obj.fp_序列);
$('x#fp_timestamp').val(json_obj.fp_timestamp);
$('#Waterform')。解除绑定(“提交”)。提交();
});//函数结果
}//功能事件
//$(“#水形式”).submit();
)//提交
}//作用
);//准备好的