Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    121
    Thanks
    22
    Thanked 3 Times in 3 Posts

    Using a script to update a Dropdown

    I'm sure this gets asked all the time, but I can't find the solution anywhere. Here is what I want to do, I have a dropdown menu that is populated from a MySQL DB with a list of parts. I also have a table that is used to list the orders from a second DB that the first relates to. I want to limit the data posted to the table based off what part is selected in the dropdown. Here's where I can't find an answer...I need to do this WITHOUT reloading the page, is this possible?

    Here is my code so far:
    Code:
    <?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 ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "Press")) {
      $insertSQL = sprintf("INSERT INTO Stock (StockName, StockWeightID, PaperVendID) VALUES (%s, %s, %s)",
                           GetSQLValueString($_POST['StockName'], "text"),
                           GetSQLValueString($_POST['StockWeightID'], "int"),
                           GetSQLValueString($_POST['PaperVendID'], "int"));
    
      mysql_select_db($database_geQuote, $geQuote);
      $Result1 = mysql_query($insertSQL, $geQuote) or die(mysql_error());
    
      $insertGoTo = "stockins.php";
      if (isset($_SERVER['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      header(sprintf("Location: %s", $insertGoTo));
    }
    
    mysql_select_db($database_geQuote, $geQuote);
    $query_VendorInfo = "SELECT PaperVendor.PaperVendID, PaperVendor.PaperVendName FROM PaperVendor ORDER BY PaperVendor.PaperVendName";
    $VendorInfo = mysql_query($query_VendorInfo, $geQuote) or die(mysql_error());
    $row_VendorInfo = mysql_fetch_assoc($VendorInfo);
    $totalRows_VendorInfo = mysql_num_rows($VendorInfo);
    
    mysql_select_db($database_geQuote, $geQuote);
    $query_StockWeight = "SELECT * FROM StockWeight ORDER BY StockWeight.StockWeightID";
    $StockWeight = mysql_query($query_StockWeight, $geQuote) or die(mysql_error());
    $row_StockWeight = mysql_fetch_assoc($StockWeight);
    $totalRows_StockWeight = mysql_num_rows($StockWeight);
    
    mysql_select_db($database_geQuote, $geQuote);
    $query_StockList = "SELECT Stock.StockName,  PaperVendor.PaperVendName FROM Stock LEFT JOIN PaperVendor USING (PaperVendID) WHERE Stock.StockWeightID=1 ORDER BY Stock.StockName";
    $StockList = mysql_query($query_StockList, $geQuote) or die(mysql_error());
    $row_StockList = mysql_fetch_assoc($StockList);
    $totalRows_StockList = mysql_num_rows($StockList);
    ?><!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>Graphic Edge Printing</title>
    
    </head>
    
    <body>
        <h1 align="center">Stock  Insert </h1>
        <p align="center">Check the list below very carefully we do not want repeat stocks,<br />
        if the Stock you would like to use is not listed, please add it. </p>
        <form action="<?php echo $editFormAction; ?>" id="Press" name="Press" method="POST">
          <table width="457" border="0" cellspacing="0" cellpadding="2" align="center">
             <tr>
              <td><div align="right">Stock Weight     : </div></td>
              <td><select name="StockWeightID">
                <?php
    do {  
    ?>
                <option value="<?php echo $row_StockWeight['StockWeightID']?>"><?php echo $row_StockWeight['StockWeight']?></option>
                <?php
    } while ($row_StockWeight = mysql_fetch_assoc($StockWeight));
      $rows = mysql_num_rows($StockWeight);
      if($rows > 0) {
          mysql_data_seek($StockWeight, 0);
    	  $row_StockWeight = mysql_fetch_assoc($StockWeight);
      }
    ?>
              </select>          </td>
            </tr>
           <tr>
              <td><div align="right">Stock Name     : </div></td>
              <td><input type="text" name="StockName" /></td>
            </tr>
            <tr>
              <td><div align="right">Paper Vendor    : </div></td>
              <td><select name="PaperVendID">
                <option value="NULL"></option>
                <?php
    do {  
    ?>
                <option value="<?php echo $row_VendorInfo['PaperVendID']?>"><?php echo $row_VendorInfo['PaperVendName']?></option>
                <?php
    } while ($row_VendorInfo = mysql_fetch_assoc($VendorInfo));
      $rows = mysql_num_rows($VendorInfo);
      if($rows > 0) {
          mysql_data_seek($VendorInfo, 0);
    	  $row_VendorInfo = mysql_fetch_assoc($VendorInfo);
      }
    ?>
              </select>          </td>
            </tr>
            <tr>
              <td width="128">&nbsp;</td>
              <td width="189"><input name="Add Stock" type="submit" id="Add Stock" value="Add Stock" /></td>
            </tr>
          </table>
          
          
          
          
          
          <input type="hidden" name="MM_insert" value="Press">
    </form>
        <p align="center">&nbsp;</p>
        <table width="600" border="1" cellspacing="0" cellpadding="2" align="center">
          <tr>
            <th scope="col">Stock Type based on <br />
            Weight selected above</th>
            <th scope="col">Paper Vendor </th>
          </tr>
          <tr>
            <td><div align="center"><?php echo $row_StockList['StockName']; ?></div></td>
            <td><div align="center"><?php echo $row_StockList['PaperVendName']; ?></div></td>
          </tr>
        </table>
    </body>
    </html>
    <?php
    mysql_free_result($VendorInfo);
    
    mysql_free_result($StockWeight);
    
    mysql_free_result($StockList);
    ?>
    Last edited by Alith7; 07-17-2009 at 04:09 PM.
    I'll try again to find, the thing that was my mind...
    The only other sign, it says I've lost my mind.

  • #2
    Regular Coder
    Join Date
    Jun 2009
    Posts
    121
    Thanks
    22
    Thanked 3 Times in 3 Posts
    ok, found a Great Tutorial on w3schools site.
    Here is the link
    http://www.w3schools.com/php/php_ajax_database.asp

    just in case anyone else wants to know.
    I'll try again to find, the thing that was my mind...
    The only other sign, it says I've lost my mind.


  •  

    Posting Permissions

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