simcoweb Posted January 17, 2007 Share Posted January 17, 2007 I have a form for contacting the site owner for an appointment. There are 3 drop down menu selectors in the form where the code is like so:[code]<?php<tr> <td class="body" align="left">Appointment is for:</td> <td class="body" align="center"><select value="appt_for[]"> <option value="Myself">Myself</option> <option value="My Son">My Son</option> <option value="My Daughter">My Daughter</option> <option value="Other">Other</option> </select></td> </tr> <tr> <td class="body" align="left">Best Days For You (select all that apply):</td> <td class="body" align="center"><input type="checkbox" name="best_days[]" value="Monday">Monday <input type="checkbox" name="best_days[]" value="Tuesday">Tuesday <br> <input type="checkbox" name="best_days[]" value="Wednesday">Wednesday <input type="checkbox" name="best_days[]" value="Thursday">Thursday <br> <input type="checkbox" name="best_days[]" value="Friday">Friday <input type="checkbox" name="best_days[]" value="Saturday">Saturday </td> </tr> <tr> <td class="body" align="left">Best Time For You:</td> <td class="body" align="center"><select value="best_time"> <option value="Before Noon" selected name="best_time[]">Before Noon</option> <option value="Afternoon before 5" name="best_time[]">Afternoon before 5</option> <option value="Evenings after 5" name="best_time[]">Evenings after 5</option> </select> </td> </tr>?>[/code]When I try to insert the results of these 3 fields into my database they are blank. Here's a sample of the query:[code]<?php{ $name = $_POST['name']; $email = $_POST['email']; $phone = $_POST['phone']; $appt_for = $_POST['appt_for']; $best_days = $_POST['best_days']; $best_time = $_POST['best_time']; $message = $_POST['message']; mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error()); $sql = "INSERT INTO schedule VALUES ('', '$name', '$email', '$phone', '$appt_for', '$best_days', '$best_time', '$message', '$date')"; $results = mysql_query($sql) or die("Error occurred: ".mysql_error()); if (!results) { echo "An error occured while attempting to send your email request. Please try again.";}}?>[/code]I need some guidance on 1) if the form is laid out properly for the select fields regarding the array format, 2) how the selection would be inserted into the database.Thanks! Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 17, 2007 Share Posted January 17, 2007 may i ask what is the 'blank insert before the name? because id primary keys dont need inserts...Ted Quote Link to comment Share on other sites More sharing options...
simcoweb Posted January 17, 2007 Author Share Posted January 17, 2007 It's an auto-incremented id field. If I don't place the blank ' ' there I get a column count error. Quote Link to comment Share on other sites More sharing options...
marcus Posted January 17, 2007 Share Posted January 17, 2007 You should check if they're blank.And don't put PHP tags around HTML unless you're echoing it.[code=php:0]if(!isset($varname)){echo "not there";}else {//query}[/code]Ted, if you're inserting into the database without supplying given table fields then the first field of that query would be $name in place of the id. It's best if you made the query with given values[code=php:0]$sql = "INSERT INTO table (name,email,etc) VALUES('$name','$email','$etc')";[/code]If no value is given for idea it won't go blank, but to keep that from even happening just make the id value NULL Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted January 17, 2007 Share Posted January 17, 2007 thanks, i see.Ted Quote Link to comment Share on other sites More sharing options...
simcoweb Posted January 17, 2007 Author Share Posted January 17, 2007 Ok, tested the contents of those 3 variables and they are not populating. Blank-o. Zip. Nada. So, basically I should start with the form. It's not clear to me how to set those 3 drop downs up in order to pass the data using the [] array signifier. Two of them are single choice while the checkboxes are multiple. Quote Link to comment Share on other sites More sharing options...
marcus Posted January 17, 2007 Share Posted January 17, 2007 <select value="appt_for[]"><select value="best_time">change value to name Quote Link to comment Share on other sites More sharing options...
dgiberson Posted January 17, 2007 Share Posted January 17, 2007 You also do not require the [] on the appt_for select box, unless you making it into a multiple selection box. Quote Link to comment Share on other sites More sharing options...
simcoweb Posted January 17, 2007 Author Share Posted January 17, 2007 First, thanks for the posts. I'm a bit fuzzy still. I hear (read) what you're saying but I don't comprehend totally since there's 2 different field types. Let's break it down for me if you would please.On the checkboxes, that is a multiple choice. So, first... setting up the form properly so that the selections are all passed to the results. I have one $best_days = $_POST['best_days']; but there could be multiple selections for that field. Will the variable hold all the selections? And, you mentioned changing the names to match the values plus eliminating the []'s. Here's a snippet of my code to use as an example. Can you show me exactly how you'd change it please?[code]<tr> <td class="body" align="left">Best Days For You (select all that apply):</td> <td class="body" align="center"><input type="checkbox" name="best_days[]" value="best_days">Monday <input type="checkbox" name="best_days[]" value="best_days">Tuesday <br> <input type="checkbox" name="best_days[]" value="best_days">Wednesday <input type="checkbox" name="best_days[]" value="best_days">Thursday <br> <input type="checkbox" name="best_days[]" value="best_days">Friday <input type="checkbox" name="best_days[]" value="best_days">Saturday </td> </tr>[/code]Then the drop down selectors, here's the snippet for that:[code]<tr> <td class="body" align="left">Appointment is for:</td> <td class="body" align="center"><select name="appt_for[]" value="appt_for"> <option value="appt_for">Myself</option> <option value="appt_for">My Son</option> <option value="appt_for">My Daughter</option> <option value="Other">Other</option> </select></td> </tr>[/code]Thanks in advance! Quote Link to comment Share on other sites More sharing options...
dgiberson Posted January 17, 2007 Share Posted January 17, 2007 [code]<tr> <td class="body" align="left">Best Days For You (select all that apply):</td> <td class="body" align="center"><input type="checkbox" name="best_days[]" value="best_days">Monday <input type="checkbox" name="best_days[]" value="best_days">Tuesday <br> <input type="checkbox" name="best_days[]" value="best_days">Wednesday <input type="checkbox" name="best_days[]" value="best_days">Thursday <br> <input type="checkbox" name="best_days[]" value="best_days">Friday <input type="checkbox" name="best_days[]" value="best_days">Saturday </td> </tr>[/code]Then the drop down selectors, here's the snippet for that:[code]<tr> <td class="body" align="left">Appointment is for:</td> <td class="body" align="center"><select name="appt_for"> <option value="appt_for">Myself</option> <option value="appt_for">My Son</option> <option value="appt_for">My Daughter</option> <option value="Other">Other</option> </select></td> </tr>[/code]Then where ever the form directs to :[code]if (isset($_POST['best_days')) { $best_days = $_POST['best_days']; // $best_days will now be an array.} else { //check box has not been selected, do whatever you need to here}$appt_for = $_POST['appt_for'];[/code] Quote Link to comment Share on other sites More sharing options...
simcoweb Posted January 17, 2007 Author Share Posted January 17, 2007 Bummer. Those 3 fields are just NOT populating the variables. I also have the results being inserted into the MySQL database. For those three fields I get:1. best_days (instead of the choice(s) )2. Array (just the word Array)3. blank ( as in nothing )All the other variables are fine. Name, email, phone, message. But, those three multiple selections just ain't doin it. Ideas? Quote Link to comment Share on other sites More sharing options...
dgiberson Posted January 17, 2007 Share Posted January 17, 2007 1. The checkboxes will always be returned as an array. How are you handling the best day of the week information in your database?2. My error on the select box last time. Should be: <td class="body" align="center"><select name="appt_for"> <option value="myself">Myself</option> <option value="son">My Son</option> <option value="daughter">My Daughter</option> <option value="Other">Other</option> </select></td>$appt_for = $_POST['appt_for]; Quote Link to comment Share on other sites More sharing options...
simcoweb Posted January 17, 2007 Author Share Posted January 17, 2007 I've got the best time and appointment for fields working fine and inserting proper selections in the database. The one that is still funky is the 'best_days' field which is the checkboxes. All it does is insert the word 'Array' into the database instead of the actual selections. My query looks like this:mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error()); $sql = "INSERT INTO schedule VALUES ('', '$name', '$email', '$phone', '$appt_for', '$best_days', '$best_time', '$message', '$date')"; $results = mysql_query($sql) or die("Error occurred: ".mysql_error()); Quote Link to comment Share on other sites More sharing options...
dgiberson Posted January 17, 2007 Share Posted January 17, 2007 simcoweb, how are you storing the best days information tho.... Do you want to submit a comma separated list? or insert multiple records into the table for each of the days they select?If you want to enter multiple records just use:mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error()); mysql_select_db($dbname) or die(mysql_error());or($i=0;$i<count($best_days);$i++) { $sql = "INSERT INTO schedule VALUES ('', '$name', '$email', '$phone', '$appt_for', '$best_days[$i]', '$best_time', '$message', '$date')"; $results = mysql_query($sql) or die("Error occurred: ".mysql_error());}For a comma separated list use your existing query (change the var $best_days to $bestdays) , but before that do this:for ($i=0;$i<count($best_days);$i++) { $bestdays .= $best_days[$i].",";}$len = strlen($bestdays) - 1;$bestdays = substr($bestdays,0,$len); Quote Link to comment Share on other sites More sharing options...
simcoweb Posted January 17, 2007 Author Share Posted January 17, 2007 Voila! That was the missing snippet. I opted for the comma separated list. Thanks for that.Another related question. Once the form parses it sends the contents to the site owner in mail() function. I would like to adapt that same principle to the output of the mail. Here's what I have now:[code]@mail($to, "Appointment request for " . $_POST['subject'], "Drew,\nOn " . date("r") . ", " . $_POST['name'] . " " . $_POST['email'] . " " . $_POST['phone'] ." sent the following message.\nNeed an appointment for " . $_POST['appt_for'] . " The best days for them are " .$_POST['best_days']. ". The best times are " .$_POST['best_time']. " Here are further instructions \n\n" . stripslashes($_POST['message']), "From: " . $_POST['email']);[/code]Which basically isn't parsing that array for 'best_days'. Would I adapt the same snippet to this in order to have it display the days? Right now it just comes up with the word 'Array'. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 17, 2007 Share Posted January 17, 2007 try this:[code]$body = "(your other stuff here) The best days for them are ";foreach($_POST['best_days'] AS $day=>$on){ $body .= $day.' ';}$body .= ". The best times are (more here)";[/code]Then do the mail() command with $body 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.