/    Sign up×
Community /Pin to ProfileBookmark

Assign value from drop down box to php variable

Hello

I need some solution for the following problem:
I would like to let user choose one item from the drop down box and then update the page with the image of the chosen item.
I’m really new to web programming so I was hoping if I could get any help from experts ?

Here’s my code (even though it’s mostly useless ?) but I think you will understand what I tried to accomplish.

[code=php]
<?php
if (!isset($_REQUEST[‘imagelist’]))
{ $currentImage=”1/0.jpg”; }
else
{ $currentImage=$_REQUEST[‘imagelist’];}
?>
[/code]

Then the drop down box

[code=php]
<select name=”imagelist”
onchange=”javascript:window.location=’index.php’;”>

<?php
for ($i = $numberOfImages; $i > 1; $i–) { echo ‘<option value=’.$i.’/0.jpg>Image ‘.$i.'</option>’ ; }
?>
[/code]

And image

[code=php]
<img src=”<?php echo $currentImage ?>” class=”currentImage”/>
[/code]

Only thing that is really bugging me is the first part, where i try to assign chosen item from the drop down box to a php value. I know that some java would come in handy and my code is pretty noob-ish, but I just started with web programming, so any help would be very much appreciated.

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@bals28mjkJun 02.2008 — Hello Gray Fox.

It's probably best that you write two separate scripts, one for JavaScript, and one for PHP. Not all users have JavaScript enabled hence the need for a PHP script.

For the php script there is no need for a loop. Give the select element a name and you can access the submitted element via a super global $_POST['aselectname']. The value returned will be whatever text you put in the option <option>thisvalue</option>, or will be the according option attribute, (if assigned) <option value="thisvalueinstead">notthisvalue</option>.

This is similar with JavaScript, but instead you access the <form> and <img> via DOM methods.
×

Success!

Help @Gray_Fox spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...