2014-08-19 11:55
浏览 61


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. 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'])) {}. I don't know what do to.. Here below is the script I'm using.


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


$(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);


if(isset($_POST['save-settings'])) {
    $updatesettings = "UPDATE `settings` SET
     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. When I remove onclick="return:false;" from input it works.. I'm so confused I appriciate any help!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douyun3887 2014-08-19 11:58

    You Forgot to include the post save-settings. You probably should've included it with the ajax post like this:

    $.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

     WHERE `id`= 1";
    打赏 评论
  • dota220141003 2014-08-19 12:00

    Click event handler function can have event argument. When you catch this argument you can use preventDefault() method. With this method default action of click will be prevented and page won't be refreshed.


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


    $("#save-settings").click(function(ev) {
            var name        = $("#webname").val();
    打赏 评论
  • dongqiao3833 2014-08-19 12:05

    writing onclick="return false" in HTML cancels the execution of the javascript code. just delete this onclick="..." and add preventDefault() like this to prevent form submittion

    $("#save-settings").click(function(e) {
    打赏 评论

相关推荐 更多相似问题