473,396 Members | 1,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

PHP Dynamic Form.

9
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

Expand|Select|Wrap|Line Numbers
  1. // connect to the server
  2.    @mysql_connect($host, $username, $password) or die ("Server is Down");
  3.    @mysql_select_db($database) or die ("Database Error");
  4.    $query="SELECT * FROM itemtbl";
  5. $result=mysql_query($query);
  6.  
  7. $num =mysql_numrows($result);
  8.  
  9. mysql_close();
  10.  
  11. ?>
  12. <table>
  13. <tr><td>ID</td><td>Session</td><td>Item</td><td>Date</td><td>Qty</td></tr>
  14. <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">
  15. <?php
  16. $i=0;
  17. while ($i < $num){
  18. $id=mysql_result($result,$i,"itemid");
  19. $info=mysql_result($result,$i,"info");
  20. $price=mysql_result($result,$i,"price");
  21. $qty=mysql_result($result,$i,"itemqty");
  22.  
  23. echo "<option value=$id> $info<option>";
  24.  
  25. $i++;
  26. }
  27. ?>
  28. </select> 
  29.  
  30. </td><td><input name="date" type="text" id="date" size="20"></td><td><select name="qty" size="1">
  31.   <option value="1">1</option>
  32.   <option value="2">2</option>
  33.   <option value="3">3</option>
  34.   <option value="4">4</option>
  35.   <option value="5">5</option>
  36.   </select> 
  37.  
  38. </td></tr>
  39. <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>
  40.  
  41. </table>
  42.  
Sep 7 '07 #1
17 3474
rpnew
188 100+
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

Expand|Select|Wrap|Line Numbers
  1. // connect to the server
  2.    @mysql_connect($host, $username, $password) or die ("Server is Down");
  3.    @mysql_select_db($database) or die ("Database Error");
  4.    $query="SELECT * FROM itemtbl";
  5. $result=mysql_query($query);
  6.  
  7. $num =mysql_numrows($result);
  8.  
  9. mysql_close();
  10.  
  11. ?>
  12. <table>
  13. <tr><td>ID</td><td>Session</td><td>Item</td><td>Date</td><td>Qty</td></tr>
  14. <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">
  15. <?php
  16. $i=0;
  17. while ($i < $num){
  18. $id=mysql_result($result,$i,"itemid");
  19. $info=mysql_result($result,$i,"info");
  20. $price=mysql_result($result,$i,"price");
  21. $qty=mysql_result($result,$i,"itemqty");
  22.  
  23. echo "<option value=$id> $info<option>";
  24.  
  25. $i++;
  26. }
  27. ?>
  28. </select> 
  29.  
  30. </td><td><input name="date" type="text" id="date" size="20"></td><td><select name="qty" size="1">
  31.   <option value="1">1</option>
  32.   <option value="2">2</option>
  33.   <option value="3">3</option>
  34.   <option value="4">4</option>
  35.   <option value="5">5</option>
  36.   </select> 
  37.  
  38. </td></tr>
  39. <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>
  40.  
  41. </table>
  42.  

[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
Sep 7 '07 #2
ezb
9
Thats exactly what I'm using and seems to work fine, well apart from the things listed above..
Sep 7 '07 #3
Purple
404 Expert 256MB
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
Sep 7 '07 #4
Purple
404 Expert 256MB
Hi,

not sure if it is a typo but:

[PHP]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++;
}[/PHP]

should be more like:

