简体   繁体   English

发布表单并将结果放入div中

[英]Post a form and put results in a div

I can't get my form to submit correctly using jquery. 我无法使用jquery正确提交表单。 I have tried using the tips here: https://api.jquery.com/jQuery.post/ and http://api.jquery.com/submit/ but can't figure out what I am missing to have the form submit and return the results to the div instead of reloading the page. 我尝试过使用这里的提示: https//api.jquery.com/jQuery.post/http://api.jquery.com/submit/但无法弄清楚我缺少什么表格提交并将结果返回到div而不是重新加载页面。 It is supposed to call an external php page to get processed and return the results. 它应该调用外部php页面进行处理并返回结果。

<script>
    // On click "button.submit"
$("input[id^=formsubmit]").submit(function( event ) {
     // Stop form from submitting normally
    event.preventDefault();
   $.get( "functions.php?do=adminmenu", function( data ) {
    $( \".contentarea\" ).html( data );
    });
    return false;
});
</script>

<form action="#" name="submitForm">
<input type="textbox" name="test">
<input type="submit" value="Save Settings" id="formsubmit">
</form>
<div class="contentarea"></div>

Try to wrap your code inside DOM ready handler $(function() {...}); 尝试将代码包装在DOM ready handler $(function() {...}); to make sure your DOM elements have been properly loaded. 确保您的DOM元素已正确加载。

Also, you can remove return false here as well as there's no need to escape the $(".contentarea" ) using \\ . 此外,您可以在此处删除return false ,并且无需使用\\来转义$(".contentarea" ) So try this code: 所以尝试这个代码:

$(function () {
    $("input[id^=formsubmit]").submit(function (event) {
        // Stop form from submitting normally
        event.preventDefault();
        $.get("functions.php?do=adminmenu", function (data) {
            $(".contentarea").html(data);
        });
    });
});

The problem is that you're not passing the data to the php file. 问题是你没有将数据传递给php文件。 Furthermore you have not specified the type of data handled. 此外,您尚未指定处理的数据类型。 And if you want to use POST you should use $.post(); 如果你想使用POST,你应该使用$ .post(); and not $.get(); 而不是$ .get();

Try it like this: I've changed the function from .get to .post, added the data you want to send to the php file and specified a data type. 试试这样:我已经将函数从.get更改为.post,添加了要发送到php文件的数据并指定了数据类型。

<script>
    $(document).ready(function () {
        $('#formsubmit').click(function () {
            $.post("functions.php?do=adminmenu",{test_val:$('#test_input').value},function (data) {
                $('.contentarea').html(data);
            },"json");
        });
    });
</script>

<input id="test_input" type="text" value="">
<input id="formsubmit" type="submit" value="Save Settings">
<div class="contentarea"></div>

Last point: What is the php file returning? 最后一点:什么是php文件返回? Is it only a string or what? 它只是一个字符串或什么? Depending on this you need to modify the function writing the returned content in the '.contentarea'. 根据这一点,您需要修改在'.contentarea'中编写返回内容的函数。

And by the way: When submitting the information via AJAX you don't need a form around it, as it just creates the need to escape the default behaviour when submitting a form. 顺便说一句:当通过AJAX提交信息时,您不需要围绕它的表单,因为它只是在提交表单时创建了逃避默认行为的需要。

If it still doesn't work let me know, I'll help you. 如果它仍然无法使用,请告诉我,我会帮助你。

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

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