Bluehost.com Web Hosting $6.95

RE: [PHP] Multiple values in a form option field

This is a discussion on RE: [PHP] Multiple values in a form option field within the PHP General forums, part of the PHP Programming Forums category; On Thursday, November 13, Jeff wrote: [snip]What I now need to do is store not only the userID but ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-13-2003
Pablo Gosse
 
Posts: n/a
Default RE: [PHP] Multiple values in a form option field

On Thursday, November 13, Jeff wrote:

[snip]What I now need to do is store not only the userID but the email
address as well. Is there a way using a form to store 2 values using
one drop down box? So basically I want to store the userID and the
Email address to a variable in the form so that both may be written to
different
fields in the database later.[/snip]

Hi Jeff. For this you'll need to use javascript and hidden form fields.
I've had to do this in a few places in my CMS and it's not too
difficult.

The Javascript will be something like this:

<script type="text/javascript">
<!--
// x is the select list in question
function setEmail(x)
{
var users = new Array();
<?php
$count = 0;
foreach ($users as $id=>$user)
{
echo 'users['.$count.'] = '.$id.';';
$count++;
}
?>

for (i=0;i<x.length;i++)
{
if (x.options[i].selected) hiddenidfield.value =
users[i];
}
}
//-->
</script>

One thing to make sure of is that you are looping through your results
in the same order both when you create the javascript array inside the
function, and also when creating the select list.

Then in your form have a hidden input as follows:

<input type="hidden" name="id">

And for the select list, use the onchange attribute to set the value of
the hidden input field (onchange="setEmail(this);").

You'll obviously need to update hiddenidfield.value with the proper
reference to this input in the elements collection for this form.

I'm writing this off the top of my head, so if there are any problems
let me know. It should be pretty close though as I had to do just what
you asked a few times recently.

Cheers,
Pablo
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:45 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0