PHP Dynamic Form.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ezb
    New Member
    • Jul 2007
    • 9

    PHP Dynamic Form.

    I have two slight problems I simply cant find an answer to anywhere, the first, only small.

    I have my database printing out items from a database into a select box, this is giving me blank options even though there are none in the database, I remember having this problem before but cant remember how to get around it?

    Secondly on the same form I would like another drop down field to change according to what is selected e.g. customer selects product, amount availible is stored in the database so I would like a qty drop down that will display from 1 upto the amount availible.
    The Webpage

    [CODE=php]
    // connect to the server
    @mysql_connect( $host, $username, $password) or die ("Server is Down");
    @mysql_select_d b($database) or die ("Database Error");
    $query="SELECT * FROM itemtbl";
    $result=mysql_q uery($query);

    $num =mysql_numrows( $result);

    mysql_close();

    ?>
    <table>
    <tr><td>ID</td><td>Session</td><td>Item</td><td>Date</td><td>Qty</td></tr>
    <tr><td><form id="orderform" name="orderform " method="post" action=" "><input name="id" type="text" id="id" size="10"></td><td><input name="name" type="text" id="name" size="20"></td><td><select name="product" size="1">
    <?php
    $i=0;
    while ($i < $num){
    $id=mysql_resul t($result,$i,"i temid");
    $info=mysql_res ult($result,$i, "info");
    $price=mysql_re sult($result,$i ,"price");
    $qty=mysql_resu lt($result,$i," itemqty");

    echo "<option value=$id> $info<option>";

    $i++;
    }
    ?>
    </select>

    </td><td><input name="date" type="text" id="date" size="20"></td><td><select name="qty" size="1">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">4</option>
    <option value="5">5</option>
    </select>

    </td></tr>
    <tr><td></td><td></td><td><input type="submit" name="submit" value="Submit"> </td><td></td><td><input type="reset" name="submit2" value="Reset"></form></td></tr>

    </table>
    [/CODE]
    Last edited by ak1dnar; Sep 7 '07, 10:58 AM. Reason: a
  • rpnew
    New Member
    • Aug 2007
    • 189

    #2
    Originally posted by ezb
    I have two slight problems I simply cant find an answer to anywhere, the first, only small.

    I have my database printing out items from a database into a select box, this is giving me blank options even though there are none in the database, I remember having this problem before but cant remember how to get around it?

    Secondly on the same form I would like another drop down field to change according to what is selected e.g. customer selects product, amount availible is stored in the database so I would like a qty drop down that will display from 1 upto the amount availible.
    The Webpage

    Code:
    // connect to the server
       @mysql_connect($host, $username, $password) or die ("Server is Down");
       @mysql_select_db($database) or die ("Database Error");
       $query="SELECT * FROM itemtbl";
    $result=mysql_query($query);
    
    $num =mysql_numrows($result);
    
    mysql_close();
    
    ?>
    <table>
    <tr><td>ID</td><td>Session</td><td>Item</td><td>Date</td><td>Qty</td></tr>
    <tr><td><form id="orderform" name="orderform" method="post" action=" "><input name="id" type="text" id="id" size="10"></td><td><input name="name" type="text" id="name" size="20"></td><td><select name="product" size="1">
    <?php
    $i=0;
    while ($i < $num){
    $id=mysql_result($result,$i,"itemid");
    $info=mysql_result($result,$i,"info");
    $price=mysql_result($result,$i,"price");
    $qty=mysql_result($result,$i,"itemqty");
    
    echo "<option value=$id> $info<option>";
    
    $i++;
    }
    ?>
    </select> 
    
    </td><td><input name="date" type="text" id="date" size="20"></td><td><select name="qty" size="1">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
      </select> 
    
    </td></tr>
    <tr><td></td><td></td><td><input type="submit" name="submit" value="Submit"></td><td></td><td><input type="reset" name="submit2" value="Reset"></form></td></tr>
    
    </table>

    [PHP]

    $num =mysql_numrows( $result);

    [/PHP]

    Are you sure that you are using this line... cause according to my knowledge this function is like this...

    [PHP]

    $num =mysql_num_rows ($result);

    [/PHP]

    Regards,
    RP

    Comment

    • ezb
      New Member
      • Jul 2007
      • 9

      #3
      Thats exactly what I'm using and seems to work fine, well apart from the things listed above..

      Comment

      • Purple
        Recognized Expert Contributor
        • May 2007
        • 404

        #4
        Hi all,

        mysql_numrows() is a deprecated alias of mysql_num_rows( ).

        You should ideally move towards using mysql_num_rows( ).

        Now I am going to take a look at your problem..

        Regards Purple

        Comment

        • Purple
          Recognized Expert Contributor
          • May 2007
          • 404

          #5
          Hi,

          not sure if it is a typo but:

          [PHP]while ($i < $num) {
          $id=mysql_resul t($result,$i,"i temid");
          $info=mysql_res ult($result,$i, "info");
          $price=mysql_re sult($result,$i ,"price");
          $qty=mysql_resu lt($result,$i," itemqty");

          echo "<option value=$id> $info<option>";

          $i++;
          }[/PHP]

          should be more like:

          [PHP]while ($i < $num) {
          $id=mysql_resul t($result,$i,"i temid");
          $info=mysql_res ult($result,$i, "info");
          $price=mysql_re sult($result,$i ,"price");
          $qty=mysql_resu lt($result,$i," itemqty");

          echo "<option value=".$id."> ".$info."</option>";

          $i++;
          }[/PHP]

          note the "/" terminator for the option element

          Not sure what impact that will have on the code, do the change or confirm typo and post back..

          also - as a matter of style, note the changes to the variables on the echo statement - doing it this way allows your IDE to identify them as variables and not just stuff to be echoed to the screen..

          Regards Purple

          Comment

          • Purple
            Recognized Expert Contributor
            • May 2007
            • 404

            #6
            Regarding your second question, you have some choices.

            You could do it by building the page using the onchange event to refresh the page and load the second dropdown - this approach will make the page judder on a slow internet connection but can be implemented using 99% PHP.

            You could use Ajax which is probably the right way to do it but assumes all of the people using the website have java enabled on their client browsers.

            Have you javascript knowledge ?

            Regards Purple

            Comment

            • ezb
              New Member
              • Jul 2007
              • 9

              #7
              Originally posted by Purple
              note the "/" terminator for the option element

              Not sure what impact that will have on the code, do the change or confirm typo and post back..

              also - as a matter of style, note the changes to the variables on the echo statement - doing it this way allows your IDE to identify them as variables and not just stuff to be echoed to the screen..

              Regards Purple
              I knew it would be something stupid like that, yes thats fixed my first problem, well noticed, cheers for that

              Comment

              • ezb
                New Member
                • Jul 2007
                • 9

                #8
                Originally posted by Purple
                Regarding your second question, you have some choices.

                You could do it by building the page using the onchange event to refresh the page and load the second dropdown - this approach will make the page judder on a slow internet connection but can be implemented using 99% PHP.

                You could use Ajax which is probably the right way to do it but assumes all of the people using the website have java enabled on their client browsers.

                Have you javascript knowledge ?

                Regards Purple
                Not really, done bits an bobs of Java, thats about it, never really got into it

                Comment

                • Purple
                  Recognized Expert Contributor
                  • May 2007
                  • 404

                  #9
                  Hi,

                  well I suggest you go the first route,

                  setup an onchange event on the select to process the form when a selection has been made - within the script test the post values of the first select and if it has been set, make your second select from the DB and build the second dropdown..

                  Hope that makes sense..

                  Regards Purple

                  Comment

                  • ezb
                    New Member
                    • Jul 2007
                    • 9

                    #10
                    Cheers for your help, I'll look it up now and give it in a bash, let you know how it turns out, cheers guys

                    Comment

                    • ezb
                      New Member
                      • Jul 2007
                      • 9

                      #11
                      having little look with locating a sufficent example on the internet, just wondering about the syntax, something like

                      Code:
                      <select name="product" size="1" onChange="$product=$_POST['product'];">
                      echo "<option value=$id> $info</option>";
                      how would I need to modify that to get it working

                      Comment

                      • Purple
                        Recognized Expert Contributor
                        • May 2007
                        • 404

                        #12
                        Originally posted by ezb
                        having little look with locating a sufficent example on the internet, just wondering about the syntax, something like

                        [PHP]<select name="product" size="1" onChange="$prod uct=$_POST['product'];">
                        echo "<option value=$id> $info</option>";[/PHP]
                        how would I need to modify that to get it working
                        Hi ezb,

                        the onChange event is client side, you need to be in JavaScript

                        [PHP]<select name="product" size="1" onChange="javas cript:refresh_w in();">
                        <?php
                        echo "<option value=$id> $info</option>";
                        ?>[/PHP]
                        with a javascript function called refresh_win() which presses a submit button on the screen - you will need the form to post to itself to make this work..

                        Regards Purple

                        Comment

                        • Purple
                          Recognized Expert Contributor
                          • May 2007
                          • 404

                          #13
                          oh and, please use code tags when posting code, no matter how small..

                          MODERATOR

                          I can't change your post cause I don't moderate this forum, I am sure one of the php mods will get on this...

                          Comment

                          • Purple
                            Recognized Expert Contributor
                            • May 2007
                            • 404

                            #14
                            Hi,

                            Code:
                            function refresh_win()
                            {
                            	document.[I]formname[/I].submit.click();
                            }
                            formname needs to be changed to the name of your form
                            assumes you have a button called submit - change to name of button if not.

                            Regards Purple

                            Comment

                            • ezb
                              New Member
                              • Jul 2007
                              • 9

                              #15
                              Originally posted by Purple
                              Hi ezb,

                              the onChange event is client side, you need to be in JavaScript

                              [PHP]<select name="product" size="1" onChange="javas cript:refresh_w in();">
                              <?php
                              echo "<option value=$id> $info</option>";
                              ?>[/PHP]
                              with a javascript function called refresh_win() which presses a submit button on the screen - you will need the form to post to itself to make this work..

                              Regards Purple
                              Sorry about that, was not intending on adding proper code so simply didnt bother with code tags, edited it accordingly.
                              The touble with this being I would like to submit the form into a database once I have the selected information, how can I do this if I'm porting the form to its self in order to this variable?

                              Comment

                              Working...