php/mysql array drop down selection form field

Discussion in 'PHP' started by thafroggy, Jul 17, 2008.

  1. #1
    Hello all, this is my first post. Very newbie.

    I am modifying a "simple customer" release. I would like to have my drop down (which gets the users listed) to not only show the user name but to also select another column from the same data so that info is what is written to the mysql database.

    Basicly I have two tables in mysql, one for customers (follows_contacts) and one for admin users (follows_users). I am trying to populate a field in the follows_contacts (contact_user) table with a value taken from the follows_user field (user_id). I want to be able to assign customers their own personal admin user for tracking and followup.

    I have my form pull from the mysql database table (follows_users) the field(s) user_name right now, this displas the first and last name of the admin user, I can select one and then this fills in the form field with that value and sends it and populates a field in the table (follows_users) called (user_assignment) well this is no longer working for me because I need more information about the admin user that is assigned to the customer, I am going to be adding a notification system so now I also need the admin users email address and may even need the phone number. that is why I would still like the form drop down selection box to display the admin users first and last name , pulled from the (follows_users) table , but when they select this user I want instead for the field value of (user_id) sent to the mysql table of (follows_contacts) (contact_user) field, so later I can just create a variable from this field and can use it for queries to get more information directly from the (follows_users) table using the (user_id) field for that given admin.

    Can somone help me with this?

    Thank you very very much... I am so new to this and just love PHP so far.

    <?php require_once('includes/config.php'); ?>
    <?php
    include('includes/sc-includes.php');
    $pagetitle = Contact;

    $update = 0;
    if (isset($_GET['id'])) {
    $update = 1;
    }
    ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    break;
    }
    return $theValue;
    }
    }

    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }



    //
    if ($update==1) {
    mysql_select_db($database_follows, $follows);
    $query_contact = "SELECT * FROM follows_contacts WHERE contact_id = ".$_GET['id']."";
    $contact = mysql_query($query_contact, $follows) or die(mysql_error());
    $row_contact = mysql_fetch_assoc($contact);
    $totalRows_contact = mysql_num_rows($contact);
    }
    //





    //UPLOAD PICTURE
    $picture = $_POST['image_location'];
    $time = substr(time(),0,5);
    if($HTTP_POST_FILES['image'] && $HTTP_POST_FILES['image']['size'] > 0){
    $ori_name = $_FILES['image']['name'];
    $ori_name = $time.$ori_name;
    $tmp_name = $_FILES['image']['tmp_name'];
    $src = imagecreatefromjpeg($tmp_name);
    list($width,$height)=getimagesize($tmp_name);
    $newwidth=95;
    $newheight=($height/$width)*95;
    $tmp=imagecreatetruecolor($newwidth,$newheight);
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
    $filename = "images/". $ori_name;
    imagejpeg($tmp,$filename,100);
    $picture = $ori_name;
    imagedestroy($src);
    imagedestroy($tmp);
    }
    //END UPLOAD PICTURE

    if ($update==0) {
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
    $insertSQL = sprintf("INSERT INTO follows_contacts (contact_first, contact_last, contact_date, contact_userassign, contact_image, contact_profile, contact_method, contact_street, contact_city, contact_state, contact_zip, contact_phone, contact_cell, contact_email, contact_web, contact_updated) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
    GetSQLValueString(trim($_POST['contact_first']), "text"),
    GetSQLValueString(trim($_POST['contact_last']), "text"),
    GetSQLValueString(time(), "date"),
    GetSQLValueString(trim($_POST['contact_userassign']), "text"),
    GetSQLValueString($picture, "text"),
    GetSQLValueString(trim($_POST['contact_profile']), "text"),
    GetSQLValueString(trim($_POST['contact_method']), "text"),
    GetSQLValueString(trim($_POST['contact_street']), "text"),
    GetSQLValueString(trim($_POST['contact_city']), "text"),
    GetSQLValueString(trim($_POST['contact_state']), "text"),
    GetSQLValueString(trim($_POST['contact_zip']), "text"),
    GetSQLValueString(trim($_POST['contact_phone']), "text"),
    GetSQLValueString(trim($_POST['contact_cell']), "text"),
    GetSQLValueString(trim($_POST['contact_email']), "text"),
    GetSQLValueString(trim($_POST['contact_web']), "text"),
    GetSQLValueString($_POST['contact_updated'], "int"));

    mysql_select_db($database_follows, $follows);
    $Result1 = mysql_query($insertSQL, $follows) or die(mysql_error());
    set_msg('Contact Added');
    $cid = mysql_insert_id();
    $redirect = "contact-details.php?id=$cid";
    header(sprintf('Location: %s', $redirect)); die;
    }
    }

    if ($update==1) {
    if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
    $updateSQL = sprintf("UPDATE follows_contacts SET contact_first=%s, contact_last=%s, contact_userassign=%s, contact_image=%s, contact_profile=%s, contact_method=%s, contact_street=%s, contact_city=%s, contact_state=%s, contact_zip=%s, contact_phone=%s, contact_cell=%s, contact_email=%s, contact_web=%s, contact_updated=%s WHERE contact_id=%s",
    GetSQLValueString(trim($_POST['contact_first']), "text"),
    GetSQLValueString(trim($_POST['contact_last']), "text"),
    GetSQLValueString(trim($_POST['contact_userassign']), "text"),
    GetSQLValueString($picture, "text"),
    GetSQLValueString(trim($_POST['contact_profile']), "text"),
    GetSQLValueString(trim($_POST['contact_method']), "text"),
    GetSQLValueString(trim($_POST['contact_street']), "text"),
    GetSQLValueString(trim($_POST['contact_city']), "text"),
    GetSQLValueString(trim($_POST['contact_state']), "text"),
    GetSQLValueString(trim($_POST['contact_zip']), "text"),
    GetSQLValueString(trim($_POST['contact_phone']), "text"),
    GetSQLValueString(trim($_POST['contact_cell']), "text"),
    GetSQLValueString(trim($_POST['contact_email']), "text"),
    GetSQLValueString(trim($_POST['contact_web']), "text"),
    GetSQLValueString(trim($_POST['contact_updated']), "int"),
    GetSQLValueString(trim($_POST['contact_id']), "int"));

    mysql_select_db($database_follows, $follows);
    $Result1 = mysql_query($updateSQL, $follows) or die(mysql_error());
    set_msg('Contact Updated');
    $cid = $_GET['id'];
    $redirect = "contact-details.php?id=$cid";
    header(sprintf('Location: %s', $redirect)); die;
    }
    }
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title><?php if ($update==0) { echo "Add Contact"; } ?><?php echo $row_contact['contact_first']; ?> <?php echo $row_contact['contact_last']; ?></title>
    <script src="includes/lib/prototype.js" type="text/javascript"></script>
    <script src="includes/src/effects.js" type="text/javascript"></script>
    <script src="includes/validation.js" type="text/javascript"></script>
    <script src="includes/src/scriptaculous.js" type="text/javascript"></script>
    <script language="javascript">
    function toggleLayer(whichLayer)
    {
    if (document.getElementById)
    {
    // this is the way the standards work
    var style2 = document.getElementById(whichLayer).style;
    style2.display = style2.display? "":"block";
    }
    else if (document.all)
    {
    // this is the way old msie versions work
    var style2 = document.all[whichLayer].style;
    style2.display = style2.display? "":"block";
    }
    else if (document.layers)
    {
    // this is the way nn4 works
    var style2 = document.layers[whichLayer].style;
    style2.display = style2.display? "":"block";
    }
    }
    </script>
    <link href="includes/style.css" rel="stylesheet" type="text/css" />
    <link href="includes/simplecustomer.css" rel="stylesheet" type="text/css" />
    </head>

    <body>
    <?php include('includes/header.php'); ?>
    <div class="container">
    <div class="leftcolumn">
    <h2><?php if ($update==1) { echo Update; } else { echo Add; } ?> Follow-Up </h2>
    <p>&nbsp;</p>
    <form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="form1" id="form1">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="28%">First Name

    <input name="contact_first" type="text" class="required" id="contact_first" value="<?php echo $row_contact['contact_first']; ?>" size="25" /></td>
    <td width="72%">Last Name

    <input name="contact_last" type="text" class="required" id="contact_last" value="<?php echo $row_contact['contact_last']; ?>" size="25" />
    </p></td>
    </tr>
    <tr>
    <td>First Contacted
    <!---<input name="contact_title" type="none" id="contact_title" value="--><?php echo date('F d, Y', $row_contact['contact_date']); ?><!---" size="25" />--> </td>
    <td>Contacted via

    <select name="contact_method" id="contact_method">
    <option value="">Select</option>
    <option value="New Harvest Outreach" <?php if (!(strcmp("New Harvest Outreach", $row_contact['contact_method']))) {echo "selected=\"selected\"";} ?>>New Harvest Outreach</option>
    <option value="Indvidual Outreach" <?php if (!(strcmp("Indvidual Outreach", $row_contact['contact_method']))) {echo "selected=\"selected\"";} ?>>Indvidual Outreach</option>
    <option value="Celebreate Recovery" <?php if (!(strcmp("Celebreate Recovery", $row_contact['contact_method']))) {echo "selected=\"selected\"";} ?>>Celebrate Recovery</option>
    <option value="Church Visit / Invite" <?php if (!(strcmp("Church Visit / Invite", $row_contact['contact_method']))) {echo "selected=\"selected\"";} ?>>Church Visit / Invite</option>
    <option value="182 Visit / Invite" <?php if (!(strcmp("182 Visit / Invite", $row_contact['contact_method']))) {echo "selected=\"selected\"";} ?>>182 Visit / Invite</option>
    <option value="Other" <?php if (!(strcmp("Other", $row_contact['contact_method']))) {echo "selected=\"selected\"";} ?>>Other</option>
    </select></td>
    </tr>
    <tr>
    <td colspan="2">Email

    <input name="contact_email" type="text" class="required validate-email" id="contact_email" value="<?php echo $row_contact['contact_email']; ?>" size="35" /></td>
    </tr>
    <tr>
    <td colspan="2"> <?php if ($update!=1) { ?> <p><a href="#" onclick="new Effect.toggle('morecontact', 'slide'); return false;">+Add more Follow-Up information [/url]</p><?php } ?>

    <div <?php if ($update!=1) { ?>id="morecontact" style="display:none"<?php } ?>>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td>Street

    <input name="contact_street" type="text" id="contact_street" value="<?php echo $row_contact['contact_street']; ?>" size="35" /></td>
    </tr>
    <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="39%">City

    <input name="contact_city" type="text" id="contact_city" value="<?php echo $row_contact['contact_city']; ?>" size="35" /></td>

    <td width="27%" valign="top">State

    <select name="contact_state" id="contact_state">
    <option value="">Select a state...</option>
    <option value="AL" <?php if (!(strcmp("AL", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Alabama</option>
    <option value="AK" <?php if (!(strcmp("AK", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Alaska</option>
    <option value="AZ" <?php if (!(strcmp("AZ", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Arizona</option>
    <option value="AR" <?php if (!(strcmp("AR", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Arkansas</option>
    <option value="CA" <?php if (!(strcmp("CA", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>California</option>

    <option value="PA" <?php if (!(strcmp("PA", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Pennsylvania</option>
    <option value="RI" <?php if (!(strcmp("RI", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Rhode Island</option>
    <option value="SC" <?php if (!(strcmp("SC", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>South Carolina</option>
    <option value="SD" <?php if (!(strcmp("SD", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>South Dakota</option>
    <option value="TN" <?php if (!(strcmp("TN", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Tennessee</option>
    <option value="TX" <?php if (!(strcmp("TX", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Texas</option>
    <option value="UT" <?php if (!(strcmp("UT", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Utah</option>
    <option value="VT" <?php if (!(strcmp("VT", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Vermont</option>
    <option value="VA" <?php if (!(strcmp("VA", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Virginia</option>
    <option value="WA" <?php if (!(strcmp("WA", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Washington</option>
    <option value="WV" <?php if (!(strcmp("WV", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>West Virginia</option>
    <option value="WI" <?php if (!(strcmp("WI", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Wisconsin</option>
    <option value="WY" <?php if (!(strcmp("WY", $row_contact['contact_state']))) {echo "selected=\"selected\"";} ?>>Wyoming</option>
    </select></td>
    <td width="34%">Zip

    <input name="contact_zip" type="text" id="contact_zip" value="<?php echo $row_contact['contact_zip']; ?>" size="10" /></td>
    </tr>
    </table></td>
    </tr>
    <tr>
    <td><table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td width="39%">Phone

    <input name="contact_phone" type="text" id="contact_phone" value="<?php echo $row_contact['contact_phone']; ?>" size="35" /></td>
    <td width="61%">Cell

    <input name="contact_cell" type="text" id="contact_cell" value="<?php echo $row_contact['contact_cell']; ?>" size="35" /></td>
    </tr>
    </table></td>
    </tr>
    <tr>
    <td>Image

    <input name="image" type="file" id="image" /><?php if ($row_contact['contact_image']) { ?>


    <img src="images/<?php echo $row_contact['contact_image']; ?>" width="95" />
    <?php } ?>
    </td>
    </tr>
    <tr>
    <td>Assign to Member

    <select name="contact_userassign" id="contact_userassign">
    <option value="">Assign someone to this followup</option>
    <?php
    $query_userassign = "SELECT user_name FROM follows_users WHERE 1";
    $userassign = mysql_query($query_userassign, $follows);
    if(mysql_num_rows($userassign)) {
    // we have at least one user, so show all users as options in select form
    while($row_userassign = mysql_fetch_row($userassign))
    {
    print("<option value=\"$row_userassign[0]\">$row_userassign[0]</option>");
    }
    } else {
    print("<option value=\"\">No users created yet</option>");
    }
    ?>
    </select>
    </td>
    </tr>
    <tr>
    <td>Background/Profile

    <textarea name="contact_profile" cols="60" rows="3" id="contact_profile"><?php echo $row_contact['contact_profile']; ?></textarea></td>
    </tr>
    </table>
    </div>
    <p>&nbsp;</p></td>
    </tr>

    <tr>
    <td colspan="2"><p>
    <input type="submit" name="Submit2" value="<?php if ($update==1) { echo Update; } else { echo Add; } ?> Follow-Up" />
    <input name="contact_updated" type="hidden" id="contact_updated" value="<?php echo time(); ?>" />
    <input type="hidden" name="MM_insert" value="form1" />
    <input name="contact_id" type="hidden" id="contact_id" value="<?php echo $row_contact['contact_id']; ?>" />
    <input name="image_location" type="hidden" id="image_location" value="<?php echo $row_contact['contact_image']; ?>" />
    </p></td>
    </tr>
    </table>
    <p>&nbsp;</p>
    <input type="hidden" name="MM_update" value="form1">
    </form>
    </div>
    <?php include('includes/right-column.php'); ?>
    <br clear="all" />
    </div>

    <?php include('includes/footer.php'); ?>

    </body>
    </html>




    thanks.
     
    thafroggy, Jul 17, 2008 IP
  2. ahmadfarhan

    ahmadfarhan Peon

    Messages:
    211
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #2
    assign the user id to the value of the drop down...
    
    <option value =userid>username</option>
    
    Code (markup):
    that will give you the id in the $_POST variable but shows the name for the user..

    at least that's what i think you wanted to do.. didn't really get you post...
     
    ahmadfarhan, Jul 17, 2008 IP
  3. thafroggy

    thafroggy Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You are absolutely right, that is exactly what I am trying to do. wow, thanks for summing it like that.

    This is the code I have in place now:

    
    <select name="contact_userassign" id="contact_userassign">
                            <option value="">Assign someone to this followup</option>
    <?php
       $query_userassign = "SELECT user_name FROM follows_users WHERE 1";
       $userassign = mysql_query($query_userassign, $follows);
         if(mysql_num_rows($userassign)) {
           // we have at least one user, so show all users as options in select form
           while($row_userassign = mysql_fetch_row($userassign))
           {
              print("<option value=\"$row_userassign[0]\">$row_userassign[0]</option>");
           }
         } else {
           print("<option value=\"\">No users created yet</option>");
         }
    ?>
    </select>
    
    Code (markup):
    How would I run the query, to work like you say:
     
    thafroggy, Jul 18, 2008 IP
  4. ahmadfarhan

    ahmadfarhan Peon

    Messages:
    211
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    erm.. it's a long post and no syntax highlighting is killing my eyes..
    but
    change the line to create the option to this
    this is assuming that the userid is in column 0 and username is in column 1

    the userid you can get from the variable $_REQUEST['contact_userassign'] (or $_POST['contact_userassign'] or $_GET['contact_userassign'], depending on weather you send a post or get request with the form)
     
    ahmadfarhan, Jul 18, 2008 IP
  5. thafroggy

    thafroggy Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you very much. I believe you solved my issue. I was able to populate a new field using this value I wanted.

    I am very grateful.

    I am sure I will be back soon for more help. Once again thanks.
     
    thafroggy, Jul 18, 2008 IP