[PHP]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++;
}[/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
Sep 7 '07 #5
Purple
404 Expert 256MB
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
Sep 7 '07 #6
ezb
9
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
Sep 7 '07 #7
ezb
9
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
Sep 7 '07 #8
Purple
404 Expert 256MB
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
Sep 7 '07 #9
ezb
9
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
Sep 7 '07 #10
ezb
9
having little look with locating a sufficent example on the internet, just wondering about the syntax, something like

Expand|Select|Wrap|Line Numbers
  1. <select name="product" size="1" onChange="$product=$_POST['product'];">
  2. echo "<option value=$id> $info</option>";
how would I need to modify that to get it working
Sep 7 '07 #11
Purple
404 Expert 256MB
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="$product=$_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="javascript:refresh_win();">
<?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
Sep 7 '07 #12
Purple
404 Expert 256MB
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...
Sep 7 '07 #13
Purple
404 Expert 256MB
Hi,

Expand|Select|Wrap|Line Numbers
  1. function refresh_win()
  2. {
  3.     document.formname.submit.click();
  4. }
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
Sep 7 '07 #14
ezb
9
Hi ezb,

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

[PHP]<select name="product" size="1" onChange="javascript:refresh_win();">
<?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?
Sep 7 '07 #15
Purple
404 Expert 256MB
Hi ezb,

you do this by building code block which run based on which post values are set

so :

[PHP]<?php
if (isset($_POST['first_select']) and $_POST['first_select'] <> "default value" and !isset($_POST['second_select']) ) {
//
do the code to build the second select
//
}
elseif(isset($_POST['first_select']) and $_POST['first_select'] <> "default value" and isset($_POST['second_select']) and $_POST['second_select'] <> "default value" ) {
//
do the code to save the data to the database
//
}
?>[/PHP]

does that help ?

Purple
Sep 7 '07 #16
ezb
9
sorry about this but the above doesnt make much snece to me.

I've got the java script working but as I though, when I select an option it basicly refreshes the form, taking all the items back to the default, was wondering if theres a way to get the same result but keeping what the user has selected in the form, purhaps taking the selected into a variable and using them as default when the form submits somehow?
Sep 7 '07 #17
Purple
404 Expert 256MB
Hi,

as with all of these things, there are a number of ways to achieve this..

simplest is prob using sessions so :

[PHP]<?php
session_start(); // this needs to be executed before anything is sent to the browser - including any white space
if (!isset$_SESSION['form_vars']) session_register('form_vars');
if(!empty($_POST['variable']) $_SESSION['form_vars']['variable'] = $_POST['variable']; // this needs to be done for all vars on the form
else $_SESSION['form_vars']['variable'] = null;
// now in the form we can set the field values to $_SESSION['form_vars']
['variable'] and this will be maintained across submissions of the form.
//*
//* build the form here
//*
// when its processed you need to do the following to clear the variables:
unset($_SESSION['form_vars'][);
// its also good practise to tidy the session record too using:
session_destroy(); // but only after you have saved your values...

?>[/PHP]

Does that help ?

Questions ?
Sep 7 '07 #18

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Pat Patterson | last post by:
I'm having serious issues with a page I'm developing. I just need some simple help, and was hoping someone might be able to help me out in here. I have a form, that consists of 3 pages of...
1
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to...
0
by: starace | last post by:
I have designed a form that has 5 different list boxes where the selections within each are used as criteria in building a dynamic query. Some boxes are set for multiple selections but these list...
3
by: CAD Fiend | last post by:
Hello, Well, after an initial review of my database by my client, they have completely changed their minds about how they want their form. As a result, I'm having to re-think the whole process....
1
by: Will | last post by:
Hi all. I'm learning VB.Net and am developing a WinForms app. I'm trying to make an app that I will use to scan in one or more than on image. I want to use a tabbed interface to hold each image....
6
by: MikeY | last post by:
Hi Everyone, Does anyone know where I can get my hands on a sample with source code of a simple dynamic button control in C# Windows form. I am looking for a sample that uses a class library...
3
by: Tyler Carver | last post by:
I am trying to use some dynamic controls that are built and then added to tables. The problem that I am having is the timing of when I can populate the controls and have the state remain after a...
7
by: Abraham Luna | last post by:
how do i stop the dynamic validators from breaking explorer if i use a dynamic validator and move to a different control it breaks explorer and i can type in the page when i'm not supposed to....
8
by: George Meng | last post by:
I got a tough question: The backgroud for this question is: I want to design an application works like a engine. After release, we can still customize a form by adding a button, and source code...
2
by: deejayquai | last post by:
Hi I'm trying to produce a report based on a dynamic crosstab. Ultimately i'd like the report to actually become a sub report within a student end of year record of achievement. The dynamic...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.