hinrg Posted June 9, 2011 Share Posted June 9, 2011 Hi All, I have a form which is a list of products being pulled from a database, it displays the product with a checkbox the user can then selected the item they want to enquire about and send them to the contact form the problem is it works fine the the 1st time but if the user goes to select more products the 1st selected products are cleared. products page <?php include("../../datalogin.php"); include("../../header.php"); ?> <div id="products-menu"> <ul> <li id="FS"><a href="../../seafood/fish">FRESH FISH</a></li> <li id="SC"><a href="../../seafood/scallops">SCALLOPS LIVE</a></li> <li id="RO"><a href="../../seafood/roe">FRESH ROE</a></li> <li id="MA"><a href="../../seafood/marinated">MARINATED</a></li> </ul> </div> <!-- end menu --> <div id="product_list"> <form action="/clamms/contact.php" method="post"> <table align = "center" width = "100%" border="0" style="font-size: 12px; margin-top: 10px; font-family: Tahoma; color: #464646;"> <tr> <?php $result_resource = mysql_query("SELECT * FROM products_fresh_fish WHERE stock_category = 'FP' OR stock_category ='FS' ORDER BY stock_description") or die(mysql_error()); $count = 1; while($data = mysql_fetch_array($result_resource)) { if($count % 2 <> 0) { ?> <td><?php echo $data['stock_code'] ?></td> <td><input type="checkbox" name="products[]" value="<?php echo $data['stock_description'] ?>" /> <?php echo $data['stock_description'] ?></td> <td><?php echo $data['unit_of_measure'] ?></td> <?php } else { ?> <td><?php echo $data['stock_code'] ?></td> <td><input type="checkbox" name="products[]" value="<?php echo $data['stock_description'] ?>" /> <?php echo $data['stock_description'] ?></td> <td><?php echo $data['unit_of_measure'] ?></td> </tr><tr> <?php } $count++; } mysql_close($con); ?> </tr> </table> <input type="submit" name="formSubmit" value="Submit Enquiry" /> </form> </div><!-- end products --> <?php include("../../footer.php"); ?> contact.php <?php session_start(); $your_email ='[email protected]';// <<=== update to your email address $errors = ''; $name = ''; $visitor_email = ''; $visitor_phone = ''; $user_message = ''; $_SESSION['data'] = $_POST['products']; function displayProducts() { $quote =$_SESSION['data']; if(empty($quote)) { echo("No products selected."); } else { $N = count($quote); for($i=0; $i < $N; $i++) { echo ($quote[$i] . "\n" ); $_SESSION['saveddata']; } } } if(isset($_POST['submit'])) { $name = $_POST['name']; $visitor_email = $_POST['email']; $visitor_phone = $_POST['phone']; $user_message = $_POST['message']; ///------------Do Validations------------- if(empty($name)||empty($visitor_email)||empty($visitor_phone)) { $errors .= "\n Name, Email and Phone are required fields. "; } if(IsInjected($visitor_email)) { $errors .= "\n Bad email value!"; } if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0) { //Note: the captcha code is compared case insensitively. //if you want case sensitive match, update the check above to // strcmp() $errors .= "\n The captcha code does not match!"; } if(empty($errors)) { //send the email $to = $your_email; $subject="$name completed the web form"; $from = $visitor_email; $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''; $body = "A user $name submitted the contact form:\n". "Name: $name\n". "Email: $visitor_email \n". "Message: \n ". "$user_message\n". "IP: $ip\n". "Item: \n". "$quote\n"; $headers = "From: $from \r\n"; $headers .= "Reply-To: $visitor_email \r\n"; mail($to, $subject, $body,$headers); header('Location: thank-you.html'); } } // Function to validate against any email injection attempts function IsInjected($str) { $injections = array('(\n+)', '(\r+)', '(\t+)', '(%0A+)', '(%0D+)', '(%08+)', '(%09+)' ); $inject = join('|', $injections); $inject = "/$inject/i"; if(preg_match($inject,$str)) { return true; } else { return false; } } ?> <?php include("header.php"); ?> <?php if(!empty($errors)){ echo "<p class='err'>".nl2br($errors)."</p>"; } ?> <div id='contact_form_errorloc' class='err'></div> <form id='clamms_contact_form' method="post" name="contact_form" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"> <p> <label>Name: </label><br /> <input type="text" name="name" size="50" value='<?php echo htmlentities($name) ?>' /> </p> <p> <label>Email: </label><br /> <input type="text" name="email" size="50" value='<?php echo htmlentities($visitor_email) ?>' /> </p> <p> <label>Phone: </label><br /> <input type="text" name="phone" size="50" value='<?php echo htmlentities($visitor_phone) ?>'/> </p> <p> <label>Message:</label> <br /> <textarea name="message" rows="8" cols="50"><?php echo htmlentities($user_message) ?></textarea> </p> <p> <label>Items of interest:</label> <br /> <textarea name="quote" rows="5" cols="50"><?php echo htmlentities(displayProducts()) ?></textarea> </p> <p> <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' alt="captach image" /> <br /> <label>Enter the code above here :</label> <br /> <input id="six_letters_code" name="6_letters_code" type="text" /> <br /> <small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </p> <input type="submit" value="Submit" name='submit' /> </form> <script language="JavaScript" type="text/javascript"> // Code for validating the form // Visit http://www.javascript-coder.com/html-form/javascript-form-validation.phtml // for details var frmvalidator = new Validator("contact_form"); //remove the following two lines if you like error message box popups frmvalidator.EnableOnPageErrorDisplaySingleBox(); frmvalidator.EnableMsgsTogether(); frmvalidator.addValidation("name","req","Please provide your name"); frmvalidator.addValidation("email","req","Please provide your email"); frmvalidator.addValidation("email","email","Please enter a valid email address"); frmvalidator.addValidation("phone","req","Please provide your phone"); </script> <script language='JavaScript' type='text/javascript'> function refreshCaptcha() { var img = document.images['captchaimg']; img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000; } </script> <noscript> Code from the <a href='http://www.html-form-guide.com/contact-form/html-contact-form-captcha.html' >php contact form</a> article. </noscript> <?php include("footer.php"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/238841-sessions-being-cleared-when-adding-more-items/ Share on other sites More sharing options...
teynon Posted June 9, 2011 Share Posted June 9, 2011 I don't see session_start(); on the product page. Also, you are reseting it each time: $_SESSION['data'] = $_POST['products']; Quote Link to comment https://forums.phpfreaks.com/topic/238841-sessions-being-cleared-when-adding-more-items/#findComment-1227248 Share on other sites More sharing options...
hinrg Posted June 9, 2011 Author Share Posted June 9, 2011 arhhh yep its in my header.php Quote Link to comment https://forums.phpfreaks.com/topic/238841-sessions-being-cleared-when-adding-more-items/#findComment-1227249 Share on other sites More sharing options...
hinrg Posted June 9, 2011 Author Share Posted June 9, 2011 I don't see session_start(); on the product page. Also, you are reseting it each time: $_SESSION['data'] = $_POST['products']; How would i go about not resetting it? Quote Link to comment https://forums.phpfreaks.com/topic/238841-sessions-being-cleared-when-adding-more-items/#findComment-1227250 Share on other sites More sharing options...
teynon Posted June 9, 2011 Share Posted June 9, 2011 Use array_merge: http://php.net/manual/en/function.array-merge.php Quote Link to comment https://forums.phpfreaks.com/topic/238841-sessions-being-cleared-when-adding-more-items/#findComment-1227252 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.