KhaledSalh Posted June 18, 2013 Share Posted June 18, 2013 (edited) Hi guys,I am taking a PHP class and I need your help. I have the instructions below:1. Create a variable called $validations that stores an empty array.2. For each data field’s input, make a call to the appropriate validation function for the field and store the result an new index in the $validations array.3. Create a variable called $errors that stores an emptyarray.4.Write a foreach loop for the $validations array. With each iteration, check if the index is empty. If the index is not empty, add the value as a new index of the $errors array.5. Otherwise, if the form has not been submitted, write statements for the following:6. Initialize a variable for each form input with a value of an empty string.Example: $first_name = '';I have written the code, but I am not sure if I got it right, this is the code: $validations = array(); $validatfirst_name = required($first_name, $min , "First Name" ); $validatlast_name = required($last_name, $min , "Last Name" ); $validatemail = valid_email($email, "Email Address"); $validatephone_num = valid_int($phone_num, $min, "Phone"); $validatephone_num2 = valid_int($phone_num2, $min, "Phone"); $validatephone_num3 = valid_int($phone_num3, $min, "Phone"); $validatmessage = required($message, '1', message); $validations = array($validatfirst_name, $validatlast_name, $validatemail, $validatephone_num, $validatephone_num2, $validatephone_num3, $validatmessage); $errors = array(); foreach($validations as $index=>$validation) { if(!empty($index)) { $errors[$index] = $validation; } } if(!isset($_POST['submit'])) { $first_name = ''; $last_name = ''; $email = ''; $phone_num = ''; $phone_num1 = ''; $phone_num2 = ''; $phone = ''; $message = ''; } Edited June 18, 2013 by KhaledSalh Quote Link to comment Share on other sites More sharing options...
requinix Posted June 18, 2013 Share Posted June 18, 2013 For #2 you didn't literally follow the instruction, but your end result is exactly the same so I think that'll be fine. (You were probably intended to put the values directly into $validations without those temporary variables, but like I said it's all the same.) #4 is weird. You did what it said but what it said is... odd. Think about it for a second. In the foreach loop the $index will be the array key and the $validation will be the value. Right so far. But the index doesn't really do a whole lot. Since you aren't using custom array keys they'll just be numbers: 0, 1, 2, and so on for each item. Checking "if the index is empty" doesn't get you anything. Ooh, it's a number. So? What the instruction should probably be is Write a foreach loop for the $validations array. With each iteration, check if the value is empty. If the value is not empty, add the value as a new index of the $errors array.But clear that with your instructor before you change your code. Quote Link to comment Share on other sites More sharing options...
richei Posted June 18, 2013 Share Posted June 18, 2013 (edited) You actually need to reverse the start of the script since you have to do default values (empty strings). Something like if(!isset($_POST['submit'])) { $first_name = ''; $last_name = ''; $email = ''; $phone_num = ''; $phone_num1 = ''; $phone_num2 = ''; $phone = ''; $message = ''; } else { $validations = array(); $errors = array(); $validatfirst_name = required($first_name, $min , "First Name" ); $validatlast_name = required($last_name, $min , "Last Name" ); $validatemail = valid_email($email, "Email Address"); $validatephone_num = valid_int($phone_num, $min, "Phone"); $validatephone_num2 = valid_int($phone_num2, $min, "Phone"); $validatephone_num3 = valid_int($phone_num3, $min, "Phone"); $validatmessage = required($message, '1', message); $validations = array($validatfirst_name, $validatlast_name, $validatemail, $validatephone_num, $validatephone_num2, $validatephone_num3, $validatmessage); foreach($validations as $index=>$validation) { if(!empty($index)) { $errors[$index] = $validation; } } } This way, you're doing the validation when the button has been submitted, not when the page is entered. Edited June 18, 2013 by richei Quote Link to comment Share on other sites More sharing options...
KhaledSalh Posted June 18, 2013 Author Share Posted June 18, 2013 I somehow got it right, but facing some issues. all the fields display an error message if there is no value assigned to it, unless the firs name field! I do not know why it does not respond like the others do, see below image. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 18, 2013 Share Posted June 18, 2013 What's your current code? 5. Otherwise, if the form has not been submitted..."Otherwise"? I don't see anything that says "if the form has been submitted then...", which should be somewhere to pair up with this instruction. I agree with Fou-Lu on codingforums.com: the instructions are odd, nevermind that the sequence of steps in richei's code is better than what you're being told to do. I suggest you talk with your teacher and get things straightened out. Quote Link to comment Share on other sites More sharing options...
Q695 Posted June 18, 2013 Share Posted June 18, 2013 Hi guys, I am taking a PHP class and I need your help. I have the instructions below: I don't do homework. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 18, 2013 Share Posted June 18, 2013 I don't do homework.Then it's a good thing OP didn't actually ask for a solution and only asked if his code was correct. Quote Link to comment Share on other sites More sharing options...
Q695 Posted June 19, 2013 Share Posted June 19, 2013 Then it's a good thing OP didn't actually ask for a solution and only asked if his code was correct. The only way to make sure code is working is to test it yourself. Quote Link to comment Share on other sites More sharing options...
requinix Posted June 19, 2013 Share Posted June 19, 2013 The only way to make sure code is working is to test it yourself.Then it's a good thing OP didn't actually ask if it was working and only asked if his code was correct (according to the instructions given). Quote Link to comment 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.