nascar_stripe Posted June 9, 2009 Share Posted June 9, 2009 Hi I have some previous experience with HTML but not at all with PHP. What I am trying to do, I can do in HTML but not in PHP. I want to simplify my form validation by creating functions to execute when a receipt is printed. The only problem I'm having is creating functions for the name fields and email to show the error messages when the form validation has occurred and the form is not correct. Can anyone direct me in the right path to correct my situation? Thanks! Here's my code: <?php //ignore this. I was merely counting my errors $errors = 0; $error_msg = array(); $array_pizza = array('Cheese', 'Pepperoni', 'Hawaiian', 'Italian Sausage', 'Vegetable'); $array_toppings = array('cheese'=>'Extra Chesse', 'pepper'=> 'Pepper', 'pineapple'=>'Pineapple', 'olive'=>'Olives', 'bacon'=>'Bacon', 'ham'=> 'Ham'); if(isset($_POST['submit'])) { $email = $_POST['email']; $pizza = $_POST['pizza']; $data = $_POST['topping']; /* function check_names() { //I want to create one function that will check both firstname and lastname to see if something was entered and what was entered is alpha only. } function check_email() { //need to make this a function to show error message. } */ if (!ctype_alpha($firstname)) { $error_msg['firstname'] = 'A name can only contain letters. Please try again.'; // $errors=$errors + 1; } else if (strlen($firstname) == 0) { $error_msg['firstname'] = 'Please enter a name.'; // $errors=$errors + 1; } else { echo $firstname; } if (!ctype_alpha($lastname)) { $error_msg['lastname'] = 'A name can only contain letters. Please try again.'; // $errors=$errors + 1; } else if (strlen($lastname) == 0) { $error_msg['lastname'] = 'Please enter a last name.'; // $errors=$errors + 1; } else { echo ' '.$lastname; } if (strlen($email) == 0) { $error_msg['email'] = 'Please enter an e-mail address.'; // $errors=$errors + 1; } else if (!ereg("^[A-Za-z0-9\.|-|_]*[@]{1}[A-Za-z0-9\.|-|_]*[.]{1}[a-z]{2,5}$", $email)) { $error_msg['email'] = 'Invalid e-mail address. Please try again.'; // $errors=$errors + 1; } else { echo '<br>'.$email.'<br><br>'; } if ($pizza=='') { $error_msg['pizza'] = ' Please select a pizza.<br>'; // $errors=$errors + 1; } if (count($_POST['topping'])<=0) { $error_msg['topping']='Please select your topping(s).<br>'; // $errors=$errors + 1; } function show_toppings() { foreach ($_POST['topping'] as $topping) { echo $topping.'<br>'; } $result = count($_POST['topping']); echo "<br>You have selected ".$result." toppings.<br>"; } //count($errors); //echo $errors; function show_receipt() { global $pizza; echo 'You have selected a '.$pizza.' pizza with the following toppings:<br>'; show_toppings(); } if (sizeof($error_msg) == 0) { show_receipt(); } else { show_form(); } } else if(!isset($_POST['submit'])) { show_form(); } function show_form() { global $error_msg, $array_pizza, $array_toppings, $data; echo <<<_HTML_ <h3>Pizza Online Ordering</h3> <form name="order" action="index.php" method="post"> _HTML_; echo 'First Name: <input type="text" name="firstname" id="firstname" value="'. $_POST['firstname'].'" ><i>'.$error_msg[firstname].'</i><br>'; echo 'Last Name: <input type="text" name="lastname" id="lastname" value="'. $_POST['lastname'].'" ><i>'.$error_msg[lastname].'</i><br>'; echo 'Email Address: <input type="text" name="email" id="email" value="'.$_POST['email'].'"><i>'.$error_msg[email].'</i><br><br>'; echo '<i>'.$error_msg[pizza].'</i>Choose your pizza:<br>'; foreach ($array_pizza as $thispizza) { echo '<input type="radio" name="pizza" value="'.$thispizza.'"'; if (strcmp($thispizza,$_POST['pizza'])==0) { echo 'checked'; } echo '>'.$thispizza.'<br>'; } echo '<br>'; echo '<i>'.$error_msg[topping].'</i>I’d like to add these toppings:<br>'; foreach ($array_toppings as $topping_id => $topping_value) { echo '<input type="checkbox" name="topping[]" id="'.$topping_id.'" value="'.$topping_id.'" '; if (isset($_POST['topping'])) { $checked_toppings = $_POST['topping']; if (in_array($topping_id, $checked_toppings)) { echo 'checked'; } } echo '>'.$topping_value.'<br>'; } echo <<<_HTML_ <br><input type="submit" name="submit" value="Submit" onclick="check_form()"/> </form> _HTML_; } ?> In the end, the receipt should show the first name, last name, email, the pizza that was selected, the toppings, and the number of toppings. Thanks in advance for any help and advice! :] Quote Link to comment https://forums.phpfreaks.com/topic/161585-having-trouble-creating-functions/ Share on other sites More sharing options...
KevinM1 Posted June 9, 2009 Share Posted June 9, 2009 You're actually on the right track. Remember that functions can take arguments and return values. So, you can pass in your error_msg array to the validation functions and add an error message, if need be. This will allow you to also forgo using the dreaded 'global' keyword, which should be avoided where possible. So, you have your error_msg array... $error_msg = array(); Now you need to pass it to your validation functions. You'll need to pass it by reference (don't worry if you don't understand that) to ensure that any changes made by the functions persist outside of them. So... function check_names(&$error_msg) { /* pre-existing name validation code goes here */ } function check_email(&$error_msg) { /* pre-existing email validation code goes here */ } function show_receipt($pizza) { /* pre-existing receipt code - WITHOUT 'global $pizza' - goes here */ } function show_form($error_msg, $array_pizza, $array_toppings, $data) { /* pre-existing form code - WITHOUT any of the globals - goes here */ } Let me know if this helps. Quote Link to comment https://forums.phpfreaks.com/topic/161585-having-trouble-creating-functions/#findComment-852745 Share on other sites More sharing options...
nascar_stripe Posted June 11, 2009 Author Share Posted June 11, 2009 Unfortunately it did not. It doesnt seem to want to go outside the function, if that makes sense. My validation works if it is not in a function, but when i try to put each section into a function, it doesnt show any of the error messages when the "submit" button is pressed if the information inputed is incorrect. The form is sending back to itself. It showed the error messages after I submitted the form, but I dont want the form to submit if the information is not correct. I was thinking of defining the array with variables, but I wanted to avoid doing that so I wont have like 7 error messages to call upon. Any other thoughts on what I can do? Without the functions, the array error_msg shows this if the information inputed is incorrect: Array ( [firstname] => Please enter a name. [lastname] => Please enter a last name. [email] => Please enter an e-mail address. [pizza] => Please select a pizza.<br> [topping] => Please select your topping(s).<br> ) Otherwise, the code with the functions that doesnt return error_msg for firstname, lastname, and email is this: <?php $error_msg = array(); $array_pizza = array('Cheese', 'Pepperoni', 'Hawaiian', 'Italian Sausage', 'Vegetable'); $array_toppings = array('cheese'=>'Extra Chesse', 'pepper'=> 'Pepper', 'pineapple'=>'Pineapple', 'olive'=>'Olives', 'bacon'=>'Bacon', 'ham'=> 'Ham'); if(isset($_POST['submit'])) { $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $pizza = $_POST['pizza']; $data = $_POST['topping']; function check_names($error_msg) { if (!ctype_alpha($firstname)) { $error_msg['firstname'] = 'A name can only contain letters. Please try again.'; } else if (strlen($firstname) == 0) { $error_msg['firstname'] = 'Please enter a name.'; } else { echo $firstname; } if (!ctype_alpha($lastname)) { $error_msg['lastname'] = 'A name can only contain letters. Please try again.'; } else if (strlen($lastname) == 0) { $error_msg['lastname'] = 'Please enter a last name.'; } else { echo ' '.$lastname; } } function check_email($error_msg) { if (strlen($email) == 0) { $error_msg['email'] = 'Please enter an e-mail address.'; } else if (!ereg("^[A-Za-z0-9\.|-|_]*[@]{1}[A-Za-z0-9\.|-|_]*[.]{1}[a-z]{2,5}$", $email)) { $error_msg['email'] = 'Invalid e-mail address. Please try again.'; } else { echo '<br>'.$email.'<br><br>'; } } if ($pizza=='') { $error_msg['pizza'] = ' Please select a pizza.<br>'; } if (count($_POST['topping'])<=0) { $error_msg['topping']='Please select your topping(s).<br>'; } function show_toppings() { foreach ($_POST['topping'] as $topping) { echo $topping.'<br>'; } $result = count($_POST['topping']); echo "<br>You have selected ".$result." toppings.<br>"; } function show_receipt($pizza) { check_names($error_msg); check_email($error_msg); echo 'You have selected a '.$pizza.' pizza with the following toppings:<br>'; show_toppings(); } if (sizeof($error_msg) == 0) { show_receipt($pizza); } else { show_form(); } } else if(!isset($_POST['submit'])) { show_form(); } function show_form() { global $error_msg, $array_pizza, $array_toppings, $data; echo <<<_HTML_ <h3>Pizza Hoot Online Ordering</h3> <form name="order" action="index.php" method="post"> _HTML_; echo 'First Name: <input type="text" name="firstname" id="firstname" value="'. $_POST['firstname'].'" ><i>'.$error_msg[firstname].'</i><br>'; echo 'Last Name: <input type="text" name="lastname" id="lastname" value="'. $_POST['lastname'].'" ><i>'.$error_msg[lastname].'</i><br>'; echo 'Email Address: <input type="text" name="email" id="email" value="'.$_POST['email'].'"><i>'.$error_msg[email].'</i><br><br>'; echo '<i>'.$error_msg[pizza].'</i>Choose your pizza:<br>'; foreach ($array_pizza as $thispizza) { echo '<input type="radio" name="pizza" value="'.$thispizza.'"'; if (strcmp($thispizza,$_POST['pizza'])==0) { echo 'checked'; } echo '>'.$thispizza.'<br>'; } echo '<br>'; echo '<i>'.$error_msg[topping].'</i>I’d like to add these toppings:<br>'; foreach ($array_toppings as $topping_id => $topping_value) { echo '<input type="checkbox" name="topping[]" id="'.$topping_id.'" value="'.$topping_id.'" '; if (isset($_POST['topping'])) { $checked_toppings = $_POST['topping']; if (in_array($topping_id, $checked_toppings)) { echo 'checked'; } } echo '>'.$topping_value.'<br>'; } echo <<<_HTML_ <br><input type="submit" name="submit" value="Submit" onclick="check_form()"/> </form> _HTML_; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161585-having-trouble-creating-functions/#findComment-853906 Share on other sites More sharing options...
nascar_stripe Posted June 11, 2009 Author Share Posted June 11, 2009 I solved my own problem! yay! haha i had everything in the wrong order. My functions always worked, I just had them in the wrong place. I chose to use global for myself, but I also checked to see if the fields/parameters would work in the () and they do. (I know it makes everything easier and neater looking, but as a super beginner in PHP I dont want to change my format until I learn how to use it properly and in context ) So, this is what was wrong. The basic structure for my little program was: if (the form was submitted) { DO form validation } else if (the form was NOT submitted) { show the form with user input } I had it: if (the form was submitted) { form validation DO form validation } else if (the form was NOT submitted) { show the form with user input } and it should be: if (the form was submitted) { DO form validation } else if (the form was NOT submitted) { show the form with user input form validation } I created and was calling the functions after the form was submitted; they should be seperate. So here's my code, if it helps anyone at all. You can compare what I had before above to what is now my test program. Thanks for the help! :] <?php $error_msg = array(); $array_pizza = array('Cheese', 'Pepperoni', 'Hawaiian', 'Italian Sausage', 'Vegetable'); $array_toppings = array('cheese'=>'Extra Chesse', 'pepper'=> 'Pepper', 'pineapple'=>'Pineapple', 'olive'=>'Olives', 'bacon'=>'Bacon', 'ham'=> 'Ham'); if(isset($_POST['submit'])) { $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $pizza = $_POST['pizza']; $data = $_POST['topping']; check_firstname($firstname); check_lastname($lastname); check_email($email); wasPizzaChecked($pizza); wasToppingChecked($topping); if (sizeof($error_msg) == 0) { show_receipt(); } else { show_form(); } } else if(!isset($_POST['submit'])) { show_form(); } function show_form() { global $error_msg, $array_pizza, $array_toppings, $data; echo <<<_HTML_ <h3>Pizza Hoot Online Ordering</h3> <form name="order" action="index.php" method="post"> _HTML_; echo 'First Name: <input type="text" name="firstname" id="firstname" value="'. $_POST['firstname'].'" ><i>'.$error_msg[firstname].'</i><br>'; echo 'Last Name: <input type="text" name="lastname" id="lastname" value="'. $_POST['lastname'].'" ><i>'.$error_msg[lastname].'</i><br>'; echo 'Email Address: <input type="text" name="email" id="email" value="'.$_POST['email'].'"><i>'.$error_msg[email].'</i><br><br>'; echo '<i>'.$error_msg[pizza].'</i>Choose your pizza:<br>'; foreach ($array_pizza as $thispizza) { echo '<input type="radio" name="pizza" value="'.$thispizza.'"'; if (strcmp($thispizza,$_POST['pizza'])==0) { echo 'checked'; } echo '>'.$thispizza.'<br>'; } echo '<br>'; echo '<i>'.$error_msg[topping].'</i>I’d like to add these toppings:<br>'; foreach ($array_toppings as $topping_id => $topping_value) { echo '<input type="checkbox" name="topping[]" id="'.$topping_id.'" value="'.$topping_id.'" '; if (isset($_POST['topping'])) { $checked_toppings = $_POST['topping']; if (in_array($topping_id, $checked_toppings)) { echo 'checked'; } } echo '>'.$topping_value.'<br>'; } echo <<<_HTML_ <br><input type="submit" name="submit" value="Submit" onclick="check_form()"/> </form> _HTML_; } function check_firstname($firstname) { global $error_msg; if (!ctype_alpha($firstname)) { $error_msg['firstname'] = 'A name can only contain letters. Please try again.'; } else if (strlen($firstname) == 0) { $error_msg['firstname'] = 'Please enter a name.'; } } function check_lastname($lastname) { global $error_msg; if (!ctype_alpha($lastname)) { $error_msg['lastname'] = 'A name can only contain letters. Please try again.'; } else if (strlen($lastname) == 0) { $error_msg['lastname'] = 'Please enter a name.'; } } function check_email($email) { global $error_msg; if (strlen($email) == 0) { $error_msg['email'] = 'Please enter an e-mail address.'; } else if (!ereg("^[A-Za-z0-9\.|-|_]*[@]{1}[A-Za-z0-9\.|-|_]*[.]{1}[a-z]{2,5}$", $email)) { $error_msg['email'] = 'Invalid e-mail address. Please try again.'; } } function wasPizzaChecked($pizza) { global $error_msg; if ($pizza=='') { $error_msg['pizza'] = ' Please select a pizza.<br>'; } } function wasToppingChecked($topping) { global $error_msg; if (count($_POST['topping'])<=0) { $error_msg['topping']='Please select your topping(s).<br>'; } } function show_toppings() { foreach ($_POST['topping'] as $topping) { echo $topping.'<br>'; } $result = count($_POST['topping']); echo "<br>You have selected ".$result." toppings.<br>"; } function show_receipt() { global $firstname, $lastname, $email, $pizza; echo $firstname.' '.$lastname.'<br>'; echo $email.'<br><br>'; echo 'You have selected a '.$pizza.' pizza with the following toppings:<br>'; show_toppings(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161585-having-trouble-creating-functions/#findComment-854103 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.