Php 数据显示在表单上,但不会保存到数据库中
我得到了堆栈,我试图自定义代码,但脚本没有将echo值保存到数据库中 以下是我目前正在做的工作:Php 数据显示在表单上,但不会保存到数据库中,php,Php,我得到了堆栈,我试图自定义代码,但脚本没有将echo值保存到数据库中 以下是我目前正在做的工作: function give_basic_fees_js() { ?> <script> <?php //Generate $digits_needed=3; global $random_number; $random_number =''; // set up a blank string $count=0; while ( $co
function give_basic_fees_js() {
?>
<script>
<?php //Generate
$digits_needed=3;
global $random_number;
$random_number =''; // set up a blank string
$count=0;
while ( $count < $digits_needed ) {
$random_digit = mt_rand(0, 9);
$random_number .= $random_digit;
$count++;
}?>
var give_global_vars;
//JS for Basic Fee calculation on the fly.
jQuery(function ($) {
/**
* Event Triggers
*/
//When document runs.
$(document).ready(give_basic_update_percentage());
//If donation amount update is triggered.
$(document).on('give_donation_value_updated', function () {
give_basic_update_percentage();
});
//If the donation.
$(document).on('blur', '.give-donation-amount .give-text-input', function (e) {
give_basic_update_percentage();
});
/**
* JS update logic - Basic update percentage JS.
*/
function give_basic_update_percentage() {
//Set vars
var percentage = <?php echo $random_number; ?>;
var current_total = $('input[name="give-amount"]').val();
//Unformat current total so fee calculation is correct for larger donations.
current_total = Math.abs(parseFloat(accounting.unformat(current_total, give_global_vars.decimal_separator)));
var fee = percentage;
var new_total = current_total + fee;
//Set the custom amount input value format properly
var format_args = {
symbol: give_global_vars.currency_sign,
decimal: give_global_vars.decimal_separator,
thousand: give_global_vars.thousands_separator,
precision: give_global_vars.number_decimals
};
fee = accounting.formatMoney(fee, format_args);
new_total = accounting.formatMoney(new_total, format_args);
//Update fee information text.
$('span.give-basic-fee-amount').text(fee);
//Update final total text.
$('.give-final-total-amount').text(new_total);
}
});
</script>
<?php }
add_action( 'wp_print_footer_scripts', 'give_basic_fees_js' );
/**
* Adds the fee to the DB upon submit.
*
* @param $sanitized_amount
*
* @return string
*/
function give_basic_fees_add_fee( $sanitized_amount ) {
//$fee_percentage = (int) give_get_option( 399 );
$fee = $sanitized_amount;
$new_total = $fee + percentage;
return $new_total;
}
add_filter( 'give_donation_total', 'give_basic_fees_add_fee', 1, 1 );
函数给出基本费用{
?>
var给出全局变量;
//JS用于实时计算基本费用。
jQuery(函数($){
/**
*事件触发器
*/
//当文档运行时。
$(文档).ready(提供基本更新百分比());
//如果触发捐赠金额更新。
$(文档).on('give\u generation\u value\u updated',函数(){
给出基本更新百分比();
});
//如果捐款。
$(文档).on('blur',')。给出捐赠金额。给出文本输入',函数(e){
给出基本更新百分比();
});
/**
*JS更新逻辑-基本更新百分比JS。
*/
函数给出基本更新百分比(){
//设置变量
风险值百分比=;
var current_total=$('input[name=“give amount”]”)。val();
//不符合当前总额,因此对于较大的捐赠,费用计算是正确的。
current_total=Math.abs(parseFloat(accounting.unformat(current_total,give_global_vars.decimal_separator));
var费用=百分比;
var new_total=当前_total+费用;
//正确设置自定义金额输入值格式
变量格式参数={
符号:给出全球货币符号,
decimal:指定\u全局变量\u小数\u分隔符,
千:提供全球变量。千分位分隔符,
精度:给出\u全局\u变量。数字\u小数
};
费用=会计。格式货币(费用,格式参数);
new_total=accounting.formatMoney(new_total,format_args);
//更新费用信息文本。
$('span.给出基本费用金额')。文本(费用);
//更新最终全文。
$('给出最终总额').text(新总额);
}
});
我在你的代码中找不到任何db交互。这实际上是我为WordPress CMSok定制的函数,我们可以看到。但是,你一直使用的数据库代码在哪里?如果你的问题是写入数据库,你需要向我们展示你是如何做到这一点的。如果你还没有编写,你需要开始,然后询问具体问题如果你遇到困难,我会提醒你。也许我写错了问题。我想要的是得到在上行(出现)生成的相同随机数值在提交之前的表单中。现在,随机数是不同的。例如,在按下提交按钮之前,随机数显示为:xxx.789,但当我按下提交按钮时,db上的值记录为:xxx.456。我的意思是,当按下按钮时,脚本生成新的一个:(你说的“上行”是什么意思??无论如何,如果您希望在页面刷新之间(即页面首次加载和回发之间(表单提交))保留数字然后,您需要将它放在表单上的一个隐藏字段中,然后在回发时读取该字段,或者将其存储在PHP会话中并从中读取。还需要设置一个if条件,以控制脚本在回发时执行时是否生成新的随机数。