March 25, 201016 yr Hi, I have created an online form with a series of check boxes. When a client submits the form an email is received with all details. However, all the check boxes have a value of 'On'. Can I change this to 'Yes' somehow? Many thanks!
March 25, 201016 yr foreach($_POST['checkboxes_id_here'] as &$val) { if($val == 'On') $val = 'Yes'; } That should do it
March 25, 201016 yr Author Legend! Thank you!! Where should I put it in the following code? <?php $my_email = "info@example.com"; $from_email = "Example Company"; $continue = "thank_you.html"; $errors = array(); if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}} if(isset($_REQUEST['email']) && !empty($_REQUEST['email'])) { $_REQUEST['email'] = trim($_REQUEST['email']); if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}} } if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";} function recursive_array_check_blank($element_value) { global $set; if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}} else { foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);} } } recursive_array_check_blank($_REQUEST); if(!$set){$errors[] = "You cannot send a blank form";} unset($set); if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;} if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");} function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");} $message = build_message($_REQUEST); $message = stripslashes($message); $subject = "New Order Form Enquiry"; $subject = stripslashes($subject); if($from_email) { $headers = "From: " . $from_email; $headers .= PHP_EOL; $headers .= "Reply-To: " . $_REQUEST['email']; } else { $from_name = ""; if(isset($_REQUEST['name']) && !empty($_REQUEST['name'])){$from_name = stripslashes($_REQUEST['name']);} $headers = "From: {$from_name} <{$_REQUEST['email']}>"; } mail($my_email,$subject,$message,$headers); ?>
March 25, 201016 yr Right at the top of the script. can I ask where you learnt to use code like that as it is massively outdated
March 25, 201016 yr Author Right at the top of the script. can I ask where you learnt to use code like that as it is massively outdated It was a free online form that I've used for years. It's just the one I'm used to using I guess!! Thank you for your help mate! EDIT: Code didn't work at the top (just above $my_email)
March 25, 201016 yr ok, can you put this at the top of your script temporarily echo '<pre>'.print_r($_POSt, true).'</pre>'; die(); and print out the output it generates (delete any sensitive information if you want, it's the keys of the array that I'm interested in) Can you then delete the code from your script, and paste exactly what you have now
March 26, 201016 yr Author Hi Jay, I added the code you provided to the top of the script but when I submit the form, all I get now is an empty blank page. The source simply reads <pre></pre>
March 26, 201016 yr If this php is on the same page as your form can you delete the die() line. Otherwise can you submit your form (Also if the form method of sending data is GET can you change $_POST to $_GET)
March 26, 201016 yr Author Hi Jay, the form is in contact.html file whilst the PHP script is in contact.php file. I've edited the PHP script in the contact.php file as advised and when I submit the form, all I get is a blank page. EDIT: Oh and method is get.
March 26, 201016 yr ok can you show your contact.html page code (only the <form> to </form> is required)
March 26, 201016 yr Author Cheers Jay! Here's the code: <form class="cmxform" id="orderform" method="post" action="../order/order.php"> <table width="92%" border="0" align="center" cellpadding="3"> <tr> <td> <label>Contact name</label> <input type="text" style="width:181px; float:right;" name="contact name" class="required" /> </td> </tr> <tr> <td> <label>Company</label> <input type="text" style="width:181px; float:right;" name="company" class="required" /> </td> </tr> <tr> <td> <label>Email</label> <input type="text" style="width:181px; float:right;" name="email" class="required" /> </td> </tr> <tr> <td> <label>Phone number</label> <input type="text" style="width:181px; float:right;" name="phone number" class="required" /> </td> </tr> <tr> <td class="right"> <label>Enquiring about</label> <textarea rows="4" name="enquiring_about" cols="30" style="float:right;" class="required"></textarea> </td> </tr> <tr> <td style="padding-top: 15px; float:right;"> <input class="submit" type="submit" value="Submit"/> <input type="reset" name="clear" id="clear" value="Clear" /> </td> </tr> <tr> <td> <label>Add me to your newsletter mailing list</label> <input type="checkbox" style="float:right" name="Newsletter sign up" /> </td> </tr> </table> </form>
March 26, 201016 yr Well you've said that the method is GET but according to your form it's POST So with your form you only have one value you want changing to Yes from On and that's the newsletter signup. Firstly change the newsletter signup name to signup in the form then use the following at the top of your php script if(isset($_POST['signup']) && $_POST['signup'] == 'On')\ $_POST['signup'] = 'Yes'; That should hopefully sort this. I'm not sure how you were getting nothing earlier with the <pre> code I had above if you submitted the form. It's very strange tbh
March 26, 201016 yr Author Hi Jay, I've added the code, submitted the form but on the thank you page I now get the following error at the top of the screen: Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /homepages/18/d229507269/htdocs/customers/customername/order/order.php on line 80
March 26, 201016 yr Wow, I've never seen that error message before. Can you paste the current script you have now in full for the php file
March 26, 201016 yr OK it's just the line I gave you from the looks of it. This must have added a \ for some reason. Remove the \ before $_POST in the line I posted above
March 26, 201016 yr Author Here you go Jay! <?php $my_email = "xxxx@hotmail.com"; $from_email = "xxxxxxxx"; $errors = array(); if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}} if(isset($_REQUEST['email']) && !empty($_REQUEST['email'])) { $_REQUEST['email'] = trim($_REQUEST['email']); if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}} } if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";} function recursive_array_check_blank($element_value) { global $set; if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}} else { foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);} } } recursive_array_check_blank($_REQUEST); if(!$set){$errors[] = "You cannot send a blank form";} unset($set); if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;} if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");} function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");} $message = build_message($_REQUEST); $message = stripslashes($message); $subject = "xxxxxxxxxxxxxx"; $subject = stripslashes($subject); if($from_email) { $headers = "From: " . $from_email; $headers .= PHP_EOL; $headers .= "Reply-To: " . $_REQUEST['email']; } else { $from_name = ""; if(isset($_REQUEST['name']) && !empty($_REQUEST['name'])){$from_name = stripslashes($_REQUEST['name']);} $headers = "From: {$from_name} <{$_REQUEST['email']}>"; } mail($my_email,$subject,$message,$headers); if(isset($_POST['signup']) && $_POST['signup'] == 'On') $_POST['signup'] = 'Yes'; ?>
March 26, 201016 yr That line needs to go at the TOP of your code, not the bottom It's pointless changing the text after you've sent the email
March 26, 201016 yr ok well last attempt is to change the $_POST to $_REQUEST to see if that works, although if it does there's something wacky going on I'll also point out that you shouldn't be using tables to format your form neatly. It can be accomplished with css (but we'll leave that for now )
March 26, 201016 yr can you do echo '<pre>'.print_r($_SERVER, true).'</pre>'; at the top of the script and submit the contact form and print the output here
March 26, 201016 yr Author Thanks Jay. Here you go!! Array ( [CONTENT_LENGTH] => 599 [CONTENT_TYPE] => application/x-www-form-urlencoded [DBENTRY] => /kunden/homepages/18/d229507269/htdocs:d0000#CPU 6 #MEM 10240 #CGI 346 #NPROC 12 #TAID 47675017 #WERB 0 #LANG 1 #STAT 1 [DOCUMENT_ROOT] => /kunden/homepages/18/d229507269/htdocs [HTTP_ACCEPT] => application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5 [HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.3 [HTTP_ACCEPT_ENCODING] => gzip,deflate,sdch [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.8 [HTTP_CACHE_CONTROL] => max-age=0 [HTTP_CONNECTION] => keep-alive [HTTP_COOKIE] => __utmz=50933499.1268353853.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=50933499.1321877029.1268353853.1269082453.1269388828.7 [HTTP_HOST] => www.creativewales.com [HTTP_ORIGIN] => http://www.creativewales.com [HTTP_REFERER] => http://www.creativewales.com/customers/xxxxxxxxxxxx/order/index.html [HTTP_USER_AGENT] => Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.5 (KHTML, like Gecko) Chrome/4.1.249.1042 Safari/532.5 [PATH] => /bin:/usr/bin [REDIRECT_DBENTRY] => /kunden/homepages/18/d229507269/htdocs:d0000#CPU 6 #MEM 10240 #CGI 346 #NPROC 12 #TAID 47675017 #WERB 0 #LANG 1 #STAT 1 [REDIRECT_SCRIPT_URI] => http://www.creativewales.com/customers/xxxxxxxxxxxx/order/order.php [REDIRECT_SCRIPT_URL] => /customers/xxxxxxxxxxxx/order/order.php [REDIRECT_STATUS] => 200 [REDIRECT_UNIQUE_ID] => S6zwIdTjHRYAAFhwuMM [REDIRECT_URL] => /customers/xxxxxxxxxxxx/order/order.php [REMOTE_ADDR] => 87.115.93.99 [REMOTE_PORT] => 51096 [sCRIPT_FILENAME] => /kunden/homepages/18/d229507269/htdocs/customers/xxxxxxxxxxxx/order/order.php [sCRIPT_URI] => http://www.creativewales.com/customers/xxxxxxxxxxxx/order/order.php [sCRIPT_URL] => /customers/xxxxxxxxxxxx/order/order.php [sERVER_ADDR] => 212.227.33.151 [sERVER_ADMIN] => webmaster@creativewales.com [sERVER_NAME] => creativewales.com [sERVER_PORT] => 80 [sERVER_SIGNATURE] => [sERVER_SOFTWARE] => Apache [uNIQUE_ID] => S6zwIdTjHRYAAFhwuMM [GATEWAY_INTERFACE] => CGI/1.1 [sERVER_PROTOCOL] => HTTP/1.1 [REQUEST_METHOD] => POST [QUERY_STRING] => [REQUEST_URI] => /customers/xxxxxxxxxxxx/order/order.php [sCRIPT_NAME] => /customers/xxxxxxxxxxxx/order/order.php [PATH_INFO] => /customers/xxxxxxxxxxxx/order/order.php [PATH_TRANSLATED] => /kunden/homepages/18/d229507269/htdocs/customers/xxxxxxxxxxxx/order/order.php [sTATUS] => 200 [php_SELF] => /customers/xxxxxxxxxxxx/order/order.php [argv] => Array ( ) [argc] => 0 )
March 26, 201016 yr Author LOL! Here you go! Bare in mind it's an order form for food. The checkbox I ticked was the top one (antipasti). All the HTML and form etc is correct. Array ( [Preferred_Menu] => Classic Light Lunch [antipasti] => on [still_mineral_water_(1_ltr_bottle)] => [sparkling_mineral_water_(1_ltr_bottle)] => [Orange_juice_(1_ltr_carton)] => [Pineapple_juice_(1_ltr_carton)] => [Grapefruit_juice_(1_ltr_carton)] => [Apple_juice_(1_ltr_carton)] => [Tomato_juice_(1_ltr_carton)] => [Elderflower_Presse_(75cl_bottle)] => [Cranberry_Juice_Drink_(1_ltr_carton)] => [contact_name] => Aled Mann [phone_number] => 07xxxxxxxxx [email] => aledmann@xxxxxxxxxx.com [delivery_address] => here [Date] => 25/03/2010 [Time_of_lunch] => 1pm [How_many_people] => 29 [Plates] => Paper Plates [serviettes] => No thanks [anything_else] => no [__utmz] => 50933499.1268353853.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none) [__utma] => 50933499.1321877029.1268353853.1269082453.1269388828.7 )
March 26, 201016 yr OK Try this at the top of your script foreach($_REQUEST as &$value) { if(strtolower($value) == 'on') $value = 'Yes'; } Fingers crossed...............
March 26, 201016 yr Author Nooooooooo! Parse error: syntax error, unexpected '&', expecting T_VARIABLE or '$' in /homepages/18/d229507269/htdocs/customers/xxxxxxx/order/order.php on line 3
March 26, 201016 yr foreach($_REQUEST as $k => $value) { if(strtolower($value) == 'on') $_REQUEST[$k] = 'Yes'; } If that doesn't work I give in What version of php are you running?
March 26, 201016 yr Author Oooooooh my God.....IT WORKED!! LOL! You are a true PHP Jedi master. Thanks a lot for all your help Jay, really, you went above and beyond what most would have done! Thanks again!
Create an account or sign in to comment