提交操作而不刷新或重定向页面[重复]
This question already has an answer here:
- jQuery Ajax POST example with PHP 13 answers
So, I have a contact form on one page (let's call it send_now.php
or example.com/send
)
Once the form is filled out and submitted, then an email is sent to a certain user while the page is directed to example.com/it_is_sent
page which contains a sent confirmation based on confirmation.php
.
I would like to know how to change it so that everything is done on example.com/send/
page without refreshing or redirecting the user to the next page.
Here is what I mean.
So, in /send/
page, an user fills out the form and click send. Then without redirect the user to /confirmation/
page, the confirmation is shown on /send/
page without redirecting the user, so everything happens within the same page.
Is there a way to do that? what is the general concept of doing things like that? or, can the form be submitted within the same page without refreshing the page?
Thanks!
</div>
此问题已经存在 这里有一个答案: p>
-
带有PHP的jQuery Ajax POST示例
13 answers
span>
\ n li>
ul>
div>
所以,我在一个页面上有一个联系表格(我们称之为
send_now.php 代码>或
example.com/send code>) p>
填写并提交表单后,会在页面被定向时向特定用户发送电子邮件 到
example.com/it_is_sent code>页面,其中包含基于
confirmation.php code>的已发送确认。 p>
我想知道如何 更改它,以便在
example.com/send / code>页面上完成所有操作,而无需刷新或重定向 用户到下一页。 p>
这就是我的意思。 p>
因此,在
/ send / code>页面中, 用户填写表单并单击“发送”。 然后,无需将用户重定向到
/ confirmation / code>页面,确认将显示在
/ send / code>页面上,而不会重定向用户,因此所有内容都在同一页面内发生。 p >
有没有办法做到这一点? 这样做的一般概念是什么? ot,是否可以在不刷新页面的情况下在同一页面中提交表单? p>
谢谢! p> div>
Take a quick search around the net for "jquery ajax form submit". The term I think you're looking for is Ajax. It is what allows you to have JavaScript send off data to a PHP script without refreshing the page.
You build your form like normal, and attach a jQuery click event to the form or submit button. The jQuery/Ajax function takes the data from the form and sends it over GET or POST to your PHP form.
Whatever your PHP script outputs is received by your jQuery/Ajax function. I like to use json_encode on a PHP Array for the PHP script output. In JavaScript I can then easily work with the results as an array of values.
Depending on what's in the Array or output depends on how your JavaScript should react. Output could be as simple as a 0 or 1, true or false, or a json Array or values like I usually do. I'll usually include at least error=true/false
.
You could have the PHP script output be displayed in a Div once the Ajax success function fires.
You could also use jQuery load() to load another page into a Div upon success. The possibilities are endless when you combine it all.
You can easily find code samples for this all over StackOverflow and tutorials on the rest of the Internet. You're looking for "jQuery Ajax Form Submit to PHP", maybe even with MySQL?
This technique makes buttons that make instant changes possible. Once you're done with this project, look into websockets if you really want to see how instant the web can be.