Working with registration form. I need all the input remain if the form can not process due to missing field, or email and username provided are unvailable.
| SitePoint Sponsor |




Working with registration form. I need all the input remain if the form can not process due to missing field, or email and username provided are unvailable.


How are you doing your vaildation?
If you are doing javascript validation then you would never have left the page and thus the information would still be there.
If you are doing some kind of server side validation (like php), then all your form variables would be in the $_GET or $_POST super global arrays, and you can thus access you variables like such
The above assume you use the POST method on your form, and had a variable called name, and you stuck it into a variable called $name_var.Code:$name_var = $_POST['name'];
You can then use that variable to populate the form, like so
Hope this helpsCode:<form method="post" action="$_SERVER['php_self']"> <input type="name" value="<?php echo (isset($name_var) ? $name_var : ''); ?>" /> <input type="submit" /> </form>![]()
type="name"???HTML Code:<input type="name" value="<?php echo (isset($name_var) ? $name_var : ''); ?>" />
Is this a valid type in HTML form?
I would do like this:
- Submit the form to the same page.
Suppose I have a page mypage.php
Hope you understand what I am doing.PHP Code:<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(!empty($_POST['my_name']) && !empty($_POST['my_address'])){
# process your code further
# redirect to some page
}
# else the page will remain posted.
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta name="author" content="3261706071">
<title>Test</title>
</head>
<body>
<form action="" method="post" name="frmreg">
Name:<input type="text" name="my_name" id="my_name" value="<?php echo $_POST['my_name'];?>" />
Address:<input type="text" name="my_address" id="my_address" value="<?php echo $_POST['my_address'];?>" />
<input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" />
</form>
</body>
</html>


Dope, my bad, well picked up there
This
should be thisCode:<input type="name" value="<?php echo (isset($name_var) ? $name_var : ''); ?>" />
.Code:<input type="text" name="name" value="<?php echo (isset($name_var) ? $name_var : ''); ?>" />
You have implemented the logic correctly, it should work fine![]()




Code:<form method="post" action="$_SERVER['php_self']">
What does this $_SERVER['php_self'] do ?






Its a variable that holds the location of the current page (to sum it up)What does this $_SERVER['php_self'] do ?
![]()
Make sure to run $_SERVER['PHP_SELF'] through htmlspecialchars() otherwise you risk xss attacks. Same goes for when you echo the variables back into the form fields.




ok, when i put that in form action, run the page. An error message "$_SERVER['PHP_SELF'] is not available in your system or site" something like that.
I will try again and get the correct message post here




It's a PHP variable so you need to wrap it in PHP tags:
Code:<form method="post" action="<?php echo $_SERVER['php_self']; ?>">
Pawel Decowski (you should follow me on Twitter)




Pawel Decowski (you should follow me on Twitter)




It's not defined by HTML specification thus it cannot be guaranteed that every browser will behave like this. Of course it may work but we should adhere to standards. As to why validator doesn't issue an error (or warning) -- that's a question for the W3C. I can only speculate that the reason is that the standard doesn't specifically require a URI, it only says that "behavior for a value other than an HTTP URI is undefined". In other words HTML specification doesn't say what the browser should do when the value isn't a URI.
http://www.w3.org/TR/html401/interac...ml#adef-action
EDIT: Actualy HTML4 specification does require a URI as a value of action attribute.
Pawel Decowski (you should follow me on Twitter)
According to the HTML specification, the data type for the action attribute is uri, and the Basic HTML data types page then says that the specification uses the term URI as defined in RFC 2396. Here's a quote from the RFC:
4.2. Same-document References
A URI reference that does not contain a URI is a reference to the current document. In other words, an empty URI reference within a document is interpreted as a reference to the start of that document, and a reference containing only a fragment identifier is a reference to the identified fragment of that document. Traversal of such a reference should not result in an additional retrieval action. However, if the URI reference occurs in a context that is always intended to result in a new request, as in the case of HTML's FORM element, then an empty URI reference represents the base URI of the current document and should be replaced by that URI when transformed into a request.
int main()
{
// No pun indented.
}
Although it has to be there, in XHTML Strict validation anyway (and who ISN'T using at least XHTML transitional by now?)
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona




It's clear that HTML4 specification does require action attribute to be a URI. It's too bad the specification is so ambiguous. Stating that "behavior for a value other than an HTTP URI is undefined" doesn't help at all. But it's been a long known fact that HTML specification is full of such ambiguous statements.
EDIT: HTML5 is looking better, but it's still missing loads of points I would like to see clearly specified.Disabled attribute for option elements being at the top of my list. It seems the specification has changed since I last checked. I'm really glad.
Pawel Decowski (you should follow me on Twitter)




Pawel Decowski (you should follow me on Twitter)




how about doing this with select and radio ?




append chosen radio option and select option.
Basically, in previous discussion we echo the data to the value attribute of text field/input to append it.
But radio button and menu/list need to have their own value at all time, so I don't think of how could I append it.

Yeh, I've started using HTML strict again recently. XHTML isn't supported by IE, and I've been swayed by the arguments on here (I like the XHTML syntax, but HTML is more practical / correct until the problems with XHTML are sorted out.. and even then it won't be suitable for a lot of applications). I may convert some of my existing sites to HTML strict as well.
Bookmarks