CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   validating a form, can it have 2 values? (http://www.codingforums.com/showthread.php?t=286506)

njfail 01-27-2013 01:52 AM

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!

AndrewGSW 01-27-2013 03:12 AM

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.

felgall 01-27-2013 03:51 AM

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.

njfail 01-27-2013 11:50 PM

Thanks for the advice!
I was unaware of the placeholder attribute :o

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 :D
Code:

<!DOCTYPE html>


All times are GMT +1. The time now is 07:36 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.