简体   繁体   English

通过AJAX通过PHP发送表单,而无需刷新页面

[英]Sending form with PHP through AJAX with no page refresh

So I have this code to POST data with PHP and AJAX without redirecting page, I'm using the same script on the login page. 因此,我有这段代码可以使用PHP和AJAX来发布数据,而无需重定向页面,我在登录页面上使用了相同的脚本。 The login page works like a charm but the other pages don't. 登录页面的作用就像超级按钮,而其他页面则不然。 The only diffeence between these is that login php script page uses if (empty($_POST) === false) {} and the other pages use if (isset($_POST['save-settings'])) {} . 两者之间的唯一区别是登录php脚本页面使用if (empty($_POST) === false) {} ,其他页面使用if (isset($_POST['save-settings'])) {} I don't know what do to.. Here below is the script I'm using. 我不知道该怎么办。以下是我正在使用的脚本。

HTML BUTTON HTML按钮

<input id="save-settings" class="submit" type="submit" name="save-settings" value="Save" onclick="return false;" />

JS SCRIPT 脚本

$(document).ready(function() {
        $("#save-settings").click(function() {
            var name        = $("#webname").val();
            var charset     = $("#webchar").val();
            var meta        = $("#webmeta").val();
            var description = $("#webdesc").val();
            var startsite   = $("#webstartsite").val();
            var starturl    = $("#webstartsiteurl").val();
            var footer      = $("#webfooter").val();

            $.post("../lib/action.php", {
                name: name,
                charset: charset,
                meta: meta,
                description: description,
                startsite: startsite,
                starturl: starturl,
                footer: footer
            }, function(data) {
                $("#gy-main-notification-bar").hide().html("<h1>!</h1><h2>" + data + "</h2>").slideDown(500);
                setTimeout(function() { $("#gy-main-notification-bar").slideUp(500) }, 2500);
            });
        });
    });

PHP SCRIPT PHP脚本

if(isset($_POST['save-settings'])) {
    $updatesettings = "UPDATE `settings` SET
    `name`='".escape($_POST['webname'])."',
    `charset`='".escape($_POST['webchar'])."',
    `meta`='".escape($_POST['webmeta'])."',
    `description`='".escape($_POST['webdesc'])."',
    `startsite`='".escape($_POST['webstartsite'])."',
    `starturl`='".escape($_POST['webstartsiteurl'])."',
    `footer`='".escape($_POST['webfooter'])."'
     WHERE `id`= 1";

     if ($update_settings = $db_connect->query($updatesettings)) {}
     echo 'Success!';
 }

I don't really want to change the isset to empty in the script due the fact that I have all my "onclick" script in one action.php file. 由于我将所有"onclick"脚本都放在一个action.php文件中,因此我真的不想在脚本中将isset更改为empty When I remove onclick="return:false;" 当我删除onclick="return:false;" from input it works.. I'm so confused I appriciate any help! 从输入它的作品..我很困惑,我寻求任何帮助!

Click event handler function can have event argument. 单击事件处理程序函数可以具有事件参数。 When you catch this argument you can use preventDefault() method. 捕获此参数时,可以使用preventDefault()方法。 With this method default action of click will be prevented and page won't be refreshed. 使用此方法,将阻止默认的单击操作,并且不会刷新页面。

Change 更改

$("#save-settings").click(function() {
        var name        = $("#webname").val();

to

$("#save-settings").click(function(ev) {
        ev.preventDefault();
        var name        = $("#webname").val();

You Forgot to include the post save-settings. 您忘记了包括帖子保存设置。 You probably should've included it with the ajax post like this: 您可能应该将其包含在ajax帖子中,如下所示:

$.post("../lib/action.php", {
            'name': name,
            'charset': charset,
            'meta': meta,
            'save-settings': true,
            'description': description,
            'startsite': startsite,
            'starturl': starturl,
            'footer': footer
        },

change this in your sql statement for the correct posts 在您的sql语句中更改此内容以获取正确的帖子

 `name`='".escape($_POST['name'])."',
`charset`='".escape($_POST['charset'])."',
`meta`='".escape($_POST['meta'])."',
`description`='".escape($_POST['description'])."',
`startsite`='".escape($_POST['startsite'])."',
`starturl`='".escape($_POST['starturl'])."',
`footer`='".escape($_POST['footer'])."'
 WHERE `id`= 1";

writing onclick="return false" in HTML cancels the execution of the javascript code. 在HTML中编写onclick="return false"取消JavaScript代码的执行。 just delete this onclick="..." and add preventDefault() like this to prevent form submittion 只需删除此onclick="..."并添加诸如此类的preventDefault()即可阻止表单提交

$("#save-settings").click(function(e) {
   e.preventDefault();
   .....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM