Complex IF's

CannonBallGuyCannonBallGuy ModeratorShared Hoster
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

  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    cbg, there are a couple of ways to do this:

    *****************************

    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.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Maybe what you said just went over my head...
    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++.
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    ah... ok... there's no straight answer for that...

    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)
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Hmm ok.
    How do I check that a field has a certain character in it?
    ie. make sure the email field has an "@" .
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    :) you really need to go to a site like php.net to check out the syntax - even better if you get a good book!

    anyway, the function you would need is:
    if(stristr($email, "@") == FALSE) {
       //do something if character @ is NOT found
    }
    else {
       //do something if character @ is found
    }
    
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Okay, thanks daboss. :)
  • martian2k4martian2k4 Llama Hunter Moderator
    There is a easyer way to lay it out BTW cbg,

    [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 :p :D
    Free MySpace Layouts- Coming soon
    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
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Firstly, why do you need to use
    [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...
  • martian2k4martian2k4 Llama Hunter Moderator
    You don't really need to show which one they forgot, just in the HTML form if you put in the text fields <?php echo $_POST["email"] ?> for the email one and name a message for the other two then it will show what they have entered and leave the field they havn't blank
    Free MySpace Layouts- Coming soon
    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
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Hmm. maybe.
    But whats the deal with all the "$_POST["name"]" things.
    Why not just use $name?
  • martian2k4martian2k4 Llama Hunter Moderator
    Ohh, i think that different versions of PHP effect it :D
    Free MySpace Layouts- Coming soon
    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
  • dabossdaboss WP V.I.P. ''The Boss'' Administrator
    cbg, to answer your question:

    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...
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Bugger.

    Thanks guys. :/
  • martian2k4martian2k4 Llama Hunter Moderator
    daboss wrote:
    cbg, to answer your question:

    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...

    Tld ya it was to o wit the versions ;) Thanks Daboss
    Free MySpace Layouts- Coming soon
    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
  • ArestiaArestia Member NAT Warrior
    this is what i use:

    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.
    <script type="text/javascript">
    <!--
    function validate() {
    if (document.FORMNAME.FIELDNAME.value.length > 0) {
    	var checkEmail = "@.";
    	var checkStr = FORMNAME.FIELDNAME.value;
    	var EmailValid = false;
    	var EmailAt = false;
    	var EmailPeriod = false;
    	for (i = 0;  i < checkStr.length;  i++)
    	{
    	ch = checkStr.charAt(i);
    	for (j = 0;  j < checkEmail.length;  j++)
    	{
    	if (ch == checkEmail.charAt(j) && ch == "@")
    	EmailAt = true;
    	if (ch == checkEmail.charAt(j) && ch == ".")
    	EmailPeriod = true;
    		  if (EmailAt && EmailPeriod)
    			break;
    		  if (j == checkEmail.length)
    			break;
    		}
    		// if both the @ and . were in the string
    	if (EmailAt && EmailPeriod)
    	{
    			EmailValid = true
    			break;
    		}
    	}
    	if (!EmailValid)
    	{
    	alert("The \"email\" field must contain an \"@\" and a \".\".");
    	FORMNAME.FIELDNAME.focus();
    	return (false);
    	}
    }
    return true;
    }
    // -->
    </script>
    

    and then in the <form> tag add the onsubmit action simular to this:
    <form name="FORMNAME" action="file.php" method="post" onsubmit="return validate();">
    

    should work
    Arestia Design Studios / Synapse Corporate Solutions
    Daniel L. Rust - Lead Architect
    Seattle, WA
  • ChroderChroder Senior Member The Royal RAM
    For the record, the varnames without using superglobals ($_POST, $_GET etc) will still work as long as register_globals is enabled in php.ini. It has been off by default since 4.3.0, I believe, so it's nothing new in PHP5. A lot of hosts re-enable it for backwards compatibility.
  • CannonBallGuyCannonBallGuy Moderator Shared Hoster
    Arestia: That seems kinda long winded. I think the stristr will be ok and is much easier.
    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.
  • ArestiaArestia Member NAT Warrior
    what i listed checks for a valid email adddress before it submits and then tells them to enter a valid emaill address
    Arestia Design Studios / Synapse Corporate Solutions
    Daniel L. Rust - Lead Architect
    Seattle, WA
  • ChroderChroder Senior Member The Royal RAM
    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.
    It's something your host will have to do. What I mean is that, if your host has it enabled now then it is likely they will keep it enabled when they upgrade to PHP5 (which will probably be a long way off anyway).

    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.
    if(!emailStr.match(/^.+@[^\.].*\.[a-z]{2,}$/))
        return false
    
Sign In or Register to comment.