Reputation: 21
On a webpage I am trying to create a dropdown menu with some features. After a selection has been made in the dropdown menu the PHP variable "$bin_sec" gets updated to a new value. After $bin_sec has been updated I want a div element (with the id tag of "WHAT") in my webpage to now be reloaded and now take on the new value of $bin_sec.
I'm open to using AJAX, PHP, jQuery, MySQL, etc...
I can't find or come to a working solution.
My code for the dropdown menu:
<form id="container2" method="post">
<select name="name" onchange='$("#WHAT").load("testing.php",{bin_sec:x});'>
<?php $bin_sec=60;?>
<option selected="selected" <?php if ($_POST['name'] == '60'){ print 'selected'; $bin_sec=60; }?> value="60">1 Minute</option>
<option <?php if ($_POST['name'] == '120'){ print 'selected'; $bin_sec=120; }?> value="120">2 Minutes</option>
<option <?php if ($_POST['name'] == '300'){ print 'selected'; $bin_sec=300; }?> value="300">5 Minutes</option>
<option <?php if ($_POST['name'] == '600'){ print 'selected'; $bin_sec=600; }?> value="600">10 Minutes</option>
</select>
</form>
<div id="WHAT">
<?php
echo $bin_sec;
...
?>
</div>
testing.php:
<?php
$bin_sec = $_POST['name'];
?>
<script type="text/javascript">
$("#WHAT").load("index.php #WHAT")
</script>
I think this code should work but it doesn't... Any corrections would be very much appreciated. Thank you in advance.
Upvotes: 1
Views: 879
Reputation: 1014
There are Many Ways to do it one sample way is given below
<?php
if(isset($_GET['bin_sec']))
{
$bin_sec=$_GET['bin_sec'];
}
else
{
$bin_sec=60;
}
?>
<select name="bin" onchange="window.location.href='stack.php?bin_sec='+this.value">
<option <?php if($bin_sec == '60'){ print 'selected';}?> value="60">1 Minute</option>
<option <?php if ($bin_sec == '120'){ print 'selected'; }?> value="120">2 Minutes</option>
<option <?php if ($bin_sec == '300'){ print 'selected'; }?> value="300">5 Minutes</option>
<option <?php if ($bin_sec == '600'){ print 'selected'; }?> value="600">10 Minutes</option>
</select>
<div id="WHAT">
<?php
echo $bin_sec;
?>
</div>
I hope it helps and stack.php is your php file where dropdown exists i hope you understand for any doubts revert back
Upvotes: 1
Reputation: 164
You can use this code:
<option <?php if ($_POST['name'] == '120'){ print 'selected'; =120; }?> value=" <?php $_POST['name'] == '120'? "$bin_sec_value":120 ; ?>">2 Minutes</option>
Upvotes: 0
Reputation: 201
You will need to echo out $bin_sec in PHP in order for it to be sent to the browser.
Additionally you need to change your $_POST var to the same name as you are sending from the browser as $_POST['name'] does is undefined
Example below
<?php
$bin_sec = $_POST['bin_sec'];
echo $bin_sec;
?>
Upvotes: 0