简体   繁体   English

如何提交表单而不用HTML重新加载整个页面?

[英]How to submit a form without reloading the whole page in HTML?

I have a simple website, not a PHP site. 我有一个简单的网站,而不是PHP网站。 i am using a free host. 我正在使用免费主机。 I want to post data on "blog_1.xhtml" page. 我想在“ blog_1.xhtml”页面上发布数据。

the form is given below : 表格如下:

<form method="post" action="blog_1.xhtml">
    <input type="hidden" name="fnc" value="new"/>    
    <input type="hidden" name="blog_title" value="USERS STATUS"/>
    <input type="text" name="blog_body" value=""/>
    <input type="submit" name="submit" value="Post"/>
</form>

Now i want to post the data without reloading the whole page. 现在,我想发布数据而无需重新加载整个页面。 Can anyone help me? 谁能帮我?

Thanks in advance :) 提前致谢 :)

Assuming that you have JQUERY allowed: 假设您已允许JQUERY:

<script>
    var p=$("form").serialize();
    $.post("/blog_1.xhtml",p,function(data){
        alert('result html:' + data);
        });
</script>

You kind find more information and example solutions here: http://api.jquery.com/jquery.post/ . 您可以在此处找到更多信息和示例解决方案: http : //api.jquery.com/jquery.post/

You can alos stop the form from submitting normally like this: 您可以像这样阻止表单正常提交:

// Stop form from submitting normally
event.preventDefault();

POST data is data that is handled server side . POST数据是在服务器端处理的数据 And Javascript is on client side. Javascript位于客户端。 So there is no way you can read a post data using JavaScript. 因此,您无法使用JavaScript读取帖子数据。

You can't do this using forms the normal way. 您不能使用表格的正常方式进行此操作。 Instead, you want to use AJAX while working with some server side language. 相反,您想在使用某些服务器端语言时使用AJAX。 A sample function that will submit the data and alert the page response. 一个示例函数,它将提交数据并警告页面响应。

<form action="" method="get" onsubmit="return submitForm();">
   <input name="search" type="text">
   <input type="submit" value="Search" >
 </form>
<script>
function submitForm() {
        var http = new XMLHttpRequest();
        http.open("POST", "<<whereverTheFormIsGoing>>", true);
        http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        var params = "search=" + <<get search value>>; // probably use document.getElementById(...).value
        http.send(params);
        http.onload = function() {
            alert(http.responseText);
        }
    }
</script>

OR use the GET method to send data and then on the other page retrieve the values from URL. 使用GET方法发送数据,然后在另一页上从URL检索值。 But the user will definitely be redirected in this case. 但是在这种情况下,肯定会重定向用户。

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

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