AndrewC14 Posted April 15, 2012 Share Posted April 15, 2012 Hi I'm new to php and have spent the last 3 days scouring the internet for solutions to my problem! I have two drop down lists within a table and when I select a value from the second drop down list and submit it, the value I selected in my first drop down list goes back to 0 I'm sure this is a noobish problem but I would love to know how to work out. this is my code: <html> <head> <?php //sums arrays $band1 = array(); $band1[1]="13903"; $band1[2]="14614"; $band1[3]="14258"; $band1[4]="17537.96"; $band1[5]="70151.82"; $band1[6]="311.79"; $band1[7]="41.57"; $band1[8]="0.69"; $band1[9]="0.0115"; ?> </head> <body> <table border="1"> <tr> <td>Band 1</td> <td>Number of Staff</td> <td>Average cost per hour/min/sec</td> <td> Select hours/mins/sec Drop down menu</td> <td>Average cost by time per person</td> </tr> <tr> <td>Cost per Hour</td> <td><FORM NAME ="staff" id="staff_filter" METHOD ="POST" ACTION ="cost improvement tool.php"> <select name="staff1" id="staff1" onchange="document.getElementById('staff_filter').submit();"> <?php $dopvalue=0; for ($i=0; $i<201; $i++) { ?> <option value="<?php echo $i; ?>"<?php if($i==$_POST["staff1"]) echo "selected" ; ?>><?php echo $i; ?></option> <?php } $dop=$_POST['staff1']; ?> </select> </form> </td> <td><?php $costphour=$band1[2]*$band1[3]; echo "£" . number_format($costphour); ?></td> <td><FORM NAME ="report" id="report_filter" METHOD ="POST" ACTION ="cost improvement tool.php"> <select name="cars" id="cars"onchange="document.getElementById('report_filter').submit();"> <?php //drop down list loop and retaining value $dropvalue=0; for ($start=0; $start<201; $start++) { ?> <option value="<?php echo $start; ?>" <?php if($start==$_POST["cars"]) echo "selected"; ?>><?php echo $start; ?></option> <?php } $drop=$_POST['cars']; ?> </select> </form> </td> <td><?php echo $drop;?> </td> </tr> <td> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/ Share on other sites More sharing options...
AndrewC14 Posted April 15, 2012 Author Share Posted April 15, 2012 Anyone? Please Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337522 Share on other sites More sharing options...
raptor30506090 Posted April 15, 2012 Share Posted April 15, 2012 where am you getting the id from? Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337526 Share on other sites More sharing options...
AndrewC14 Posted April 15, 2012 Author Share Posted April 15, 2012 sorry? Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337527 Share on other sites More sharing options...
raptor30506090 Posted April 15, 2012 Share Posted April 15, 2012 are you using a database for your menu? if so where are you getting the id for the menu or drop down menu Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337528 Share on other sites More sharing options...
AndrewC14 Posted April 15, 2012 Author Share Posted April 15, 2012 Oh no I'm not using a database to put the values into - I just simply gave the form an id so that I could submit it without a submit button using onchange. (don't really know what I'm doing) Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337529 Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 This can't be the entire code, right? There's errors that makes it spend several seconds to load. Why does it go to 201? for ($start=0; $start<201; $start++) Try to use foreach instead, or replace 201 with a variable that is earlier set to be count($array) where $array is where array you want to loop through. This gives me error: <?php if($i==$_POST["staff1"]) echo "selected" ; ?> Because $_POST['staff1'] is not necessarily set, so check that before you try to use it. Perhaps you want two different loops, one when staff is set, and one where it isn't. Or perhaps instead, you set another variable to be the value of $_POST['staff1'] and if it doesn't exist set it to -1 or something. ^ The same goes for $_POST['cars'] If the drop down menus form data are sent to the same page, you can probably set the action parameter in the form tag to just be empty, like this: action="". Now, to you problem. Here is one of them: <select name="cars" id="cars"onchange="document.getElementById('report_filter').submit();"> or more exactly: onchange="document.getElementById('report_filter').submit();" I'm not pro at javascript, but I bet when you click on one of the selections in the drop down list, it automatically submits the form, and the other drop down list is not in the same form. The solution is to put them both into the same form, and change the getElementById to the combined form id. Here's a fix, but I have not corrected the 201 in the loops: <html> <head> <?php //sums arrays $band1 = array(); $band1[1]="13903"; $band1[2]="14614"; $band1[3]="14258"; $band1[4]="17537.96"; $band1[5]="70151.82"; $band1[6]="311.79"; $band1[7]="41.57"; $band1[8]="0.69"; $band1[9]="0.0115"; ?> </head> <body> <table border="1"> <tr> <td>Band 1</td> <td>Number of Staff</td> <td>Average cost per hour/min/sec</td> <td> Select hours/mins/sec Drop down menu</td> <td>Average cost by time per person</td> </tr> <tr> <td>Cost per Hour</td> <td><FORM NAME ="staff" id="staff_filter" METHOD ="POST" ACTION =""> <select name="staff1" id="staff1" onchange="document.getElementById('staff_filter').submit();"> <?php $dopvalue=0; if(isset($_POST['staff1'])){ $dop=$_POST['staff1']; }else{ $dop=-1; } for ($i=0; $i<201; $i++) { ?> <option value="<?php echo $i; ?>"<?php if($i==$dop) echo "selected" ; ?>><?php echo $i; ?></option> <?php } ?> </select> </td> <td><?php $costphour=$band1[2]*$band1[3]; echo "£" . number_format($costphour); ?></td> <td> <select name="cars" id="cars"onchange="document.getElementById('staff_filter').submit();"> <?php //drop down list loop and retaining value $dropvalue=0; if(isset($_POST['cars'])){ $drop=$_POST['cars']; }else{ $drop=-1; } for ($start=0; $start<201; $start++) { ?> <option value="<?php echo $start; ?>" <?php if($start==$drop) echo "selected"; ?>><?php echo $start; ?></option> <?php } ?> </select> </form> </td> <td><?php echo $drop;?> </td> </tr> <td> </table> </body> </html> I still think it's a pretty horrible code, but at least we got the worst stuff fixed! Oh, and it does as you want. Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337530 Share on other sites More sharing options...
raptor30506090 Posted April 15, 2012 Share Posted April 15, 2012 ok so you have a drop down in the form what are you trying to do with it are you just posting? Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337531 Share on other sites More sharing options...
AndrewC14 Posted April 15, 2012 Author Share Posted April 15, 2012 Hey sorry yeah it is a horrible code haha I've just started learning php the last week! I used 201 as I needed 200 values so instead of doing <option>1</option> etc. up till 200 I just created a loop but obviously there is a better, more efficient way to do this? I think it would work making them all part of one form but on the next row I will be having another 2 drop down menus. Then on the same page I will have 8 other similar tables. I need it to work so that when a user selects a value from the drop down, the result of their action will show up in a cell beside it (if that makes sense) but this can't work with multiple submits can it? Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337577 Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 Hey sorry yeah it is a horrible code haha I've just started learning php the last week! I used 201 as I needed 200 values so instead of doing <option>1</option> etc. up till 200 I just created a loop but obviously there is a better, more efficient way to do this? I think it would work making them all part of one form but on the next row I will be having another 2 drop down menus. Then on the same page I will have 8 other similar tables. I need it to work so that when a user selects a value from the drop down, the result of their action will show up in a cell beside it (if that makes sense) but this can't work with multiple submits can it? Just test the code I posted earlier. Why would you need multiple forms? You can submit multiple inputs in the same form. Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337592 Share on other sites More sharing options...
AndrewC14 Posted April 15, 2012 Author Share Posted April 15, 2012 sorry, multiple submits I mean but it doesn't matter your code works, apart from the value -1 is in the cell beside it where I am echoing it. What do you think I should do about 201 loop? Is there a more efficient way to do this? Thanks for your time so far by the way Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337598 Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 sorry, multiple submits I mean but it doesn't matter your code works, apart from the value -1 is in the cell beside it where I am echoing it. What do you think I should do about 201 loop? Is there a more efficient way to do this? Thanks for your time so far by the way If you want it to loop 201 times, no. I thought you would use it along with some array. If it starts at 0, then at 200 it will have printed 201 values 0,1,2,3,4,5 = 6 numbers Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337611 Share on other sites More sharing options...
AndrewC14 Posted April 15, 2012 Author Share Posted April 15, 2012 oh haha obviously! So do you think I should just create an array with values 0,200? Is there a quicker way to do this rather than writing 0,1,2,3 etc. haha? Then how would I loop this array? Sorry for being such a noob! Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337614 Share on other sites More sharing options...
AndrewC14 Posted April 15, 2012 Author Share Posted April 15, 2012 and would looping an array be more efficient than what I've done? Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337615 Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 oh haha obviously! So do you think I should just create an array with values 0,200? Is there a quicker way to do this rather than writing 0,1,2,3 etc. haha? Then how would I loop this array? Sorry for being such a noob! No, I thought you wanted to do this: $l = count($array_with_data) for($i=0; $i<$l; $i++){ echo $array_with_data[$i]; } But you just wanted the number, not some data in an array, so ignore this. I got confused about it because of the $band array, oh and arrays usually start at 0, and not 1. Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337618 Share on other sites More sharing options...
AndrewC14 Posted April 15, 2012 Author Share Posted April 15, 2012 Oh right yeah I just wanted the numbers 0-200 in a drop down form, so should I stick with the loop with I've got? Are there any other errors in my code or mistakes lol? Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337620 Share on other sites More sharing options...
MMDE Posted April 15, 2012 Share Posted April 15, 2012 Oh right yeah I just wanted the numbers 0-200 in a drop down form, so should I stick with the loop with I've got? Are there any other errors in my code or mistakes lol? If that is all you wanted to do, then it's fine. The thing that made your script take several seconds to load was because of these two: <?php if($i==$_POST["staff1"]) echo "selected" ; ?> <?php if($start==$_POST["cars"]) echo "selected"; ?> When the post data wasn't set, it spent a lot of time trying to find it, and then issuing an error. Repeat this 201 times per case and you got yourself a script that takes much longer time than needed to load. If you had turn on error reporting, I'm sure you would have noticed it instantly! The other thing I react on is more of visual look of the code. You keep using <?php and ?>, and some places there's too little space, other places too much. It's a pretty big mess, which can become very hard to handle, especially the code gets longer. Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337662 Share on other sites More sharing options...
AndrewC14 Posted April 16, 2012 Author Share Posted April 16, 2012 Thank you I'll always check next time And oh okay...I thought you had to use <?php and ?> ? Quote Link to comment https://forums.phpfreaks.com/topic/260977-help-with-drop-down-lists/#findComment-1337755 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.