gpt4 book ai didi

php - 从 php 页面在 mysql 表中插入 jquery var 时,我遇到了一件非常奇怪的事情

转载 作者:行者123 更新时间:2023-11-29 05:27:20 25 4
gpt4 key购买 nike

我有一个 php 页面,我在其中使用 jquery 函数根据复选框、单选按钮和文本框的值获取动态值。怎么回事是我使用了两个警报

1.) 警报(数据);

2.)警报(总计);

在我的 Jquery 函数的 ajax 部分中,只是为了确保我在“grand_total”中获得什么值。一切正常,警报正常,数据正确插入表中。

然后我从函数中删除了警报,一段时间后我再次开始测试整个站点,我发现 grand_total 的值没有被插入到 mysql 表中。

我再次发出这些警报以检查出了什么问题,再次一切正常。再次删除,问题又开始了。知道哪里出了问题吗?

这是来自 "xyz.php" 的 JQUERY func 的代码片段:

<script type="text/javascript">
$(document).ready(function() {

var grand_total = 0;

$("input").live("change keyup", function() {

$("#Totalcost").val(function() {

var total = 0;
$("input:checked").each(function() {

total += parseInt($(this).val(), 10);
});
var textVal = parseInt($("#min").val(), 10) || 0;

grand_total = total + textVal;

return grand_total;
});

});
$("#next").live('click', function() {

$.ajax({
url: 'xyz_sql.php',
type: 'POST',
data: {
grand_total: grand_total
},
success: function(data) {
// do something;

}
});

});

});

对应的HTML代码:

       <form method="post" id="logoform3" action="xyz_sql.php">
<input type="text" name="Totalcost" id="Totalcost" disabled/>
<input type="submit" id="Next" name="next"/>

这是来自 *"xyz_sql.php"* 的代码:

<?php
session_start();

include ("config.php");

$uid = $_SESSION['uid'];

$total= mysql_real_escape_string($_POST['grand_total']);

$sql="INSERT INTO form2 (total,uid)VALUES('$total','$uid');";

if($total > 0){
$res = mysql_query($sql);
}

if($res)
{
echo "<script> window.location.replace('abc.php') </script>";
}
else {
echo "<script> window.location.replace('xyz.php') </script>";
}
?>

最后但同样重要的是:echo "window.location.replace('abc.php') ";无论数据是否插入表中,都不会执行。

最佳答案

首先你像表单一样提交表单,而不是像 ajax - 因为在点击提交按钮时没有 preventDefault Action 。这就是为什么它看起来是正确的。但是在那种形式中没有名为“grand_total”的输入。所以你的 php 脚本失败了。

其次 - 您将 ajax 绑定(bind)到 ID 为“next”的元素 - 但在您的 html 中没有具有该 ID 的此类元素,这就是永远不会调用 ajax 的原因。

关于php - 从 php 页面在 mysql 表中插入 jquery var 时,我遇到了一件非常奇怪的事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18633395/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com