按后退按钮后,jQuery ajax 调用不会更新 mysql

发布于 2024-10-06 08:08:55 字数 1613 浏览 0 评论 0原文

我有一个表单,它使用 ajax 将数据提交到 mysql 数据库,然后将表单发送到 PayPal。

但是,提交后,如果我单击浏览器上的后退按钮,更改一些字段,然后再次提交表单,则 mysql 数据不会更新,也不会创建新条目。

这是我的 Jquery:

$j(".submit").click(function() {
        var hasError = false;
        var order_id = $j('input[name="custom"]').val();
        var order_amount = $j('input[name="amount"]').val();
        var service_type = $j('input[name="item_name"]').val();
        var order_to = $j('input[name="to"]').val();
        var order_from = $j('input[name="from"]').val();
        var order_message = $j('textarea#message').val();
        if(hasError == false) {
            var dataString = 'order_id='+ order_id + '&order_amount=' + order_amount + '&service_type=' + service_type + '&order_to=' + order_to + '&order_from=' + order_from + '&order_message=' + order_message;
            $j.ajax({ type: "GET", cache: false, url: "/gc_process.php", data: dataString, success: function() { } });

        } else {

            return false;

        }
    });

这是我的 PHP 脚本的样子:

<?php

// Make a MySQL Connection
include('dbconnect.php');

// Get data
$order_id = $_GET['order_id'];
$amount = $_GET['order_amount'];
$type = $_GET['service_type'];
$to = $_GET['order_to'];
$from = $_GET['order_from'];
$message = $_GET['order_message'];

// Insert a row of information into the table
mysql_query("REPLACE INTO gift_certificates (order_id, order_type, amount, order_to, order_from, order_message) VALUES('$order_id', '$type', '$amount', '$to', '$from', '$message')");

mysql_close();

?>

有什么想法吗?

I have a form that uses ajax to submit data to a mysql database, then sends the form on to PayPal.

However, after submitting, if I click the back button on my browser, change some fields, and then submit the form again, the mysql data isn't updated, nor is a new entry created.

Here's my Jquery:

$j(".submit").click(function() {
        var hasError = false;
        var order_id = $j('input[name="custom"]').val();
        var order_amount = $j('input[name="amount"]').val();
        var service_type = $j('input[name="item_name"]').val();
        var order_to = $j('input[name="to"]').val();
        var order_from = $j('input[name="from"]').val();
        var order_message = $j('textarea#message').val();
        if(hasError == false) {
            var dataString = 'order_id='+ order_id + '&order_amount=' + order_amount + '&service_type=' + service_type + '&order_to=' + order_to + '&order_from=' + order_from + '&order_message=' + order_message;
            $j.ajax({ type: "GET", cache: false, url: "/gc_process.php", data: dataString, success: function() { } });

        } else {

            return false;

        }
    });

Here's what my PHP script looks like:

<?php

// Make a MySQL Connection
include('dbconnect.php');

// Get data
$order_id = $_GET['order_id'];
$amount = $_GET['order_amount'];
$type = $_GET['service_type'];
$to = $_GET['order_to'];
$from = $_GET['order_from'];
$message = $_GET['order_message'];

// Insert a row of information into the table
mysql_query("REPLACE INTO gift_certificates (order_id, order_type, amount, order_to, order_from, order_message) VALUES('$order_id', '$type', '$amount', '$to', '$from', '$message')");

mysql_close();

?>

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绮筵 2024-10-13 08:08:55

您确实应该使用 POST 而不是 GET,但无论如何,我会检查以下内容:

  1. 在您单击返回并更改信息后,jQuery 正在执行 ajax 调用,您可能应该放置 console.log 或警报调用来查看如果 javascript 失败,
  2. 在 PHP 中添加一些回显和一些退出,然后逐行查看它能达到多远。由于您将其作为获取,因此您只需在浏览器中加载另一个选项卡并更改您需要的信息即可。
  3. 如果 jQuery 中的 $j 是您应该能够执行 $j.serialize() 的表单,这是一个方便的函数,可以在一个字符串中获取所有表单数据

You really should be using POST instead of GET, but regardless, I would check the following:

  1. That jQuery is executing the ajax call after you click back and change the information, you should probably put either a console.log or an alert calls to see if javascript is failing
  2. Add some echos in the PHP and some exits and go line by line and see how far it gets. Since you have it as a get, you can just load up another tab in your browser and change the information you need to.
  3. if $j in your jQuery is the form you should be able to just do $j.serialize(), it's a handy function to get all the form data in one string
爱人如己 2024-10-13 08:08:55

伙计,

你是否将 jquery 包含在中

$j(function(){


});

以确保它仅在 dom 准备好时执行?

另外,我假设您已手动将 jquery 从“$”重命名为“$j”以防止命名空间冲突。如果不是这种情况,则应该是 $(function 而不是 $j(function

除此之外,这里有一些针对您的代码的提示:

步骤 1:将所有“名称”字段重命名为您希望它们使用的名称例如,将 input[name=from] 更改为名称“order_from”

第 2 步:
使用此代码。
$j(函数(){
$j(".submit").click(function() {
var hasError = false;
如果(有错误==假){
var dataString = $j('form').serialize();

            $j.ajax({ type: "GET", cache: false, url: "/gc_process.php?uu="+Math.random(), data: dataString, success: function() { } });

        } else {

            return false;

        }
    });

});
你会注意到我在 url 上添加了一个随机变量“uu=random”,这通常是 jquery 的内置函数,但为了确保它不会缓存响应,你可以使用此方法强制它。

祝你好运。如果这不起作用,请尝试该脚本,而无需在新页面上重命名 jquery。看看这是否有效,该脚本与页面上的其他脚本之间可能会发生一些冲突

Mate,

Have you enclosed your jquery in

$j(function(){


});

To make sure it is only executed when the dom is ready?

Also, I'm assuming that you've manually gone and renamed jquery from "$" to "$j" to prevent namespace conflicts. If that isn't the case it should be $(function and not $j(function

Anyway apart from that, here are some tips for your code:

Step 1: rename all the "name" fields to be the name you want them to be in your "dataString" object. For example change input[name=from] to have the name "order_from"

Step 2:
Use this code.
$j(function(){
$j(".submit").click(function() {
var hasError = false;
if(hasError == false) {
var dataString = $j('form').serialize();

            $j.ajax({ type: "GET", cache: false, url: "/gc_process.php?uu="+Math.random(), data: dataString, success: function() { } });

        } else {

            return false;

        }
    });

});
You'll notice i slapped a random variable "uu=random" on the url, this is generally a built in function to jquery, but to make sure it isn't caching the response you can force it using this method.

good luck. If that doesn't work, try the script without renaming jquery on a fresh page. See if that works, you might have some collisions between that and other scripts on the page

小巷里的女流氓 2024-10-13 08:08:55

事实证明,问题是由于我使用 iframe 造成的。我通过制作没有 iframe 的页面来解决这个问题。感谢大家的帮助!

Turns out the problem is due to the fact that I am using iframes. I was able to fix the problem by making the page without iframes. Thanks for your help all!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文