#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    New Zealand
    Posts
    167
    Rep Power
    14

    Question Javascript/php select box


    I have a page where the options in a select box are generated from the database. I'm trying to grab the selected value using Javascript, and I'm not quite sure why I'm having problems - as I understand it, the php/mySQL should run when the page first loads, and print the html output to the page - so it shouldn't be any different to a normal JS selectbox value.

    It could be the JS I'm using, so here's the code for both - any suggestions would be great.

    function copyValues()
    {
    var region = document.form.company_region.value;
    var city = document.form.company_city.value;
    var suburb = document.form.company_suburb.options[document.form.company_suburb.selectedIndex].value;
    var street = document.form.company_street.value;

    var address = region + city + suburb + street;

    document.form.company_postal.value = address;
    }

    ...

    <tr>
    <td width="262">Suburb</td>
    <td width="194" colspan="2">
    <?php
    $query = "SELECT Suburb.Name FROM Suburb, City WHERE City.Name='Wellington' AND Suburb.CityID=City.CityID ORDER BY Suburb.Name ASC";
    $result = mysql_query ($query,$db_link_id);
    $total_num_rec=mysql_num_rows($result);
    echo "<select size='1' name=\"company_suburb\">";
    if($total_num_rec!=0){
    for ($i=0; $i<$total_num_rec; $i++){
    $current_row = mysql_fetch_array($result);
    $suburb_name=$current_row[Name];
    echo "<option>$suburb_name</option>";
    }
    }else{
    $error_msg="No records found";
    echo "<option>$error_msg</option>";
    }
    echo "</select>";
    echo "</td>";
    echo "</tr>";
    ?>

    <?php
    session_register("company_suburb");
    ?>
    </td>
    </tr>

    ...

    Thanks!

    Jen
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Brisbane, Australia
    Posts
    32
    Rep Power
    14
    What are the problems you are having?
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    New Zealand
    Posts
    167
    Rep Power
    14

    sorry - longer explanation


    Sorry if that wasn't clear.

    Basically, I am taking 4 values from the form after the user has input them:

    company_region, which is a simple text box
    company_city, which is also a simple text box
    company_suburb, which is the dynamic select box
    company_street, which is a simple text box.

    I'm getting these values and compiling them into one string which is then posted back to the form in the company_postal text box.

    At the moment, the script is returning the region, the city and the street value, but is missing the suburb value.

    I'm not sure if this is a php error or a JS error. It just doesn't seem to be picking up a value for the company_suburb.

    Thanks,

    Jen
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2001
    Location
    Brisbane, Australia
    Posts
    32
    Rep Power
    14
    Hehe, I didn't notice the first time, but this is not a PHP nor mySQL nor JavaScript problem.... whats left? HTML.

    You didn't specify a value for your options!

    Replace
    PHP Code:
    echo "<option>$suburb_name</option>"
    With
    PHP Code:
    echo "<option value=\"$suburb_name\">$suburb_name"
    The </option> tag is not required in HTML programming.

    Simple errors are the best errors.

    Regards,
    Robbo
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2001
    Location
    New Zealand
    Posts
    167
    Rep Power
    14

    Hooray!


    Thanks for that!

    Simple errors are indeed the best... can't believe I overlooked that.

    Thanks

    Jen

IMN logo majestic logo threadwatch logo seochat tools logo