The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP5 - Questions about stopping and beginning the next function
Discuss Questions about stopping and beginning the next function in the PHP Development forum on Dev Shed. Questions about stopping and beginning the next function PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 28th, 2011, 11:15 PM
|
|
Contributing User
|
|
Join Date: Feb 2011
Posts: 306
  
Time spent in forums: 1 Day 18 h 20 m 4 sec
Reputation Power: 5
|
|
|
PHP5 - 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.
|

March 1st, 2011, 08:13 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
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.
|

March 1st, 2011, 09:32 AM
|
|
Contributing User
|
|
Join Date: Feb 2011
Posts: 306
  
Time spent in forums: 1 Day 18 h 20 m 4 sec
Reputation Power: 5
|
|
Quote: | 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. 
|

March 1st, 2011, 12:03 PM
|
|
Contributing User
|
|
Join Date: Feb 2011
Posts: 306
  
Time spent in forums: 1 Day 18 h 20 m 4 sec
Reputation Power: 5
|
|
|
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..?
|

March 1st, 2011, 12:44 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
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( $field, FILTER_SANITIZE_EMAIL ); //filter_var() validates the e-mail
if ( filter_var( $field, FILTER_VALIDATE_EMAIL ) )
{
return TRUE;
}
else
{
return FALSE;
}
}
if ( count( $errors ) > 0 ) {
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
|

March 1st, 2011, 03:08 PM
|
|
Contributing User
|
|
Join Date: Feb 2011
Posts: 306
  
Time spent in forums: 1 Day 18 h 20 m 4 sec
Reputation Power: 5
|
|
Quote: | 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( $field, FILTER_SANITIZE_EMAIL ); //filter_var() validates the e-mail
if ( filter_var( $field, FILTER_VALIDATE_EMAIL ) )
{
return TRUE;
}
else
{
return FALSE;
}
}
if ( count( $errors ) > 0 ) {
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?
|

March 1st, 2011, 03:25 PM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
Quote: | 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.
Quote: | 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.
Quote: | $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.
Quote: | 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, empty is a built in function. There are 40,000 of them (yes, really).
Quote: |
$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.
Quote: | 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
Last edited by ManiacDan : March 1st, 2011 at 04:26 PM.
|

March 1st, 2011, 03:34 PM
|
|
Contributing User
|
|
Join Date: Feb 2011
Posts: 306
  
Time spent in forums: 1 Day 18 h 20 m 4 sec
Reputation Power: 5
|
|
Quote: | 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, empty 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!
|

March 1st, 2011, 03:35 PM
|
 |
Moderator Emeritus
|
|
Join Date: Feb 2002
Location: Scottsdale, AZ
|
|
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).
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|