Jump to content

Recommended Posts

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!
Link to comment
https://forums.phpfreaks.com/topic/34594-need-help-with-array-mysql/
Share on other sites

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
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.
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!
[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]
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?
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];

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());
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);
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'.
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
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.