'PHP Using Select Value To Set Variable

I'm working in Wordpress here.

In the posts page I have a dropdown list of values:

<select name='imagesize'>
<option value='200x200'>200 x 200</option>
<option value='300x300'>300 x 300</option>
<option value='400x400'>400 x 400</option>
<option value='500x500'>500 x 500</option>
</select>

I have a button:

<a class="button" href="http://www.example.com/???">Click</a>

I want to replace the "???" with the value that is currently selected in the list when the user hits the button.

Any ideas how I can do this? Ajax?



Solution 1:[1]

You should be using a form submit button to submit the form. If you use a link you will need to run some javascript first to modify the link on the fly to append your information.

<form action="yourPHP.php" method="post">

<select name='imagesize'>
<option value='200x200'>200 x 200
<option value='300x300'>300 x 300
<option value='400x400'>400 x 400
<option value='500x500'>500 x 500
</select>

<input type="submit">

</form>

Then in your PHP code, access it via:

<?php
    $yourImageSize="";
    if(isset($_POST["imagesize"]))
    {
        $yourImageSize=$_POST["imagesize"];
    }
    echo "Your image size is: ".$yourImageSize."<br>";
?>

Solution 2:[2]

<select name='imagesize' id='imagesize'>
<option value='200x200'>200 x 200</option>
<option value='300x300'>300 x 300</option>
<option value='400x400'>400 x 400</option>
<option value='500x500'>500 x 500</option>
</select>
<a class="button" id="click_btn" href="http://www.example.com/???">Click</a>

you should add some javascript before the element :

<script type="text/javascript">
    $("#imagesize").change(function(){
        $("#click_btn").attr("href","http://www.example.com/"+$("#imagesize").val())?
    });
</script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fluffeh
Solution 2 nomaka