grk101 Posted May 2, 2009 Share Posted May 2, 2009 hi guys, i really haven't used an array for ever, probably never, i am reading on it on php.net trying to get an idea but i can't understand it fully. sorry i am a noob. <?php session_start(); if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) { // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. $error=('required.html'); $choice=$_POST['choice']; $division=$_POST['division']; $name=$_POST['name']; $sex=$_POST['sex']; if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email'])) { die("Invalid e-mail address"); } // Check if the field was empty or not: if( empty($_POST['choice']) ) { header( "Location:$error" ); exit; } if( empty($_POST['division']) ) { header( "Location:$error" ); exit; } if( empty($_POST['company']) ) { header( "Location:$error" ); exit; } if( empty($_POST['comments']) ) { header( "Location:$error" ); exit; } $mailcontent = "Registration Information:\n\n". "I am registering as an: ".$choice ."\n\n". "Division Selected: ".$division . "\n\n". "Player Information:"."\n\n". "Name: ".$name."\n\n". $subject = "Outdoor Volunteer"; $emailto = "test@test.com\n\n"; $fromname = $name." ".$email; $others="From: ".$fromname."<".$user_email.">\n"; mail($emailto, $subject, $mailcontent, $others); unset($_SESSION['security_code']); } else { header( "security.php" ); } $os0 = $_POST['os0']; $os1 = $_POST['os1']; ?> how can i shorten this down? i have like 20 more fields to enter <?php $field = array("division", "name", "age"); foreach($field as $key => $value) { echo $key . " " . $value . "<br>"; } I am assuming that i have to create I am not sure if that is the proper way to display that they haven't entered a field. I want an error page to be shown and they have to hit back to fill it out. thats why i made the required.html page but write now i am wrote this from examples i found online echo $key . " " . $value . "<br>"; how can i make it work properly? thanks for any help Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 <?php $error = 'required.html'; foreach ($_POST as $key => $value) { if (empty($value)) header('Location: ' . $error); } Quote Link to comment Share on other sites More sharing options...
semlabs Posted May 2, 2009 Share Posted May 2, 2009 I'm not sure exactly what you want to do, but I don't think there is a way to shorten what I think you are trying to do. I have PMed you with an alternative solution. Quote Link to comment Share on other sites More sharing options...
grk101 Posted May 2, 2009 Author Share Posted May 2, 2009 hey guys while you responded, i was putting up my whole script i only included a bit of it by mistake. what i want to do, eventually it will end up checking 20 fields, Out of the 20 fields only 15 will be required. but it would look weird typing in 15 of these if( empty($_POST['choice']) ) { header( "Location:$error" ); exit; } Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 Oh, I didn't see the edited code. This should help: <?php $error = 'required.html'; $requiredfields = array('fieldname1', 'fieldname2', 'fieldname3'); foreach ($_POST as $key => $value) { if (in_array($key, $requiredfields) && empty($value)) header('Location: ' . $error); ${$key} = $value; } Quote Link to comment Share on other sites More sharing options...
grk101 Posted May 2, 2009 Author Share Posted May 2, 2009 hey Ken thanks for your reply, while i was adding the edit code in, it told me someone replied,i was a tad slow. I dont have to do the following below do i? my friend was telling me i would have to declare the array above my code? $errors = array(); // set the errors array to empty, by default $fields = array(); // stores the field values$message = ""; $values = array(); // stores the validation rules Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 2, 2009 Share Posted May 2, 2009 The $errors will be useless because you're going to be redirecting the user anyways. What would you put in for the validation rules? I don't think you would need to do all that. Also, in my code, if you want a field name say it's $_POST['field1'], you can just call $field1 instead because I defined it in the foreach loop. Just in case you didn't know what the last statement in that loop did. Quote Link to comment Share on other sites More sharing options...
grk101 Posted May 3, 2009 Author Share Posted May 3, 2009 awesome, thank you so much for your help, truly appreciated i will be back to respond to this thread when i get home and i hope all works well, thanks once again Quote Link to comment Share on other sites More sharing options...
grk101 Posted May 3, 2009 Author Share Posted May 3, 2009 hey Ken, everything works! question for you though.. in you code <?php $error = 'required.html'; $requiredfields = array('fieldname1', 'fieldname2', 'fieldname3'); foreach ($_POST as $key => $value) { if (in_array($key, $requiredfields) && empty($value)) header('Location: ' . $error); ${$key} = $value; } is there a way to display what fields need to be complete by the user? i tried the following.. foreach ($_POST as $key => $value) { if (in_array($key, $requiredfields) && empty($value)); echo $key . " " . $value . "<br>"; which shows all my fields in a new page even the ones entered, so gauranteed i am doing something wrong.. In the form processor i have a redirect going if everything is correct, for donations for our club, , unless i can display the errors and what needs to be completed on a different pages like error.php so the redict doesn't happen to the donations page. Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 3, 2009 Share Posted May 3, 2009 <?php $error = 'required.html'; $requiredfields = array('fieldname1', 'fieldname2', 'fieldname3'); $errors = array(); foreach ($_POST as $key => $value) { if (in_array($key, $requiredfields) && empty($value)) $errors[] = $key; ${$key} = $value; } Instead of redirecting the user, it will store all the required fields that are empty into the $errors array. Then do as you wish from there. Quote Link to comment Share on other sites More sharing options...
grk101 Posted May 4, 2009 Author Share Posted May 4, 2009 Okay so this is the code below: Okay so far it works great, and I appreciate the help; What happens is: the echo statement, lists every field and the ones entered, and the entries, and then the javascript automatically goes to paypal. Thats why i was thinking that if the errors show up on a different page, therefore the redirect wouldn't happen or is that not do able. I guess you could say i am a little bit lost right now ! I used a javascript redirect cause i didn't know how to do it in php. <?php session_start(); if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) { // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. $error=('required.php'); $choice=$_POST['choice']; $division=$_POST['division']; $os0=$_POST['os0']; $sex=$_POST['sex']; $birth=$_POST['birth']; $birthmonth=$_POST['birthmonth']; $birthyear=$_POST['birthyear']; $address=$_POST['address']; $city=$_POST['city']; if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email'])) { die("Invalid e-mail address"); } $requiredfields = array('choice', 'division', 'name','sex','birth','birthmonth','birthyear','address','city'); $errors = array(); foreach ($_POST as $key => $value) { if (in_array($key, $requiredfields) && empty($value)) $errors[] = $key; ${$key} = $value; echo $key . " " . $value . "<br>"; } $mailcontent = "Registration Information:\n\n". "I am registering as an: ".$choice ."\n\n". "Division Selected: ".$division . "\n\n". "Player Information:"."\n\n". "Name: ".$os0."\n\n". "Sex: ".$sex."\n\n". "Date Of Birth: " . $birth ."-".$birthmonth."-".$birthyear. "\n\n". "Address: ".$address."\n\n". "City: ".$city."\n\n". $subject = "Youth Online Donation"; $emailto = "test test@gmail.com\n"; $others="From: ".$os0."<".$email.">\n"; mail($emailto, $subject, $mailcontent, $others); $others="From: test@info.com\n" . "Reply-To: test@.com\n"; $subject = "Youth Online Donation"; $email_to = "$email"; $message = "Thank you for your submission."; @mail($email_to, $subject ,$message,$others); unset($_SESSION['security_code']); } else { header( "Location: security.php" ); } ?> <html><head> <script language="JavaScript" type="text/javascript"> <!-- function submitForm() { document.myForm.submit(); setTimeout('self.close();',10000); } //--> </script> </head> <body onLoad="submitForm()"> You are being redirected to PayPal. <form name="myForm" method="post" action="https://www.paypal.com/cgi-bin/webscr" target="_self"> <input type="hidden" name="add" value="1"> <input type="hidden" name="cmd" value="_xclick"> <input type="hidden" name="business" value="test@gmail.com"> <input type="hidden" name="item_name" value="Youth Donation"> <input type="hidden" name="item_number" value="Youth Donation"> <input type="hidden" name="on0" value="Name"> <input type="hidden" name="os0" value="<? echo $os0 ?>"> <!-- <input type="hidden" name="on1" value="URL"> <input type="hidden" name="os1" value="<?// echo $os1 ?>">--> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 Check if $errors array is empty. If it's not empty, then redirect to require.html with header. Quote Link to comment Share on other sites More sharing options...
grk101 Posted May 4, 2009 Author Share Posted May 4, 2009 Oh, so there is no way to actually let them know what is missing? I thought that is what <?php session_start(); if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) { // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. $error=('required.php'); $choice=$_POST['choice']; $division=$_POST['division']; $os0=$_POST['os0']; $sex=$_POST['sex']; $birth=$_POST['birth']; $birthmonth=$_POST['birthmonth']; $birthyear=$_POST['birthyear']; $address=$_POST['address']; $city=$_POST['city']; if (!preg_match("/^([a-zA-Z0-9])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-]+)+/", $_POST['email'])) { die("Invalid e-mail address"); } $requiredfields = array('choice', 'division', 'name','sex','birth','birthmonth','birthyear','address','city'); $errors = array(); foreach ($_POST as $key => $value) { if (in_array($key, $requiredfields) && empty($value)) $errors[] = $key; ${$key} = $value; echo $key . " " . $value . "<br>"; } did where the echo would write out the key errors, Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted May 4, 2009 Share Posted May 4, 2009 There is. It's in the array $errors. After the foreach loop, do: var_dump($errors); That will tell you the required fields that are empty. Quote Link to comment Share on other sites More sharing options...
grk101 Posted May 4, 2009 Author Share Posted May 4, 2009 I wish i knew my php like you or even remotely close as you. if i use the following: foreach ($_POST as $key => $value) { if (in_array($key, $requiredfields) && empty($value)) header('Location: ' . $error); ${$key} = $value; } what happens is the error pages shows up, which is great, but the page still submits to my email it doesn't stop it at the validation. I tried using the die; command or exit; which stopped the email from being sent, but no page displayed 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.