#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    306
    Rep Power
    6

    Questions about stopping and beginning the next function


    I cannot figure out for nothing how you separate different commands or functions. Lets say I want to check to make sure my form had a topic entered and then once it passes it continues on to check the email field for injection. How do I do that? I did not copy my whole script here, just the end of one to the next.

    Thanks!

    if(!filter_has_var(INPUT_POST, "topic"))
    {
    echo("You did not enter a topic.");
    }
    else



    function spamcheck($field)


    Also, lets say i had a code to remove all html tags and inserted that after the spamcheck was complete. Will the form know to send the email with all these corrections or do I need to output something after each correction is made? I am completely confused and trying to learn this.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    PHP code is executed procedurally. All code that exists on its own line without being "inside" something is executed top-to-bottom.

    Code that is placed inside curly braces is considered a "block." Blocks of code are only executed if their condition is met. For instance, in your example, the echo will only be executed if the IF statement is true. You can have 50,000 lines inside that IF statement, and none of them will do anything if the IF is false.

    Similarly, FUNCTIONs are groups of statements that will only execute if something "calls" that function.

    I recommend a good PHP for beginners book, or the PHP manual itself.

    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    306
    Rep Power
    6
    Originally Posted by ManiacDan
    PHP code is executed procedurally. All code that exists on its own line without being "inside" something is executed top-to-bottom.

    Code that is placed inside curly braces is considered a "block." Blocks of code are only executed if their condition is met. For instance, in your example, the echo will only be executed if the IF statement is true. You can have 50,000 lines inside that IF statement, and none of them will do anything if the IF is false.

    Similarly, FUNCTIONs are groups of statements that will only execute if something "calls" that function.

    I recommend a good PHP for beginners book, or the PHP manual itself.

    -Dan
    Thank you. That does help to make better since of things.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    306
    Rep Power
    6
    Can you help me with this?

    Here is my code:

    <?php $topic = $_POST['topic'] ; //This checks the subject
    if($topic == '')
    if(!$topic)
    {
    echo "You have not entered a subject, please go back and enter a subject.";
    }
    $message = $_POST['message'] ; //This checks the message
    if($message == '')
    if(!$message)
    {
    echo "You have not entered a message, please go back and enter a message.";
    }
    function spamcheck($field) //filter_var() sanitizes the e-mail
    {
    $field=filter_var($field, FILTER_SANITIZE_EMAIL); //filter_var() validates the e-mail
    if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
    return TRUE;
    }
    else
    {
    return FALSE;
    }
    }
    if (isset($_REQUEST['email'])) //if "email" is filled out, proceed with sending
    //check if the email address is invalid
    $mailcheck = spamcheck($_REQUEST['email']);
    if ($mailcheck==FALSE)
    {
    echo "You have not entered a valid email, please press the back button and try again.";
    }
    else //send email if validation has passed
    {
    $topic = $_POST['topic'];
    $email = $_POST['email'];
    $url = $_POST['url'] ;
    $message = $_POST['message'] ;
    $message1 = $url."\n\n".$message ;
    $message2 = $url."\n\n".$message."\n\n".$signature ;
    $subject ="***$form Alert*** $topic" ;
    $headers ="From: $email";
    mail ($to, $subject, $message1, $headers) ;
    $subject ="***$company Email Receipt*** $topic" ;
    $headers ="From: $webmaster";
    mail ($email, $subject, $message2, $headers) ;
    print "Your message was sent successfully. The webmaster will be contacting you as soon as possible.";
    }
    ?>


    As you can see, I am trying to validate that there is a topic and message entered before sending. However, if one of these fails it still continues to go on down the line and send. It will give the error message but also says Your message was sent.

    How can I make it stop if one fails and etc..?
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    Cleaned up, indented, and commented:
    PHP Code:
    <?php
    $errors 
    = array();

    $topic $_POST['topic']; //This checks the subject
    if ( $topic == '' //WHY!?
      
    if ( ! $topic //All of this only executes if the one above it is true.  You don't need that
      
    {
        echo 
    "You have not entered a subject, please go back and enter a subject.";
      }
      
      
    //I changed this block to be the way it SHOULD be, compare to above.
    $message $_POST['message']; 
    if ( empty( 
    $message ) )   
    {
      
    $errors[] = "You have not entered a message, please go back and enter a message.";
    }
      
      
    //this function is called "spamcheck" but all it does is validate emails.  The name should change
    function spamcheck$field //filter_var() sanitizes the e-mail
    {
      
    $field filter_var$fieldFILTER_SANITIZE_EMAIL ); //filter_var() validates the e-mail      
      
    if ( filter_var$fieldFILTER_VALIDATE_EMAIL ) )
      {
        return 
    TRUE;
      }
      else
      {
        return 
    FALSE;
      }
    }

    if ( 
    count$errors ) > ) {
      die(
    "You must fix the following errors before proceeding: <br />" implode("<br />"$errors));
    }

    if ( isset( 
    $_REQUEST['email'] ) ) //if "email" is filled out, proceed with sending    
    {
      
    //check if the email address is invalid   
      
    $mailcheck spamcheck$_REQUEST['email'] );
      if ( 
    $mailcheck == FALSE )
      {
        echo 
    "You have not entered a valid email, please press the back button and try again.";
      }
      else 
    //send email if validation has passed          
      
    {
        
    $topic $_POST['topic'];
        
    $email $_POST['email'];
        
    $url $_POST['url'];
        
    //look into variable concatenation.  $message .= $someString; adds $someString to the end of $message.
        
    $message $_POST['message'];
        
    $message1 $url "\n\n" $message;
        
    $message2 $url "\n\n" $message "\n\n" $signature;// you add the URL twice.
        
    $subject "***$form Alert*** $topic"//$form comes from nowhere
        
    $headers "From: $email";
        
    mail$to$subject$message1$headers ); //$to comes from nowhere
        
    $subject "***$company Email Receipt*** $topic"//$company comes from nowhere.
        
    $headers "From: $webmaster"//webmaster comes from nowhere, and this overwrites the line 3 up from here.
        
    mail$email$subject$message2$headers );
        print 
    "Your message was sent successfully. The webmaster will be contacting you as soon as possible.";
      }
    }
    ?>
    -Dan
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    306
    Rep Power
    6
    Originally Posted by ManiacDan
    Cleaned up, indented, and commented:
    PHP Code:
    <?php
    $errors 
    = array();

    $topic $_POST['topic']; //This checks the subject
    if ( $topic == '' //WHY!?
      
    if ( ! $topic //All of this only executes if the one above it is true.  You don't need that
      
    {
        echo 
    "You have not entered a subject, please go back and enter a subject.";
      }
      
      
    //I changed this block to be the way it SHOULD be, compare to above.
    $message $_POST['message']; 
    if ( empty( 
    $message ) )   
    {
      
    $errors[] = "You have not entered a message, please go back and enter a message.";
    }
      
      
    //this function is called "spamcheck" but all it does is validate emails.  The name should change
    function spamcheck$field //filter_var() sanitizes the e-mail
    {
      
    $field filter_var$fieldFILTER_SANITIZE_EMAIL ); //filter_var() validates the e-mail      
      
    if ( filter_var$fieldFILTER_VALIDATE_EMAIL ) )
      {
        return 
    TRUE;
      }
      else
      {
        return 
    FALSE;
      }
    }

    if ( 
    count$errors ) > ) {
      die(
    "You must fix the following errors before proceeding: <br />" implode("<br />"$errors));
    }

    if ( isset( 
    $_REQUEST['email'] ) ) //if "email" is filled out, proceed with sending    
    {
      
    //check if the email address is invalid   
      
    $mailcheck spamcheck$_REQUEST['email'] );
      if ( 
    $mailcheck == FALSE )
      {
        echo 
    "You have not entered a valid email, please press the back button and try again.";
      }
      else 
    //send email if validation has passed          
      
    {
        
    $topic $_POST['topic'];
        
    $email $_POST['email'];
        
    $url $_POST['url'];
        
    //look into variable concatenation.  $message .= $someString; adds $someString to the end of $message.
        
    $message $_POST['message'];
        
    $message1 $url "\n\n" $message;
        
    $message2 $url "\n\n" $message "\n\n" $signature;// you add the URL twice.
        
    $subject "***$form Alert*** $topic"//$form comes from nowhere
        
    $headers "From: $email";
        
    mail$to$subject$message1$headers ); //$to comes from nowhere
        
    $subject "***$company Email Receipt*** $topic"//$company comes from nowhere.
        
    $headers "From: $webmaster"//webmaster comes from nowhere, and this overwrites the line 3 up from here.
        
    mail$email$subject$message2$headers );
        print 
    "Your message was sent successfully. The webmaster will be contacting you as soon as possible.";
      }
    }
    ?>
    -Dan
    You are awesome! The form works great! I am thankful there are people like yourself who are willing to take the time and help others who are wanting to learn.

    By looking at this, I figure errors is a set php function like echo correct and doesnt need defining like a variable does? It stops the form or action from continuing. Also, I was wondering if you could explain to me a little about these functions so I can better understand them.

    $errors = array(); This looks a little confusing and why the ()?

    if ( empty( $message ) ) I am assuming that empty is another set function in PHP and doesn't have to be defined like a variable.

    $errors[] = ? What does the [] do here? Being brand new at this I would have put echo '$errors';

    What is hard for me to understand is when to use () , [] & the {} symbols.

    Just a quick note also, I had the variables $form, $to, $company, & $signature on a separate php file called config.php in case I ever wanted to change these easily. I require this file at the beginning of this file. Is that ok to do?
  12. #7
  13. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6351
    By looking at this, I figure errors is a set php function like echo correct and doesnt need defining like a variable does?
    Nope, I initialized it on the first line.

    I was wondering if you could explain to me a little about these functions so I can better understand them.
    Bad news: No functions here except the "spamcheck" one you wrote yourself. There are built in functions count(), empty(), and isset(), all of which have well-written entries in the php manual.

    $errors = array(); This looks a little confusing and why the ()?
    Because that's correct. An array is a language construct, it requires the parentheses, even if there's nothing in them.

    if ( empty( $message ) ) I am assuming that empty is another set function in PHP and doesn't have to be defined like a variable.
    Yes, [phpnet=empty]empty[/phpnet] is a built in function. There are 40,000 of them (yes, really).
    $errors[] = ? What does the [] do here? Being brand new at this I would have put echo '$errors';
    $arrayName[] = "something"; appends "something" as a new item to the end of $arrayName. Since $errors was defined at the start of the script as an array, this line takes the new error and puts it on to the "errors array." Arrays are like lists, but much more powerful.

    What is hard for me to understand is when to use () , [] & the {} symbols.
    () is for function calls, language constructs, and logical separations.

    [] is for arrays.

    {} is for string interpolation and control structures.

    Example:
    PHP Code:
    $greetings = array( "Hello!""Bonjour!" );
    $a "Hello!";
    if ( 
    in_array$a$greetings ) ) {
      echo 
    "I saw my friend paul and I was expecting either {$greetings[0]} or {$greetings[1]}, but he said {$a}";

    You'll get better at it with practice. Get that book. I recommend "Web Database Programming with PHP & MySQL" from O'Reilly Press. It's a little advanced for you though, browse the bookstore.

    -Dan

    Comments on this post

    • ishnid agrees
    Last edited by ManiacDan; March 1st, 2011 at 04:26 PM.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    306
    Rep Power
    6
    Originally Posted by ManiacDan
    Nope, I initialized it on the first line.

    Bad news: No functions here except the "spamcheck" one you wrote yourself. There are built in functions count(), empty(), and isset(), all of which have well-written entries in the php manual.

    Because that's correct. An array is a language construct, it requires the parentheses, even if there's nothing in them.

    Yes, [phpnet=empty]empty[/phpnet] is a built in function. There are 40,000 of them (yes, really).
    $arrayName[] = "something"; appends "something" as a new item to the end of $arrayName. Since $errors was defined at the start of the script as an array, this line takes the new error and puts it on to the "errors array." Arrays are like lists, but much more powerful.



    () is for function calls, language constructs, and logical separations.

    [] is for arrays.

    {} is for string interpolation and control structures.

    Example:
    PHP Code:
    $greetings = array( "Hello!""Bonjour!" );
    $a "Hello!";
    if ( 
    in_array$a$greetings ) ) {
      echo 
    "I saw my friend paul and I was expecting either {$greetings[0]} or {$greetings[1]}, but he said {$a}";

    You'll get better at it with practice. Get that book. I recommend "Web Database Programming with PHP & MySQL." It's a little advanced for you though, browse the bookstore.

    -Dan
    Will do, thanks again for all your help!
  16. #9
  17. Moderator Emeritus
    Devshed Supreme Being (6500+ posts)

    Join Date
    Feb 2002
    Location
    Austin, TX
    Posts
    7,186
    Rep Power
    2265
    This is a great book for beginners. Get the book Dan's recommending, too - it's a good idea to have a couple of different vantage points to reference when you're learning a new language. But, you can work through the exercises in PHP MySQL Web Development, and be productive (not an expert, obviously, but able to find your way around) in PHP within about 2 weeks (as long as you work at it everyday, of course).
    DrGroove, Devshed Moderator | New to Devshed? Read the User Guide | Connect with me on LinkedIn

IMN logo majestic logo threadwatch logo seochat tools logo