Code trouble probably simple

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Code trouble probably simple

Post by tommy1987 »

Hi,
I am trying to use three select boxes together, each of which update the next one.
All of the data for all of them is from a mySQL database.

What I am doing is resubmitting the form each one is changed, now, to stop the selected values from going, I am writing them to a variable and recieving them as they are posted. Then when filling the select box after the form has been submitted I make the varialbe the selected value. The only problem is that it is only storing the first word of the selected entry. e.g. for "Isle of Man" it gets "Isle", this seems bazarre.

Here is the code, I hope you guys can help..

First is right at the top of the code where the value is saved:

Code: Select all

//remember value of region box
$reg = "";
if (isset($_POST['selid']))
{
	$reg = $_POST['selid'];
}

Then is where the select box is populated from the database..

Code: Select all

<?php
								$query="select instRegion from instregions_details";
								$result = mysql_query ($query);
								$num_results=mysql_num_rows($result);
								echo '<input type="hidden" name="a" value="1">';
								echo '<select name="selid" onChange="form1.submit();">';
									
									for($i=0; $i<$num_results; $i++)
									{
										$row=mysql_fetch_array($result);
										if ($row['instRegion'] == $reg)
										{
											//value was saved
										echo "<option selected>".$row["instRegion"]."</option>";
										}
										else
										{
											echo '<option value='.$row["instRegion"].'>';
										}
										echo htmlspecialchars(stripslashes($row["instRegion"]));
										echo "</option>";
									}
								echo '</select>';			
								?>
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Re: Code trouble probably simple

Post by santosj »

tommy1987 wrote:Hi,
I am trying to use three select boxes together, each of which update the next one.
All of the data for all of them is from a mySQL database.

What I am doing is resubmitting the form each one is changed, now, to stop the selected values from going, I am writing them to a variable and recieving them as they are posted. Then when filling the select box after the form has been submitted I make the varialbe the selected value. The only problem is that it is only storing the first word of the selected entry. e.g. for "Isle of Man" it gets "Isle", this seems bazarre.

Here is the code, I hope you guys can help..
I'm sorry, but I'm going to impose and make the following suggestion.

Code: Select all

<?php
$query = 'SELECT instRegion FROM instregions_details';
$query = mysql_query($query);

echo '<input type="hidden" name="a" value="1">';
echo '<select name="selid" onChange="form1.submit();">';

while($row = mysql_fetch_assoc($query))
{
	$option = '<option value="'.$row['instRegion'].'"';

	if(isset($_POST['selid']) and ($row['instRegion'] == $_POST['selid']))
	{
		$option .= ' selected';
	}

	$option .= '>'.htmlspecialchars(stripslashes($row['instRegion'])).'</option>';

	echo $option;
}

echo '</select>';
Last edited by santosj on Mon May 15, 2006 1:47 pm, edited 3 times in total.
wbryan6
Forum Newbie
Posts: 22
Joined: Sat Feb 04, 2006 12:13 pm

Post by wbryan6 »

Try chaning from mysql_fetc_array to mysql_fetch_row.
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

The above doesnt seem to work either, the page wont even load! Im puzzled
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Post by santosj »

Okay, try it now, I added a missing ')' to the if statement. I also double checked the values and it should all be in order now.
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

Thanks very much for the high level of service!! :D
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

Now i have another problem which you can probably help with. I have three select boxes like I said, and they all update from each other. The first two are working as they should, however, the third one only updates from the second one and not the first one. The way I am knowing if it has changed is posting a hidden value called a as can be seen and then saying if($POST_['a']) and then my code.

It works fine for the first and second one however for the last one I tried writing if($_POST_['a'] or $_POST['b']) and then my update code.

I should probably mention that b is the hidden variable i post with the second select box.

Its so annoying!!

Thanks anyway Tom
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Post by santosj »

tommy1987 wrote:Now i have another problem which you can probably help with. I have three select boxes like I said, and they all update from each other. The first two are working as they should, however, the third one only updates from the second one and not the first one. The way I am knowing if it has changed is posting a hidden value called a as can be seen and then saying if($POST_['a']) and then my code.

It works fine for the first and second one however for the last one I tried writing if($_POST_['a'] or $_POST['b']) and then my update code.

I should probably mention that b is the hidden variable i post with the second select box.

Its so annoying!!

Thanks anyway Tom
Hidden values should not matter as long as you have all three select boxes in the same <form> ... </form>.

I would only check for the select boxes values. I don't have the full script so I can't help you there. I would check to see that all three boxes are in the same form block. If one or the other isn't showing up then this could be the cause of your problem.
Post Reply