Page 1 of 1

Show region if option is selected

Posted: Fri Jan 29, 2010 9:17 am
by koolsamule
Hi Chaps,

I have a PHP form, with a Customer Select List. Once an option has been selected, the 'custid', is passed to Javascript that collects all the contacts linked to that customer and populates another Select List.
This works fine, but what I'm after is an extra two inputs to appear when a particular customer is selected, but I'm not entirely sure how to go about it.
Here it goes:
Table/Form:

Code: Select all

<script type="text/javascript">
 
var ajax = new Array();
 
function getTextList(sel)
{
    var Customer = sel.options[sel.selectedIndex].value;
    document.getElementById('text').options.length = 0;
    if(Customer.length>0){
        var index = ajax.length;
        ajax[index] = new sack();
        
        ajax[index].requestFile = 'getText.php?custid='+Customer;
        ajax[index].onCompletion = function(){ createText(index) };
        ajax[index].runAJAX();
    }
}
 
function createText(index)
{
    var obj = document.getElementById('text');
    eval(ajax[index].response); 
}   
</script>
      <table>
      <tr valign="baseline">
        <th>Customer:</th>
        <td colspan="2" class="normal"><span id="spryselect1">
          <select id="customer" name="FK_custid" onchange="getTextList(this)">
        <option value="">Select Customer</option>
        <?php
            $result = mysql_query("SELECT DISTINCT custname, custid FROM tbl_customers ORDER BY custname") 
            or die(mysql_error());
            while($cust = mysql_fetch_array( $result )) 
            {
                  echo '<option value="'.$cust['custid'].'">'.$cust['custname'].'</option>';
            }
        ?>
    </select><br />
          <span class="selectRequiredMsg">Please select Customer</span></span></td>
      </tr>
      <tr valign="baseline">
        <th>Customer Contact:</th>
        <td colspan="2" class="normal"><span id="spryselect6">
          <select id="text" name="projcontact">
            <option value="">--Select Customer First--</option>
          </select><br />
          <span class="selectRequiredMsg">Please select a Project Contact</span></span></td>
      </tr>
      <tr valign="baseline">
        <th>Purchase Order Ref:</th>
        <td colspan="2" class="normal"><input type="text" id="count" name="projporder" value="" size="32" /></td>
      </tr>
      <?php 
        if ('CustomerID'=='XX'){?>
      <tr valign="baseline">
        <th>Cost Centre:</th>
        <td colspan="2" class="normal"><input type="text" id="count" name="projcostcentre" value="" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <th>Cost Centre Contact:</th>
        <td colspan="2" class="normal"><input type="text" id="count" name="projcostcentrecontact" value="" size="32" /></td>
      </tr>
      <?php 
        };
      ?>
      </table>
GetText.php:

Code: Select all

<?php require_once('../../Connections/conndb2.php'); 
    $Customer = $_GET['custid'];
    if(isset($_GET['custid'])){
        $result = mysql_query("SELECT * FROM tbl_contacts WHERE FK_custid='$Customer' ORDER BY contactname") 
        or die(mysql_error());
             while($text = mysql_fetch_array( $result )) 
              {
                echo 'obj.options[obj.options.length] = new Option("'.$text['contactname'].'","'.$text['contactname'].'");';
              } 
    }
?>
I know that I need the 'custid' to be set somewhere, but not sure at which point to do it. Any ideas welcome!
Apologies if this is a Javascript question, if so I'll move this to a different forum.

Re: Show region if option is selected

Posted: Fri Jan 29, 2010 5:10 pm
by JakeJ
I'll take a stab at it.

First of all, since you might want to display other inputs for other customers in the future, it would be best not to code something for a specific customer. Meaning don't do this: If ($cust_id == 3) { $dosomething }.
What you are better off doing is creating a new table for priority codes. Then in another field on that table, you could store the text needed for the extra inputs for a given code. Then when you query customer information, if a priority code is set then the extra inputs get tossed in to your form.

Or you might try using hidden inputs and then unhiding them when the priority condition is met rather than storing the html code in a field but I think storing it provides the best scalability.

Re: Show region if option is selected

Posted: Mon Feb 01, 2010 4:14 am
by koolsamule
Hi Jake, thanks for the reply:
What you are better off doing is creating a new table for priority codes. Then in another field on that table, you could store the text needed for the extra inputs for a given code. Then when you query customer information, if a priority code is set then the extra inputs get tossed in to your form.
That sounds good, can you give me a few pointers in the right direction?, as I don't know how to do this.

Re: Show region if option is selected

Posted: Mon Feb 01, 2010 1:10 pm
by JakeJ
koolsamule wrote:Hi Jake, thanks for the reply:
What you are better off doing is creating a new table for priority codes. Then in another field on that table, you could store the text needed for the extra inputs for a given code. Then when you query customer information, if a priority code is set then the extra inputs get tossed in to your form.
That sounds good, can you give me a few pointers in the right direction?, as I don't know how to do this.
Create a table called 'priority' with the following fields:

id (integer, auto increment)
text (varchar, 150)

Then, in your customer table, add a field called priority (integer).

Then when pulling data in to your form do a join so that the text field is included SELECT text from priority where customer.priority = priority.id

I assume you already know how to assign data to variables.

Re: Show region if option is selected

Posted: Tue Feb 02, 2010 3:21 am
by koolsamule
Ahh, I see, makes perfect sense. Thanks for the tips!