Jump to content

php change option


matthew9090

Recommended Posts

i have a basic html form with a drop down selection box with 2 options ('old' and 'new'). when you click one it swaps them round to you can click the other because it is an onchange event.

 

<form action="<?php $_SERVER['self']; ?>" method="get">
<select name="Time" onchange="this.form.submit();">
<option  value="New">New</option>
<option  value="Old">Old</option>
</select></form> 

Link to comment
Share on other sites

i'm thinking something like this?

<?php
	if (isset($_GET['Time']))
	{
	?>
<script type="text/javascript">
//javascript code here
</script>
<?php
}
?>
	<form action="<?php $_SERVER['self']; ?>" method="get">
	<select name="Time" onchange="this.form.submit();">
<option id="1" value="New">New</option>
<option id="2" value="Old">Old</option>
</select></form>

 

but i don't know how to use javascript to swap them round. any ideas?

Link to comment
Share on other sites

One of the

 


 

I'm not sure how you're planning to use this but it is not a question of javascript, since you're submitting the form anytime this is changed.  Instead, it's a question of examining the $_GET[] for the current value and setting the selected to be that value in your php script.  This is because you specified the method of the form to be get, but if it was a "post" method as most forms are, you'd look at $_POST.

 

$timeoptions = array(1 => 'New', 2 => 'Old');

if (isset($_GET['Time'])) {
        $selectoption = ($_GET['Time'] == 'Old') ? 'Old' : 'New';
}
?>
</pre>
<form action="<?php%20%24_SERVER%5B'self'%5D;%20?>" method="get">

foreach ($timeoptions as $id => $value) {
        $selected = ($selectoption == $value) ? ' selected' : '';
        echo "$value";
}
?>

</form

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.