1

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.

4
  • AS far as i have understood your question is whenever user chooses the dropdown the value of the drop down he chooses is required and to be displayed in the what div am i right please correct me if your requirement is a different one Commented Aug 1, 2014 at 11:36
  • @NaveenThally - yes- that is correct. But also bear in mind that because PHP runs 1st and then the Javascript runs second, the div element need to be updated with the new PHP variable. Commented Aug 1, 2014 at 19:02
  • Better: if(isset($_POST['name']) && $_POST['name'] == 'X') { .. } Commented Aug 1, 2014 at 19:05
  • @true - so do you mean something like-- <option <?php if ($_POST['name'] == '120'){ print 'selected'; $bin_sec=120; }?> value="120">2 Minutes</option> --to become-- <option <?php if(isset($_POST['name']) && $_POST['name'] == '120'){ print 'selected'; $bin_sec=120; }?> value="120">2 Minutes</option> --is this what you had mind? Commented Aug 1, 2014 at 19:49

3 Answers 3

1
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

Sign up to request clarification or add additional context in comments.

3 Comments

this code works! THANK YOU SO MUCH FOR THIS! you have just ended my rainstorm of embarrassment :)
what were the links that you tried to embed in your code above? When I click "There" and "Many Ways" I'm not forwarded to any site.
hi sorry for that, those are not links i had formatted the whole reply has code which feels like links , cheers
0

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;
?>

1 Comment

your reasoning seems to be perfect but when I apply the change I still don't 1) update the PHP variable $bin_sec and 2) the div (id="WHAT") does not reload to reflect the updated value of $bin_sec. my change is the following: testing.php now looks like: <?php $bin_sec = $_POST['name']; echo $bin_sec;?> <script type="text/javascript"> $("#WHAT").load("index.php #WHAT")</script> - does this change reflect what you had in mind?
0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.