简体   繁体   English

防止在表单提交后重定向到php文件

[英]prevent redirect to php file after form submit

After researching this for too many hours I finally decided to post the question myself, as what is working for others, doesn't seem to work for me. 在研究了多个小时之后,我终于决定自己发布问题,因为对别人有用的东西似乎对我没用。 Please keep in mind that I am fairly new to ajax and jquery, but as I am on a deadline with my current project I wont have time to go through it all. 请记住,我对ajax和jquery还是比较陌生,但是由于我正处于当前项目的最后期限,所以我没有时间进行全部研究。

I have the following html form: 我有以下HTML表单:

<div class="form">
    <form id="savePlacemarkForm" method="post" action="createPlacemark.php">
        <div>
            <input type="text" id="placemarkName" name="placemarkName" placeholder="Name:"/>
        </div>
        <div>
            <input type="text" id="placemarkAddress" name="placemarkAddress" placeholder="Adress:"/>
        </div>
        <div>
            <input type="text" id="placemarkTag" name="placemarkTag" placeholder="Tags:"/>
        </div>
        <div>
            <textarea id="placemarkDescription" name="placemarkDescription" placeholder="Description" rows="1" cols="1"></textarea>
        </div>
        <div>
            <input id="latitude" type="text" name="latitude"/>
        </div>
        <div>
            <input id="longtitude" type="text" name="longtitude"/>
        </div>
        <button class="md-close" id="savePlacemark" onclick="createPlacemark();"/>Save</button>
    </form>
    <button class="md-close">Cancel</button>
    <script src="my_script.js" type="text/javascript"></script>
</div>

As you see I have the action set to createPlacemark.php which takes input from these fields and saves it to my DB, which works fine!! 如您所见,我将动作设置为createPlacemark.php,该动作将从这些字段中获取输入并将其保存到我的数据库中,效果很好! However since this should work without redirecting or resubmitting the page, meaning ajax! 但是,由于这应该工作而无需重定向或重新提交页面,因此意味着ajax! I include my_script.js which looks like this: 我包含如下所示的my_script.js:

$("#savePlacemark").click( function() {
    $.post( $("#savePlacemarkForm").attr("action"), 
        $("#savePlacemarkForm :input").serializeArray();                     
    });
    clearInput();
});

$("#savePlacemarkForm").submit( function() {
    return false;   
});

function clearInput() {
    $("#savePlacemarkForm :input").each( function() {
        $(this).val('');
    });
}   

As you see it does the post for me, which works, but for some reason the return false; 如您所见,该帖子对我来说是有效的,但是由于某种原因,返回false; doesnt seem to work for me, as I am continuously redirected to the before mentioned php file. 似乎不为我工作,因为我不断重定向到前面提到的php文件。

Any help would be greatly appreciated! 任何帮助将不胜感激! thx! 谢谢!

Try the following. 尝试以下方法。 The .submit() function actually triggers a submit for the form. .submit()函数实际上触发了表单的提交。 Take this out of the script since you don't need it when you've already posted the values with $.post . 从脚本中删除它,因为当您已经用$.post发布值时就不需要它了。

$("#savePlacemark").click( function() {
 $.post( $("#savePlacemarkForm").attr("action"), 
         $("#savePlacemarkForm :input").serializeArray();                    
  });
  clearInput();
});

function clearInput() {
    $("#savePlacemarkForm :input").each( function() {
       $(this).val('');
    });
}   

try this 尝试这个

<button onclick="return createPlacemark();">Save</button>

it may help you. 它可能会帮助您。

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

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