Jump to content

Help with drop down lists


AndrewC14

Recommended Posts

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>

Link to comment
Share on other sites

:o 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.

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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. :P

 

If it starts at 0, then at 200 it will have printed 201 values ;) 0,1,2,3,4,5 = 6 numbers

Link to comment
Share on other sites

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? :P

 

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. :P

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.