Nasan Ert
Nasan Ert

Reputation: 41

Change PHP variable values using drop down menu

I wondering if any know way to change values of variable using option drop-down list ??

php script is:

<?php $searchBoxfruit = "Red"; require_once('includes/search_box.php'); ?>

and option list

<option value="Type">Type</option>
            <option value="Red">Red</option>
            <option value="Green">Green</option>
            <option value="Gray">Gray</option> 
            </select>

so basicly when user selected green option in drop down menu, $searchBoxfruit values must change to Green.

I try to fix this problem with this code:

$searchBoxfruit = "";

if (isset($_GET['0'])):  
    $searchBoxfruit = "Red";
elseif (isset($_GET['1'])):
   $searchBoxfruit = "Blue";
elseif (isset($_GET['2'])):  
    $searchBoxfruity = "White";
else:  (isset($_GET['3'])):
    $searchBoxfruit = "Black";  
endif;

php $searchBoxfruit = ""; require_once('includes/search_box.php');

but still it s does not work !!

Upvotes: 0

Views: 2272

Answers (4)

Amit Sharma
Amit Sharma

Reputation: 1988


<?php
<form action="" method="post" id="kid">
<select name="op">
<option value="Pink">Pink</option>
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Gray">Gray</option>
</select>


<input type="submit" value="SUBMIT" name='but'>
</form>
<?php
<br>if( $_POST ) <br>{ <br> var_dump( $_POST );
<br>} <br>



?>

I think u should study this code a bit

Upvotes: 0

Gerald Versluis
Gerald Versluis

Reputation: 34013

This can't be done with purely HTML/PHP.

You could trigger a form post when the users selects another option, or, better yet, do an AJAX call to set the variable.

But why would you want to do it real time?

Just get the value when the form is submitted in the $_POST['nameofyoucontrol'] variable.

Upvotes: -1

Salepate
Salepate

Reputation: 327

Php is always interpreted before sending content to client. HTML is part of that content. Once you get to see HTML elements, such as your dropdown list, you can't tell PHP to modify an exisiting variable.

The best thing to do is to use a form whichs sends GET or POST data to a php page.

Upvotes: 2

Shubham
Shubham

Reputation: 22317

Thats the quite basic part of form processing. Like,

If your "<select>" is named fruit and the form is submitted.

$_POST['fruit'] shall give the value.

Upvotes: 0

Related Questions