krepid Posted March 17, 2007 Share Posted March 17, 2007 Hello, I am trying to create a PHP form that will collect a series of data. I am new to PHP and would appreciate any helpful tips/hints to solve this dilemma. The form will email, but no data is sent ... I thought I could collect all the POST data in a variable I created called $comments ... such as: $comments = $_POST['co, useraddr, username, useremail, yopen, jobdes, salar, position']; and then use another variable named $msg to pass in the email ... #message from the feedback form $msg = $comments; Obviously, that is not acceptable. What can I do? Thank you for reading that! Code below. <?php $self = $_SERVER['PHP_SELF']; $co = $_POST['co']; $useraddr = $_POST['useraddr']; $username = $_POST['username']; $useremail = $_POST['useremail']; $yopen = $_POST['yopen']; $jobdes = $_POST['jobdes']; $salar = $_POST['salar']; $posistion = $_POST['position']; $comments = $_POST['co, useraddr, username, useremail, yopen, jobdes, salar, position']; $sent = $_POST['sent']; # the HTML form $form ="<form action=\"$self\" method=\"post\">"; $form.="<table width=\"450\" border=\"0\"><br /><strong>Please list information so that PCS can better assist you</strong><br /><strong style=\"background-color:yellow; color:red;\">* Indicates a required field"; $form.="<tr><td> <strong style=\"color:red;\">* </strong><strong>Company Name:<input type=\"text\" name=\"co\""; $form.=" size=\"60\" value=\"$co\" ></strong></td></tr>"; $form.="<tr><td> <strong style=\"color:red;\">* </strong><strong>Company Address:<input type=\"text\" name=\"useraddr\" "; $form.=" size=\"60\" value=\"$useraddr\"></strong></td></tr>"; $form.="<tr><td> <strong style=\"color:red;\">* </strong><strong>Your Name and Your Title:<input type=\"text\" name=\"username\""; $form.=" size=\"60\" value=\"$username\" ></strong></td></tr>"; $form.="<tr><td><strong style=\"color:red;\">* </strong><strong>Your Email Address:</strong><input type=\"text\" name=\"useremail\""; $form.=" size=\"38\" value=\"$useremail\"></td></tr>"; $form.="<tr><td> <strong style=\"color:red;\">* </strong><strong>Why is the position available?:<input type=\"text\" name=\"yopen\""; $form.=" size=\"60\" value=\"$yopen\" ></strong></td></tr>"; $form.="<tr><td> <strong style=\"color:red;\">* </strong><strong>The Title and Job Description of the posistion available:<input type=\"text\" name=\"jobdes\""; $form.=" size=\"60\" value=\"$jobdes\" ></strong></td></tr>"; $form.="<tr><td> <strong style=\"color:red;\">* </strong><strong>Please List the Salary Range:<input type=\"text\" name=\"salar\""; $form.=" size=\"60\" value=\"$salar\" ></strong></td></tr>"; $form.="<tr><td><strong style=\"color:red;\">* </strong><strong>Please tell us about the opening you currently have, the type of applicant you desire to fill this opening, and all pertinent credentials (ex: degree, license):</strong>"; $form.="<textarea name=\"position\" cols=\"50\" rows=\"12\">"; $form.="$position</textarea></td></tr>"; $form.="<tr><td><input type=\"submit\" name=\"sent\" value=\"Send Form\">"; $form.="</form>"; $form.="</td></tr>"; $form.="</table>"; if($sent) { #set variable default value $valid=true; #check co field is not blank if( !$co ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Enter the Company Name...</strong><br />"; $valid = false; } #check field is not blank if( !$useraddr ) { $errmsg .="<strong style=\"color:red;\">ERROR ...Enter the Company address...</strong><br />"; $valid = false; } #check username field is not blank if( !$username ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Enter your name...</strong><br />"; $valid = false; } #check username field is not blank // if( !$useremail ) // { $errmsg.="<strong style=\"color:red;\">ERROR ...Enter your email address...</strong><br />"; $valid = false; } #check username field is not blank if( !$yopen ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Why is the position open?...</strong><br />"; $valid = false; } #check username field is not blank if( !$jobdes ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Enter the job description...</strong><br />"; $valid = false; } #check username field is not blank if( !$salar ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Enter the salary range...</strong><br />"; $valid = false; } #check textarea field is not blank if( !$posistion ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Enter the position you need PCS to fill. </strong><br />"; $valid = false; } #check validity of email format $useremail = trim($useremail); #permitted patterns for name,domain and top-level domains $_name = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+"; $_host = "([-0-9A-Z]+\.)+"; $_tlds = "([0-9A-Z]){2,4}$/i"; if( !preg_match( $_name."@".$_host .$_tlds,$useremail ) ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Email address has incorrect format!</strong><br />"; $valid=false; } } #if not valid write the error message/s and repeat the form if($valid != true) { echo( $errmsg.$form ); } else #if the form is valid send the email { #recipients email address $to = "[email protected]"; #subject of the message $re = "Feedback from $username"; #message from the feedback form $msg = $comments; #set the Content-type header for HTML mail $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html;"; $headers .= "charset=\"iso-8859-1\"\r\n"; #set the From header $headers .= "From: $useraddr \r\n"; #send the email now... if(mail($to,$re,$msg, $headers)) { echo("Your information has been sent - Thanks $username <br />Click <a href=\"http://www.example.com\">Here</a> to revisit the homepage");} } ?> Link to comment https://forums.phpfreaks.com/topic/43073-collecting-html-form-info/ Share on other sites More sharing options...
fert Posted March 17, 2007 Share Posted March 17, 2007 $comments=$_POST['co'].$_POST['useraddr'].$_POST['username'].$_POST['useremail'].$_POST['yopen'].$_POST['jobdes'].$_POST['salar'].$_POST['position']; Link to comment https://forums.phpfreaks.com/topic/43073-collecting-html-form-info/#findComment-209230 Share on other sites More sharing options...
kenrbnsn Posted March 17, 2007 Share Posted March 17, 2007 That will give the OP all fields concatenated together in one unreadable string. The OP probably wants a comma separated string. Here is one way of getting that from the data: <?php $flds = array('co', 'useraddr', 'username', 'useremail', 'yopen', 'jobdes', 'salar', 'position'); $tmp = array(); foreach ($flds as $fld) $tmp[] = $_POST[$fld]; $comments = implode(', ',$tmp); ?> A note to "fert" -- one line answers with no explanations are not very helpful, especially for newer coders. Ken Link to comment https://forums.phpfreaks.com/topic/43073-collecting-html-form-info/#findComment-209233 Share on other sites More sharing options...
krepid Posted March 17, 2007 Author Share Posted March 17, 2007 Ken, Thanks for the code ... while testing this it did make it easier to read as you advised ... I have not had a chance to absorb the entire code, but I get the gist. I would like to ask you: if I wanted a hard return, could I use a carriage return in the foreach loop? If so, how could I insert that? - Thanks again Link to comment https://forums.phpfreaks.com/topic/43073-collecting-html-form-info/#findComment-209237 Share on other sites More sharing options...
kenrbnsn Posted March 17, 2007 Share Posted March 17, 2007 Where do you want the carriage return placed? Between each field instead of the comma? If so, replace the comma in the implode with the newline character: <?php $comments = implode("\n",$tmp) . "\n"; ?> Ken Link to comment https://forums.phpfreaks.com/topic/43073-collecting-html-form-info/#findComment-209238 Share on other sites More sharing options...
krepid Posted March 17, 2007 Author Share Posted March 17, 2007 Ken, Yes, that was what I was implying ... sorry I should have been more specific. I really appreciate your help. Unfortunately, I am not getting the form data to print as you recommended. It is still just a bunch of data without any comma separators or returns ... It reads like this: Test 6 Someplace Ave/ Someplace, State 22222 Test, the tester [email protected] Leave replacementTest/Tester $45,000 - $65,000 asdasd asdasdasd asdasd asdasdasdasdasdasd asdasdasd asdadasdasd asdasdasd Maybe I am inserting what you assisted me with incorrectly. I am pasting the code with the additions below. Thanks again. -Keith <?php $self = $_SERVER['PHP_SELF']; $co = $_POST['co']; $useraddr = $_POST['useraddr']; $username = $_POST['username']; $useremail = $_POST['useremail']; $yopen = $_POST['yopen']; $jobdes = $_POST['jobdes']; $salar = $_POST['salar']; $posistion = $_POST['position']; $flds = array('co', 'useraddr', 'username', 'useremail', 'yopen', 'jobdes', 'salar', 'position'); $tmp = array(); foreach ($flds as $fld) $tmp[] = $_POST[$fld]; $comments = implode("\n",$tmp) . "\n"; //$comments=$_POST['co'].$_POST['useraddr'].$_POST['username'].$_POST['useremail'].$_POST['yopen'].$_POST['jobdes'].$_POST['salar'].$_POST['position']; $sent = $_POST['sent']; #the HTML form $form ="<form action=\"$self\" method=\"post\">"; $form.="<table width=\"450\" border=\"0\"><br /><strong>Please list information so that PCS can better assist you</strong><br /><strong style=\"background-color:yellow; color:red;\">* Indicates a required field"; $form.="<tr><td> <strong style=\"color:red;\">* </strong><strong>Company Name:<input type=\"text\" name=\"co\""; $form.=" size=\"60\" value=\"$co\" ></strong></td></tr>"; $form.="<tr><td> <strong style=\"color:red;\">* </strong><strong>Company Address:<input type=\"text\" name=\"useraddr\" "; $form.=" size=\"60\" value=\"$useraddr\"></strong></td></tr>"; $form.="<tr><td> <strong style=\"color:red;\">* </strong><strong>Your Name and Your Title:<input type=\"text\" name=\"username\""; $form.=" size=\"60\" value=\"$username\" ></strong></td></tr>"; $form.="<tr><td><strong style=\"color:red;\">* </strong><strong>Your Email Address:</strong><input type=\"text\" name=\"useremail\""; $form.=" size=\"38\" value=\"$useremail\"></td></tr>"; $form.="<tr><td> <strong style=\"color:red;\">* </strong><strong>Why is the position available?:<input type=\"text\" name=\"yopen\""; $form.=" size=\"60\" value=\"$yopen\" ></strong></td></tr>"; $form.="<tr><td> <strong style=\"color:red;\">* </strong><strong>The Title and Job Description of the posistion available:<input type=\"text\" name=\"jobdes\""; $form.=" size=\"60\" value=\"$jobdes\" ></strong></td></tr>"; $form.="<tr><td> <strong style=\"color:red;\">* </strong><strong>Please List the Salary Range:<input type=\"text\" name=\"salar\""; $form.=" size=\"60\" value=\"$salar\" ></strong></td></tr>"; $form.="<tr><td><strong style=\"color:red;\">* </strong><strong>Please tell us about the opening you currently have, the type of applicant you desire to fill this opening, and all pertinent credentials (ex: degree, license):</strong>"; $form.="<textarea name=\"position\" cols=\"50\" rows=\"12\">"; $form.="$position</textarea></td></tr>"; $form.="<tr><td><input type=\"submit\" name=\"sent\" value=\"Send Form\">"; $form.="</form>"; $form.="</td></tr>"; $form.="</table>"; if($sent) { #set variable default value $valid=true; #check co field is not blank if( !$co ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Enter the Company Name...</strong><br />"; $valid = false; } #check field is not blank if( !$useraddr ) { $errmsg .="<strong style=\"color:red;\">ERROR ...Enter the Company address...</strong><br />"; $valid = false; } #check username field is not blank if( !$username ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Enter your name...</strong><br />"; $valid = false; } #check username field is not blank // if( !$useremail ) // { $errmsg.="<strong style=\"color:red;\">ERROR ...Enter your email address...</strong><br />"; $valid = false; } #check username field is not blank if( !$yopen ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Why is the position open?...</strong><br />"; $valid = false; } #check username field is not blank if( !$jobdes ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Enter the job description...</strong><br />"; $valid = false; } #check username field is not blank if( !$salar ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Enter the salary range...</strong><br />"; $valid = false; } #check comments field is not blank if( !$posistion ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Enter the position you need PCS to fill. </strong><br />"; $valid = false; } #check validity of email format $useremail = trim($useremail); #permitted patterns for name,domain and top-level domains $_name = "/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+"; $_host = "([-0-9A-Z]+\.)+"; $_tlds = "([0-9A-Z]){2,4}$/i"; if( !preg_match( $_name."@".$_host .$_tlds,$useremail ) ) { $errmsg.="<strong style=\"color:red;\">ERROR ...Email address has incorrect format!</strong><br />"; $valid=false; } } #if not valid write the error message/s and repeat the form if($valid != true) { echo( $errmsg.$form ); } else #if the form is valid send the email { #recipients email address $to = "[email protected]"; #subject of the message $re = "Feedback from $username"; #message from the feedback form $msg = $comments; #set the Content-type header for HTML mail $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/html;"; $headers .= "charset=\"iso-8859-1\"\r\n"; #set the From header $headers .= "From: $useraddr \r\n"; #send the email now... if(mail($to,$re,$msg, $headers)) { echo("Your information has been sent - <strong style=\"color:1b6e50;\">Thanks $username</strong><br />Click <a href=\"http://www.example.com\">Here</a> to revisit the homepage");} } ?> Link to comment https://forums.phpfreaks.com/topic/43073-collecting-html-form-info/#findComment-209246 Share on other sites More sharing options...
krepid Posted March 17, 2007 Author Share Posted March 17, 2007 I have not had much luck with form data collection, and I wanted to know if it were possible to add a line-break so that the data collected is easier to read? Or - should I collect the form data differently so that it avoids this problem? Link to comment https://forums.phpfreaks.com/topic/43073-collecting-html-form-info/#findComment-209409 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.