I want to use an IF to make sure that an email address was entered in the email field of a form.
Here is what I have so far:
FORM
<body>
<form action="book2.php" method="get">
<input type="text" name="name" value="Your Name" />
<input type="text" name="email" value="Your eMail" />
<input type="text" name="message" value="Your Message" />
<input type="submit" name="SUBMIT" value="Go" />
<input type="reset" name="RESET" value="Clear" />
</form>
HANDLER
[php]<?php
if ($name == "") {
print
"Please enter a name:
<form action=\"book2.php\" method=\"get\">
<input type=\"text\" name=\"name\" value=\"Your Name\" />
<input type=\"text\" name=\"email\" value=\"Your eMail\" />
<input type=\"text\" name=\"message\" value=\"Your Message\" />
<input type=\"submit\" name=\"SUBMIT\" value=\"Go\" />
<input type=\"reset\" name=\"RESET\" value=\"Clear\" />
</form>
";
} elseif ($email == "") {
print
"Please enter an email address:
<form action=\"book2.php\" method=\"get\">
<input type=\"text\" name=\"name\" value=\"Your Name\" />
<input type=\"text\" name=\"email\" value=\"Your eMail\" />
<input type=\"text\" name=\"message\" value=\"Your Message\" />
<input type=\"submit\" name=\"SUBMIT\" value=\"Go\" />
<input type=\"reset\" name=\"RESET\" value=\"Clear\" />
</form>
";
} elseif ($message == "") {
print
"Please enter a message:
<form action=\"book2.php\" method=\"get\">
<input type=\"text\" name=\"name\" value=\"Your Name\" />
<input type=\"text\" name=\"email\" value=\"Your eMail\" />
<input type=\"text\" name=\"message\" value=\"Your Message\" />
<input type=\"submit\" name=\"SUBMIT\" value=\"Go\" />
<input type=\"reset\" name=\"RESET\" value=\"Clear\" />
</form>
";
} else {
print
"You said: <br />
Name: $name <br />
eMail: $email <br />
and: $message <br />
";}
?>[/PHP]
(I just realised I could have defined the code for the form as a string and just called that after the if's... nevermind. lol)
So, I have made it that if there is nothing in one of the fields, it tells you to put it in and prints the form again.
But this doesnt stop people putting normal text in the email field. I want to somehow make sure they have entered an email address.
Any help appreciated, but please don't just do it for me, i'm trying to learn.
BTW: How is this for a first script? :P
Comments
*****************************
the easiest way would be to print a link back to the form after the message - i.e. when the user reads the error message, he has to click on the link to get back to the form to try again.
*****************************
the most elegant way of doing this is to use javascript on the form to check if the user has filled in all fields. if not, pop up a message and don't go to the php handler
*****************************
if you don't want javascript and only want php, then what you can do is to have both the form and the handler in a same file - e.g. book.php.
1. use a variable (e.g. named $shown) to keep track if the form has been shown.
2. this variable needs to be used as a hidden field in the form with a value of true. the form's action is the file itself (in our example, book.php)
3. in book.php, have the handler code exist before the form
4. if $shown==false, bypass the handler code and display the form
5. the user fills in the form and clicks on submit. the form sets the hidden field $shown to true and calls book.php again
6. this time, since $shown==true, do not bypass the handler code. the handler code will print the error mesage. the form gets displayed as usual.
But it seems to me you'r trying to help me make sure the fields are entered.
I have already done that.
Try the script yourself, if you don't fill in a field on the original page, the handler says so, then prints the form out again, below. If you still don't do it, it keeps repeating the process untill every field has something in it.
But what I want to do now is make sure that the user not only enteres data but also that it is the correct type of data. For example, I want to make sure they ahve put an email address in the email field. As it is now, they could say "Boom" is their email address, when clearly it is not.
It would not surprise me if this is very very difficult to do... But I would like to have a go anyway. After this wednesday coming, I have about 10 weeks in which I have all day, every day, to learn PHP and C++.
what you have to do is to think of what to check and then check individual field values...
e.g. age is stored in a $age field. you'd hae to test using php's functions to determine if $age is numeric, if $age > 0 and $age < 120. the function to use would be the following:
bool is_numeric ( mixed var)
How do I check that a field has a certain character in it?
ie. make sure the email field has an "@" .
anyway, the function you would need is:
[PHP]<?php
if ( $_POST["submit"] ) {
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
if ( stristr ($email, "@) == FALSE) {
?>
HTML HERE, EMAIL NOT CONTAINING A @ SYMBOL
<?php
}
if ( (!$name) || (!$email) || (!$message) ) {
?>
HTML HERE, ALL REQUIRED FIELDS WERE NOT ENTERED
<?php
}
}else{
?>
HTML FORM
<?php
}
?>
[/PHP]
There are probable a few errors but i am very tired
Photoshop Tutorials- Coming soon
Premium PHP Scripts- Coming soon
Haha i should really do some work so i can remove all the coming soon's
[php]if ( $_POST["submit"] ) {
$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];[/php]
I haven't in my script and $name, $email and $message all work fine for me.
Secondly you just seem to check that they all exist mine does it individually to show which one the user forgot to enter...
Photoshop Tutorials- Coming soon
Premium PHP Scripts- Coming soon
Haha i should really do some work so i can remove all the coming soon's
But whats the deal with all the "$_POST["name"]" things.
Why not just use $name?
Photoshop Tutorials- Coming soon
Premium PHP Scripts- Coming soon
Haha i should really do some work so i can remove all the coming soon's
the way you've coded it (i.e. directly using $email instead of $email=$_POST["email"]) won't work in php 5. in php 5, the values from one form to another will need to be retrieved through wither a $_POST or $_GET...
Thanks guys.
Tld ya it was to o wit the versions Thanks Daboss
Photoshop Tutorials- Coming soon
Premium PHP Scripts- Coming soon
Haha i should really do some work so i can remove all the coming soon's
this somewhere in the <head>. change the FORMNAME to the name you applied to the <form> and change the FIELDNAME to the emaill address field.
and then in the <form> tag add the onsubmit action simular to this:
should work
Daniel L. Rust - Lead Architect
Seattle, WA
Webmaster-Talk.com
Chroder.com
Thanks anyway.
Chroder: Is it likely that I will have access to the php.ini file in the future, or is it something only my host can do? Thanks.
Daniel L. Rust - Lead Architect
Seattle, WA
But that doesn't mean you shouldn't use the superglobal arrays instead of relying on register_globals. Relying on register_globals can bring up security issues in the long run, and it's always best to make sure your script will work on different servers (ie: if another server has register_globals disabled).
@Arestia: That is a client side form of validation, which is dreadfully easy to circumvent. Client-side validation should never be relied upon; thus, CBG would need a server-side solution anyway. Unless CBG wants some nifty real-time error messages, there is no use in using two methods. Though I think you overcomplicated it, you could use a simple regular expression to check the email, you don't need to loop through each character.
Webmaster-Talk.com
Chroder.com