Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2013, 01:52 AM   PM User | #1
njfail
Regular Coder

 
Join Date: Aug 2010
Posts: 125
Thanks: 2
Thanked 0 Times in 0 Posts
njfail is an unknown quantity at this point
validating a form, can it have 2 values?

Hey guys, I'm just learning PHP, and I'm trying to validate a form.
The problem I'm having, is with value=

Here is a snipit of the form code as an example:
Code:
<label>Full Name
<span class="small">First Name Last Name</span>
</label>
<input onfocus="this.value=''" type="text" value="John Smith" name="name" id="name" />
I have the value set as an example of the field, and it disappears when the user clicks the field.
But the validation tutorial I'm following is telling me to set the value as
Code:
value="<?=$varname;?>
I believe that this prevents the individual from having to re-enter their information if they forgot a field, thus triggering the validation/error message and reloading the page.

How can I have the 'example' of the field and the var so that people don't have to re-enter their information when the form is validated?

Please give me an example of the correct code, as I'm still learning

Thanks!

Last edited by njfail; 01-27-2013 at 02:08 AM..
njfail is offline   Reply With Quote
Old 01-27-2013, 03:12 AM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Quote:
How can I have the 'example' of the field and the var so that people don't have to re-enter their information when the form is validated?
I don't understand that sentence.

Rather than using onfocus() and relying of JavaScript - it's annoying anyway to have the value removed each time (suppose I just want to correct the spelling?) - use HTML5 placeholder:

PHP Code:
<input type="text" value="<?php echo $varname?>" name="name" id="name" placeholder="John Smith" />
If you are using the HTML5 doctype then you don't need ' /' at the end either.

It is also advisable to always use the full php opening tag <?php and echo the value.

You'll notice that if you delete the value and click away from the input the placeholder text will show. However, placeholder text should say something useful such as "Name is required" or "Minimum 5 characters" rather than an actual name, as this is mis-leading.

BTW I think name is a very poor name for the name. Use a little imagination; even 'fullname' is better.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 01-27-2013 at 03:16 AM..
AndrewGSW is offline   Reply With Quote
Old 01-27-2013, 03:51 AM   PM User | #3
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,461
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Code:
<?php 
$fullname = '';
if (isset($_POST['fullname'])) $fullname = validateFullName($_POST['fullname']);
?>

<label for="fullname">Full Name
<span class="small">First Name Last Name</span>
</label>
<input type="text" value="<?php echo $fullname ?>" name="fullname" id="fullname"/>
If you are serving the (X)HTML5 as XHTML then you need the closing / but if you are serving it as HTML then you don't.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 01-27-2013 at 03:53 AM..
felgall is online now   Reply With Quote
Old 01-27-2013, 11:50 PM   PM User | #4
njfail
Regular Coder

 
Join Date: Aug 2010
Posts: 125
Thanks: 2
Thanked 0 Times in 0 Posts
njfail is an unknown quantity at this point
Thanks for the advice!
I was unaware of the placeholder attribute

Can I ask why name is a bad name? I understand it is simple/common but is there a reason that is bad?

I'm not very familiar with doctypes. My knowledge of html/css/php is mainly from following tutorials and taking advice from random pages online. People have told me to just use this, so its what I use
Code:
<!DOCTYPE html>
njfail is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:06 AM.


Advertisement
Log in to turn off these ads.