Forum Moderators: coopster

Message Too Old, No Replies

Having trouble with selected option

         

neoh

10:19 pm on Nov 2, 2006 (gmt 0)

10+ Year Member



Can someone tell me how to get my selected option to reflect what option is selected after a page update. I checked PHP and register_globals are on. Do I have the code right?

<select name=\"centerPoint\" class=\"selectBoxes\">
<option";
if($centerPoint=="41.409775, -80.744018), 7") echo " SELECTED";
echo " value=\"41.409775, -80.744018), 7\">The Entire Area</option>
<option";
if($centerPoint=="41.849616, -79.143447), 11") echo " SELECTED";
echo " value=\"41.849616, -79.143447), 11\">Allegheny River - Kinzua Dam to Buckaloons Rec Area</option>

//get the first result if a category was selected
if(isset($_REQUEST['centerPoint']))
$centerPoint=$_REQUEST['centerPoint'];
//if no category was selected, these results won't exist, so define default values
if(!isset($centerPoint) or strlen($centerPoint)<1) $centerPoint="41.409775, -80.744018), 7";

// CREATE THE MAP
var map = new GMap2(document.getElementById(\"map\"));
map.addControl(new GLargeMapControl());
map.addControl(new GMapTypeControl());

// Set the center point of your map and set the zoom

map.setCenter(new GLatLng($centerPoint);

jatar_k

10:22 pm on Nov 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld neoh,

I assume this is after the user submits your form. Is it being sent by POST or GET?

also, is it not selecting the proper value? (I assume so or you wouldn't be posting ;))

have you viewed the source of the final page to see if it is possibly just an error in the html?

neoh

10:36 pm on Nov 2, 2006 (gmt 0)

10+ Year Member



After a post:
<form action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">

It shows the first option in the list no matter what.

And I didnt see any error in the html

Steerpike

10:45 pm on Nov 2, 2006 (gmt 0)

10+ Year Member




Try changing the values of the select to something a little more basic (like 1 and 2) for testing purposes. I'm not sure if I like those )'s in the value - that could be affecting it.

neoh

10:51 pm on Nov 2, 2006 (gmt 0)

10+ Year Member



Is there another way of rewriting the code so it will work the same?

eelixduppy

10:56 pm on Nov 2, 2006 (gmt 0)



Welcome noeh!

Here's a related thread [webmasterworld.com].

Note the use of the ternary operator [us3.php.net].

good luck!

neoh

11:08 pm on Nov 2, 2006 (gmt 0)

10+ Year Member



Would this line

<option";
if($centerPoint=="41.409775, -80.744018), 7") echo " SELECTED";
echo " value=\"41.409775, -80.744018), 7\">The Entire Area</option>

be changed to this?

<option";
if($_POST['centerPoint'] == "41.409775, -80.744018), 7") echo " SELECTED";
echo " value=\"41.409775, -80.744018), 7\">The Entire Area</option>

eelixduppy

11:13 pm on Nov 2, 2006 (gmt 0)




<option value="41.409775, -80.744018, 7"
<?php
echo ($_POST['centerPoint'] == "41.409775, -80.744018, 7")? " SELECTED":"";
?>
>The Entire Area</option>

Your logic would work also, just you have some syntax errors. The above is just a more condensed version using the ternary operator.

[edited by: eelixduppy at 11:18 pm (utc) on Nov. 2, 2006]

mcibor

11:17 pm on Nov 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also try to see what exactly is in the $_POST variable:

print_r($_POST);
die();

just for testing reasons, certainly

Michal

neoh

11:24 pm on Nov 2, 2006 (gmt 0)

10+ Year Member



I need to have the ) as part of the data. Will this effect it?

Example:

"41.409775, -80.744018), 7"

eelixduppy

11:29 pm on Nov 2, 2006 (gmt 0)



No it's fine, I just thought it to be a spelling error ;)

neoh

11:36 pm on Nov 2, 2006 (gmt 0)

10+ Year Member



Works now. Thank you very much. You guys are great. :)

neoh

11:43 pm on Nov 2, 2006 (gmt 0)

10+ Year Member



I have one more problem. I select the 2nd option and my 3rd option and it works but when I go back to my first option it doesn't work. Does it have to do with these lines of code?

//get the first result if a category was selected
if(isset($_REQUEST['centerPoint']))
$centerPoint=$_REQUEST['centerPoint'];
//if no category was selected, these results won't exist, so define default values
if(!isset($centerPoint) or strlen($centerPoint)<1) $centerPoint="41.409775, -80.744018), 7";

eelixduppy

11:50 pm on Nov 2, 2006 (gmt 0)



Try something like this:

if(![url=http://us2.php.net/manual/en/function.empty.php]empty[/url]($_POST['centerPoint'])) {
$centerPoint = $_POST['centerPoint'];
} else {
$centerPoint = "41.409775, -80.744018), 7";
}

neoh

12:00 am on Nov 3, 2006 (gmt 0)

10+ Year Member



I tried it and its still not working right. The first result is shown when I first open the page. I select the next 2 options and they work then wnt I go back to the first option it doesn't work. This is for a google map by the way. Can someone check my link to see what it is doing?

neoh

12:32 am on Nov 3, 2006 (gmt 0)

10+ Year Member



The first time the page is open nothing is selected (see source code)

<select name="centerPoint" class="selectBoxes">
<option name="centerPoint" value="41.409775, -80.744018,) 7">The Entire Area</option>
<option name="centerPoint" value="41.849616, -79.143447), 11">Allegheny River - Kinzua Dam to Buckaloons Rec Area</option>
<option name="centerPoint" value="41.403941, -81.171562), 12">Cuyahoga River - Russell Park</option>
</select>

I am able to select the second and third options and the will show selected but if I select the first option it never shows selected. Any ideas why?

outdoorxtreme1

1:01 am on Nov 3, 2006 (gmt 0)

10+ Year Member



Should I make a option with value like "Select an Area" or is there a way around this?

outdoorxtreme1

1:06 am on Nov 3, 2006 (gmt 0)

10+ Year Member



Nevermind I just figued it out. I was an overlooked mistake on my part. Thanks for the help everyone.

eelixduppy

3:19 am on Nov 3, 2006 (gmt 0)



if($outdoorxtreme1 == $neoh) {
echo 'Glad you got it working!';
} else {
echo 'What?';
}
;)