display two fields as one, in a pull-down menu
12-13-2010, 01:17 PM
|
display two fields as one, in a pull-down menu
|
Posts: 19
|
I have a pull down menu in an HTML form. I want to populate that pull down with names from a database. I have it working pullin just the frist name, but I need it to pul both First and Last name from the database, and they are two different fields. here is what I have so far...
PHP Code:
<?php
echo '<select name= "First_Name" , "Last_Name">';
while( $array = mysql_fetch_assoc($result) ) {
echo "<option value=\"{$array['value']}\">{$array['First_Name']}</option>\n";
}
echo '</select>';?>
This is working and pulling only the First Name into the pull down. I need it to pull the last name too, but I do not know how to code it to display both.
Please help.
|
|
|
|
12-13-2010, 01:32 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 879
Name: Paul W
|
$text_for_select = $array["First_Name"] . " " . $array["Last_Name"];
Last edited by PaulW; 12-13-2010 at 01:33 PM..
|
|
|
|
12-13-2010, 01:42 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 19
|
Quote:
Originally Posted by PaulW
$text_for_select = $array["First_Name"] . " " . $array["Last_Name"];
|
Nope. When I add what yo suggested:
PHP Code:
<?php
echo '<select name= "First_Name" , "Last_Name">';
while( $array = mysql_fetch_assoc($result) ) {
echo "<option value=\"{$array['value']}\">{$array['First_Name'] . " " . $array['Last_Name']}</option>\n";
}
echo '</select>';?>
</p>
It gives me this error:
Parse error: syntax error, unexpected '.', expecting '}' in /home5/windfal2/public_html/ledge/wp-content/themes/arras/letter.php on line 72
This being line 72:
PHP Code:
echo "<option value=\"{$array['value']}\">{$array['First_Name'] . " " . $array['Last_Name']}</option>\n";
|
|
|
|
12-13-2010, 02:08 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 879
Name: Paul W
|
Check private messages
Last edited by PaulW; 12-13-2010 at 02:11 PM..
|
|
|
|
12-13-2010, 02:13 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 19
|
Quote:
Originally Posted by PaulW
Check private messages
|
Uh... I'm confused. Can you provide an example?
|
|
|
|
12-13-2010, 02:16 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 879
Name: Paul W
|
Odd things going on when I type code  Do the assignment inside the loop and follow it with the echo for the option line but use the variable $str in value and option display text -- no array references.
|
|
|
|
12-13-2010, 02:23 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 19
|
Quote:
Originally Posted by PaulW
Odd things going on when I type code  Do the assignment inside the loop and follow it with the echo for the option line but use the variable $str in value and option display text -- no array references.
|
I am not exactly sure what you want me to do. Currently this is working:
PHP Code:
<?php
echo '<select name= "First_Name" , "Last_Name">';
while( $array = mysql_fetch_assoc($result) ) {
echo "<option value=\"{$array['value']}\">{$array['First_Name']}</option>\n";
}
echo '</select>';?>
Except for the fact that I can't get it to pull the last name. All I need is to be able to pull multiple fields into the select menu. Just First and Last Name. I am not sure what you are asking me to do or where. I am fairly new to PHP, I'm sorry.
|
|
|
|
12-13-2010, 02:41 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 879
Name: Paul W
|
My fault: I can't type code in here at the moment -- have a look at http://www.mredding.co.uk/pw2.html
|
|
|
|
12-13-2010, 02:52 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 19
|
Aha!!!! Thank you so much!! That did the trick. Sorry I was so confused. I understand your previous posts now.
Now. I have one more thing I am wanting to do. When someone selects the name from the drop-down, i want the form to send to the address related to the selection. The emails are already in the Database. Any I idea how to send that address to the $to variable after the drop-down selection is made?
|
|
|
|
12-13-2010, 02:56 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 879
Name: Paul W
|
Is there a unique id for each record?
|
|
|
|
12-13-2010, 02:59 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 19
|
Quote:
Originally Posted by PaulW
Is there a unique id for each record?
|
Currently there is not. I created the database from a Excel Spreadsheet. Should I add one?
|
|
|
|
12-13-2010, 03:14 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 879
Name: Paul W
|
Yes, (and pretty much always for a db table). Having done that, add the id to your initial db query and look at the link I sent before -- I've added some more below the first bit I gave.
|
|
|
|
12-13-2010, 03:44 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 19
|
Quote:
Originally Posted by PaulW
Yes, (and pretty much always for a db table). Having done that, add the id to your initial db query and look at the link I sent before -- I've added some more below the first bit I gave.
|
When I do that, it creates two entries for each name in the database. Plus, I am not sure what you mean with the explode. how exactly does that work. And do I put that in the form?
|
|
|
|
12-13-2010, 03:57 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 879
Name: Paul W
|
Re two entries: not sure what you're doing. (The problem if you don't have that primary key is that duplicate names can't be handled. ) When the form is submitted and you're doing whatever, put the explode in there, get the email from the database and send your email. (ie it isn't part of the form presentation, it's part of the form submission processing).
|
|
|
|
12-13-2010, 04:06 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 19
|
Ok, so here is what I have so far. No longer pulling duplicates. (That was my fault)
PHP Code:
<?php
echo '<select name= "First_Name" , "Last_Name">';
while( $array = mysql_fetch_assoc($result) ) {
$text_for_select = $array["First_Name"] . " " . $array["Last_Name"]. " " . $array["District"];
$value_for_select = $array["First_Name"] . " " . $array["Last_Name"] . "_" . $array["id"];
echo "<option></option>\n";
echo "<option value=\"$value_for_select\">$text_for_select</option>\n";
}
echo '</select>';?>
Seems to be working just fine.
Now here is what my submission code looks like:
PHP Code:
<?php
if($_POST){
$to = $email;
$subject = "WHAT SHOULD THIS BE";
$message = "Date: $date\n\r".
"Dear $First_Name, $Last_Name,\n\r".
"Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah.Blah Blah BLah..\n\r".
"Sincerely,\n".
"$name \n".
"$street \n".
"$city, $zip \n".
"$email \\n".
$headers = "From: $email";
mail($to, $subject, $message, $headers);
// SUCCESS!
echo '<p class="notice">'.
'Thank you for your submission. '.
'</p>';
// clear out the variables for good-housekeeping
unset($date,$legislator,$bill,$name,$street,$city,$zip,$email);
$_POST = array();
}
?>
Where would I place the explode code? And also what would I have the $to field equal to?
|
|
|
|
12-13-2010, 04:34 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 879
Name: Paul W
|
I need more Lemsip. In your original form, put the name attribute of the SELECT to something like "lazer_select", not what you currently have. Put the explode line at the start of the submission processing but where I've put value_for_select in the _POST reference, put lazer_select. Then you do the db lookup to get the email and name that goes with the id. Okay, I'm off to bed to feel fluey  Good luck.
|
|
|
|
12-13-2010, 05:45 PM
|
Re: display two fields as one, in a pull-down menu
|
Posts: 19
|
Hmm... anyone else have any ideas?
|
|
|
|
|
« Reply to display two fields as one, in a pull-down menu
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|