I'm creating my first website at the moment, it's a simple one page site with a contact form. Once the form has been submitted, I don't want to reload the page, I just want a confirmation message to appear underneath the Send button. I'm using the mail code from http://www.w3schools.com/php/php_secure_mail.asp, but as soon as the form is submitted it goes to a completely blank page and puts the confirmation message there. How do I stop this happening?
If you want the form to submit without reloading then you'll have to do it with Ajax (not recommended, not all visitors will have Javascript available/enabled). The alternative is to put the form and the mail sending PHP in the same script file and have a check to see whether or not the form was submitted and a mail sent. If it wasn't, then show the form, if it was then show the confirmation message. You can improve upon this by showing the form with fields already filled out where an error occurred (i.e. the user didn't fill in all required fields, etc).
Thanks for the cautionary advice on w3schools, noted.
So if I put the html form code in the file with the php script, I'd then call that script in the html file where the form had previously been? (sorry, I've been teaching myself html/css, now delving into javascript and php. I aim to avoid js with this project tho)
Go with your second, I'm not sure what you mean in the first!
The action of the form can either be left blank, or changed to index.php (assuming that is the name you use for the file). A blank action by default will mean the form submits to itself (I've yet to see a browser which doesn't do this). Then, in your script you can check to see if it's been submitted or not and display the right output with a line like this:
PHP Code:
if(isset($_REQUEST['submit'])) { // process user submission } else { // show form }
Where 'submit' is the name of a form field. Typically I name my submit buttons 'submit', but any form field will do the trick.
You can't avoid using client-side languages while simultaneously wanting code that changes client-side behavior. Pick one or the other, because this:
Originally Posted by rewind
Once the form has been submitted, I don't want to reload the page, I just want a confirmation message to appear underneath the Send button.
is not possible using only HTML with no Javascript.
Originally Posted by Ashley Sheridan
A blank action by default will mean the form submits to itself (I've yet to see a browser which doesn't do this)
... which isn't a very good justification to throw caution to the wind and start ignoring established standards. Well... it isn't unless you're hoping to work for Microsoft, I suppose.
Ah yes, didn't realise it was so out of date. I somehow doubt that browsers will start following the spec on this one though, as it would break too many websites, and no browser wants to be the one that is technically right but "doesn't work" for people using it.
However under HTML 4 the current specification says that an empty action attribute resolves to the document's base URL (likewise for other attribute values that are supposed to be interpreted as URLs). Unless otherwise stated the document's base URL is the URL of the document.
In other words, in HTML5, the default value for a form's action attribute is one that cannot be specified explicitly!
(Apparently, action="." also works, but as far as I can make out, by rights it should , given a base of http://www.example.com/a/b/form.php, resolve into http://www.example.com/a/b/ and that's not what you want....)
Last edited by Weedpacket; 11-02-2012 at 12:16 AM.
Thank you everyone, I think I'm on the right path now.
Originally Posted by bradgrafelman
You can't avoid using client-side languages while simultaneously wanting code that changes client-side behavior. Pick one or the other, because this:
is not possible using only HTML with no Javascript.
In other words, in HTML5, the default value for a form's action attribute is one that cannot be specified explicitly!
Actually, the default value is the URL of the current document, which you will get if you omit the action attribute. But you can of course still specify the current URL explicitly as well.
Actually, the default value is the URL of the current document, which you will get if you omit the action attribute. But you can of course still specify the current URL explicitly as well.
What I meant by that is more accurately phrased (emphasis mine):
The action of an element is the value of the element's formaction attribute, if the element is a submit button and has such an attribute, or the value of its form owner's action attribute, if it has one, or else the empty string.
The action IDL attribute must reflect the content attribute of the same name, except that on getting, when the content attribute is missing or its value is the empty string, the document's address must be returned instead.
There are two "attributes" here - you speak of the IDL interface attribute, I speak of the HTML attribute. (I can guess why the two have different "defaults" - the HTML attribute isn't expected to be aware of information that is not intrinsically part of the document.)
Which still doesn't explain why action="" is deemed invalid, since it implies right there that the attribute can have an empty string for a value (it's a consequence of a more general rule).
Last edited by Weedpacket; 11-02-2012 at 10:41 PM.
Bookmarks