I'm having some trouble , and was wondering if someone could help me...

I have PHP code that creates a select box on how many rows are in my MySQL database. Then, when the user clicks on a number, I want a form to be populated with all the values from that row. I'm having trouble doing this...

lets say something like this..

the user selects row 6 from the drop down list.

then i want the following fields to be populated from the corresponding fields in the mysql DB.

<input type = "text" name=product >
<input type = "text" name=credit >

and so on

    i should say that this is the code i'm using to populate the select box with how many records there are

    $query = "SELECT * FROM $userstable";

    $query_result_handle = mysql_query ($query)

    or die ('<h1><center>The requested table or database does not exist</center></h1>');

    $num_of_rows = mysql_num_rows ($query_result_handle)

    or die ("No records in DB");

    print "Which record would you like to update?<br>";
    print "<select name=rows>";
    for ($count = 1; $row = mysql_fetch_row ($query_result_handle); ++$count)
    {

    print "<option value= $row[0]> $count </option>";

    }
    print "</select>";
    ?>

      You are going to have to do one of two things:

      either a) on the OnChange event of the drop down force a form submission "document.formname.submit()" and in the receiving page requey the db and dynamicly fill in the form elements much as you populate the drop down

      or

      b) on the onChange event call a javascript that will read the value of the drop down item selected and populate the other text boxes with the appropriate value. This script must be built dynamically by your code.

      The first option is easier for a Javascript newbie, but the second is a much better because it reduces calls to your server and database

        wont that make page download time longer, and get all of the possible form fill-ins?

          so this is something that i will have to use javascript for? I'm really a novice at javascript...most of my js experience is cut and paste...heh

            Yup, option b has a larger size, depending on the amount of option for the drop down.

            If javascript is not your thing just do something like this in the first page:

            <FORM NAME="MyForm" ACTION="Validate.php" METHOD="POST">
            <SELECT NAME="Whatever" OnChange="Javascript: document.MyForm.submit();">
            <OPTION>.....</OPTION>
            </SELECT>
            </FORM>

            Either have the form submit to the same page and have a hidden field to determine if the submit was triggered by the onChange event (and redisplay with text boxes filled in) or by the submit button. This way you have to make sure the submit button changes the value of the hidden form before the form is submitted.

            The other way is to have a second form on the page. It might look like this:

            <FORM NAME="OnChangeForm" METHOD="POST" ACTION="ThisPage.php">
            <INPUT TYPE="HIDDEN" NAME="DropDownValue"
            </FORM>

            and have the onchange event of the drop down look more like this:
            onChange="Javascript: document.OnChangeForm.DropDownValue.value = document.MyForm.options[document.MyForm.Whatever.selectedIndex].value; document.OnChangeForm.submit();"

            This will cause the hidden form to submit the value to the same page (assume the page the form is on is ThisPage.php). Then the php code should test to see if a value was passed. If a value was passed fill in your text boxes, else leave blank.

            The javascript is kinda nasty, but the php is very doable this way.

              Write a Reply...