SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    oklahoma
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Javascript/PHP Question

    I have a table in a PHP form. I have two selectboxes on my form and one is tied to a database. Selectbox1 is data I hard-coded and Selectbox2 is linked to my database. I want Selectbox2 to only show records matching what was chosen in Selectbox1. From my understanding I need to do this through Javascript but I don't know how. Can I refer to Selectbox1 through Javascript even though it is in PHP code? How do I do this? As you can tell I am completely new to this so any and all help will be greatly appreciated. Thanks.

  2. #2
    SitePoint Zealot cmorbutts's Avatar
    Join Date
    Apr 2002
    Location
    Sydney, Australia
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can do this in both PHP and Javascript. I'll explain both approaches.

    If you do it in PHP, whenever an option in box1 is selected, you will have to use Javascript to force a page submit. Then your PHP script must grab the option out, go to the database, grab the right data and populate box2. Not very efficient because the page has to be reloaded and reloaded but it will work.

    If you do it mainly in Javascript, then what you would do is use PHP to grab all the data and generate Javascript code dynamically on the server. You can store the data returned in Javascript arrays. Then when something from box1 is selected, you use javascript to extract that option and use it to populate box2. All the action is done locally on the client side so it's more efficient.

    The PHP solution would be easiest to implement but I would recommend doing it in Javascript.

    How do you do it with Javascript? I don't know it depends on how complicated your box1 to box2 rules are.

    Hope this helps.

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2003
    Location
    oklahoma
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is my complete code...can you please take a look at it and tell me what I should/could do? As I mentioned previously, the 2nd lookup is pulling all options from the database and I only want specific ones. Thanks for your help.

    <?php require_once('Connections/Thor.php'); ?>


    <?php
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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 = $HTTP_SERVER_VARS['PHP_SELF'];
    if (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
    $editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
    }
    $jobval = GetSQLValueString($HTTP_POST_VARS['txtJobNo'], "text");
    if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "form1")) {
    if (preg_match("/[A-a]{2,2}[0-9]{8,8}/", $jobval)){
    $insertSQL = sprintf("INSERT INTO POProjIdent (PIJobNo, PIPrepBy, PIClient, PIJobName) VALUES (%s, %s, %s, %s)",
    GetSQLValueString($HTTP_POST_VARS['txtJobNo'], "text"),
    GetSQLValueString($HTTP_POST_VARS['txtPrepby'], "text"),
    GetSQLValueString($HTTP_POST_VARS['txtClient'], "text"),
    GetSQLValueString($HTTP_POST_VARS['txtJobName'], "text"));

    mysql_select_db($database_Thor, $Thor);
    $Result1 = mysql_query($insertSQL, $Thor) or die(mysql_error());
    } else {
    echo '<script type = "text/javascript"> alert("Please enter a valid job number.");</script>';
    }}

    mysql_select_db($database_Thor, $Thor);
    $query_RstProjIdent = "SELECT * FROM POProjIdent";
    $RstProjIdent = mysql_query($query_RstProjIdent, $Thor) or die(mysql_error());
    $row_RstProjIdent = mysql_fetch_assoc($RstProjIdent);
    $totalRows_RstProjIdent = mysql_num_rows($RstProjIdent);

    mysql_select_db($database_Thor, $Thor);
    $query_RstServices = "SELECT * FROM POSERVICES";
    $RstServices = mysql_query($query_RstServices, $Thor) or die(mysql_error());
    $row_RstServices = mysql_fetch_assoc($RstServices);
    $totalRows_RstServices = mysql_num_rows($RstServices);
    ?>

    <html>
    <head>
    <title>PROJECT OPENING REPORT</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#CCCCCC">
    <table width="631" height="143" border="0">
    <tr>
    <td width="332" height="137"><img src="intranet_01.jpg" name="Image1" width="300" height="50" id="Image1"><img src="intranet_03.jpg" name="Image2" width="300" height="50" id="Image2"></td>
    <td width="283"> <div align="center"><strong><font size="5">Project Opening
    Report</font></strong></div></td>
    </tr>
    </table>
    <form name="form1" method="POST" action="<?php echo $editFormAction; ?>">

    <table width="95%" border="1" align="center">
    <tr>
    <td colspan="2"><strong>Date Prepared: </strong>10/30/2003</td>
    <td colspan="2"> <div align="left"><strong>Prepared By:
    <input name="txtPrepby" type="text" id="txtPrepby" value="<?php echo $row_RstProjIdent['PIPrepBy']; ?>">
    </strong></div></td>
    </tr>
    <tr>
    <td colspan="4"><strong>Client: </strong> <input name="txtClient" type="text" id="txtClient" value="<?php echo $row_RstProjIdent['PIClient']; ?>" size="50"></td>
    </tr>
    <tr>
    <td colspan="2"><strong>Job Number:
    <input name="txtJobNo" type="text" id="txtJobNo" value="<?php echo $row_RstProjIdent['PIJobNo']; ?>" size="15">
    </strong></td>
    <td colspan="2"><strong>Job Name:
    <input name="txtJobName" type="text" id="txtJobName" value="<?php echo $row_RstProjIdent['PIJobName']; ?>" size="40">
    </strong></td>
    </tr>
    <tr>
    <td colspan="4"><strong>Service Code:
    <select name="selectIndustry" style="width:20%" size="1" id="selectIndustry">
    <option value="2">Electric</option>
    <option value="4">IT</option>
    <option value="6">Natural Gas</option>
    <option value="7">Other</option>
    <option value="8">Telecom</option>
    <option value="9">Wtr &amp; WasteWtr</option>
    </select>
    <select name="selectPrArea" id="selectPrArea" style="width:30%" >
    <option value="05">Acquisition, Consolidation, Priv &amp; Valuation</option>
    <option value="10">Antitrust &amp; Mkt Analysis</option>
    <option value="15">Billing, Metering &amp; Data Collection &amp; Processing</option>
    <option value="20">Cost of Capital &amp; Regulatory Econ</option>
    <option value="25">Cost of Service &amp; Rates</option>
    <option value="30">Econometric Forecasting &amp; Statistical Analysis</option>
    <option value="35">Education and Training</option>
    <option value="40">Financial Forecasting &amp; Analysis</option>
    <option value="50">Industry Restructuring &amp; Retail Competition</option>
    <option value="70">Resource Planning &amp; Procurement</option>
    <option value="75">Software Sales &amp; Support</option>
    <option value="80">Strategic Planning &amp; Analysis</option>
    <option value="90">Transmission Planning &amp; Analysis</option>
    </select>
    <select name="selectSvc" size="1" style="width:30%" id="select">
    <?php
    do {
    ?>
    <option value="<?php echo $row_RstServices['POSSvcCode']?>"><?php echo $row_RstServices['POSDESC']?></option>
    <?php
    } while ($row_RstServices = mysql_fetch_assoc($RstServices));
    $rows = mysql_num_rows($RstServices);
    if($rows > 0) {
    mysql_data_seek($RstServices, 0);
    $row_RstServices = mysql_fetch_assoc($RstServices);
    }
    ?>
    </select>
    </strong></td>
    </tr>
    <tr>

    <td width="28%" height="25">&nbsp;</td>
    <td width="7%">&nbsp;</td>
    <td width="33%">&nbsp;</td>
    <td width="32%">&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </table>
    <p>
    <input type="submit" name="Submit" value="Submit">
    </p>
    <input type="hidden" name="MM_insert" value="form1">
    </form>
    </body>
    </html>
    <?php
    mysql_free_result($RstProjIdent);



    mysql_free_result($RstServices);
    ?>
    <?php

    ?>


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
  •