SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP and JS in drop-down menu

    I posted this in the PHP forum, but no one replied and I realize this probably belongs here in the JS forum.

    I have a table in my database called "emails" that has an "id" row, a "name" row, and an "email" row.

    I then run a mysql query to select emails and names and ready them for display in a drop-down menu:
    PHP Code:
    $sql mysql_query("SELECT * FROM emails ORDER BY name ASC" );
    $altOptions "";
    while (
    $output mysql_fetch_array($sql)) {
    $altOptions .= "<option value=\"" $output['email'] . "\">" $output['name'] . "</option>";

    I then have this drop-down menu, a tekstbox, and a textarea:
    Code:
    <select name="altusers" class="input" onChange="altemailinsert(document.form.altusers.options[document.form.altusers.selectedIndex].value)">
    <option value="">> Select email <</option>
    <?=$altOptions;?>
    </select>
    
    <input type="text" size="40" name="altemail" class="input">
    <input type="button" value="Insert" class="button" onClick="newsinsert('<a href=""&quot;mailto:' + document.form.altemail.value + '&quot;>Name</a>');">
    
    <textarea name="newstext" rows="17" cols="96" class="input"><?echo $newstext?></textarea>
    What I want is for people to select a name from the drop-down menu, instantly the email of that name is shown in the textbox and then insert it into the textarea. That works almost fine with the code above. If someone selects the name "SitePoint" and the email me@sitepoint.com is shown, then "<a href="mailto:me@sitepoint.com">Name</a> is inserted. That is almost correct.

    However, what I want it to insert is this:
    "<a href="email.php?user=1">Sitepoint</a>" if the name Sitepoint was selected and that row had id 1 in the database table. I still want the email from that row to be shown in the textbox though.

    Is this possible? I can't think of a solution unless an option tag could have two values and that's not possible as far as I know.

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lots of possibilities. Could you post a sample of generated (i.e., client-delivered) HTML, complete with those functions? Makes it simpler...

    You can use an option value/index as a pointer to as many arrays as you like.
    ::: certified wild guess :::

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    407
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the generated code, I removed most of the HTML and those parts of the form that are not important. I hope this is what you wanted

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <TITLE></TITLE>
    <STYLE TYPE="text/css">
    <!--
    @import url(styles.css);
    -->
    </STYLE>
    <SCRIPT TYPE="text/javascript">
    function newsinsert(element) {
    		document.form.newstext.value = document.form.newstext.value + element; }
    </SCRIPT>
    <SCRIPT TYPE="text/javascript">
    function altemailinsert(element) {
    		document.form.altemail.value = element; }
    </SCRIPT>
    
    </HEAD>
    <body link="#000080" topmargin="0" leftmargin="0" bgcolor="#FFFFFF" 
    background="http://www.worldofmi.com/bg.jpg" bgproperties="fixed">
    
    <center class="headlinelrg">Add news</center><p>
    <div align="center">
    <table width="79%" cellspacing="0" cellpadding="0" class="border">
    <tr>
    <td>
     <form action="/admin/news/addnews.php" method="post" name="form">
    
    
    <select name="altusers" class="input" 
    onChange="altemailinsert(document.form.altusers.options[document.form.altusers.selectedInd
    ex].value)">
    
    <option value="">> Select email <</option>
    <option value="carla@domain.com">Carla</option>
    <option value="david@domain.com">David</option>
    <option value="matthew@domain.com">Matthew</option>
     
    </select>
     
    <input type="text" size="40" name="altemail" class="input">
    <input type="button" value="Insert" class="button" onClick="newsinsert('<a 
    href=""&quot;mailto:' + document.form.altemail.value + '&quot;>Name</a>');">
    <br>
    <br>
    <center>
    <b class="text">News Text:</b>
    <br>
    <textarea name="newstext" rows="17" cols="96" class="input"></textarea>
    <br>
    <input type="submit" name="preview" value="Preview" class="button">
    
     </form>
    </td>
    </tr>
    </table>
    </div>
     
    </BODY>
    </HTML>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •