By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,615 Members | 1,961 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,615 IT Pros & Developers. It's quick & easy.

PHP select form

P: 10
I'm new in PHP programming, I suppose that my problem will be a child's-play for experts....
I need to write a PHP script with a "select" input form, which will offer 4 languages to chose between. When the user clicks on an item in the select list, the menu (buttons), some pictures shall change on the page regarding to the choice.
Please if exists, send me an example or good links for building web pages in PHP.
Thank You!
Jul 17 '06 #1
Share this Question
Share on Google+
9 Replies


iam_clint
Expert 100+
P: 1,208
Expand|Select|Wrap|Line Numbers
  1. <?
  2. $language = $_GET["language"];
  3. ?>
  4. if ($language = 1) {
  5. ?>
  6. this picture changes cause language is set to English
  7. <?
  8. }
  9. if ($language = 2) {
  10. ?>
  11. this picture is something cause the language is set to chinese
  12. <?
  13. }
  14. ?>
  15. <select onchange="window.location='whatever.php?language='+this.value">
  16. <option value="1">English</option>
  17. <option value="2">Chinese</option>
  18. </select>
  19.  
small sample of how this would be done whatever.php = the page your on.
Jul 17 '06 #2

P: 10
Thank you for your quick help!

There are some errors in your script, I managed to correct some things, but it still doesn't work...

<?
$language = $_GET["language"];

if ($language == 1) {
?>
English
<?
}
if ($language == 2) {
?>
Chinese
<?
}
?>
<select onchange="window.location='select.php?language='+t his.value">
<option value="1">English</option>
<option value="2">Chinese</option>
</select>

The script prints "Chinese" when you select "Chinese", but in the select field is still "English", and when click on "English", nothing happens....
Jul 18 '06 #3

iam_clint
Expert 100+
P: 1,208
you need a better javascript then what i gave you it was an example
Expand|Select|Wrap|Line Numbers
  1. <select onchange="window.location='select.php?language='+this.value">
  2. <option value="0">- Select Language -</option>
  3. <option value="1" <?if ($language == 1) {?>selected<?}?>>English</option>
  4. <option value="2" <?if ($language == 2) {?>selected<?}?>>Chinese</option>
  5. </select>
  6.  

i am trying to refrain from giving you code without trial and error you will never know exactly what you did wrong.. anymore that needs to be done to this script you can code and i can check your code.
Jul 18 '06 #4

P: 10
Hello iam_clint!

I'm back again with my probleme....

So, as I mentioned I've tried to combine php and java to build a dynamic menu with select form. Here is my code (example), I wonder if I'm going on the right way....
Please help if U have some good ideas... ;-)

<script language="JavaScript">

function jumpmenu(selLang)
{
var s = selLang;
echo "<img src=$s.gif>";
}

</script>

<?
print <<<HERE
<select name="select" onchange="jumpmenu(this)">
<option value="1" selected>Srpski</option>
<option value="2">Magyar</option>
<option value="3">English</option>
<option value="4">Deutsch</option>
</select>
HERE;
?>

As you can suppose it doesn't works...
Aug 11 '06 #5

P: 10
I managed to echo a select drop-down menu with 4 languages with an array:

[php]
<?php
$language = $_GET["language"];
?>
<select onchange="window.location='select.php?language='+t his.value">
<?php
$language = array(
"",
"English",
"Deutsch",
"Magyar",
"Srpski");
for ($i = 1; $i <= 4; $i++)
{
print "<option value=$i>$language[$i]</option>";
print "<h3>Language is : $language[$i]</h3>";
}
?>
</select>
[/php]

I can't see my mistake : in the select field always stays "English" and don't change to the selecteg language... The other question: the script doesn't prints the text in <h3> tag. Why is that?
Aug 15 '06 #6

iam_clint
Expert 100+
P: 1,208
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $lang = $_GET["language"];
  3. ?>
  4. <select onchange="window.location='select.php?language='+this.value">
  5. <?php
  6. $language = array(
  7.     "-- Make A Selection --",
  8.     "English",
  9.     "Deutsch",
  10.     "Magyar",
  11.     "Srpski");
  12. for ($i = 1; $i <= 4; $i++)
  13. {
  14. ?>
  15. <option value="<?=$i?>" <?if ($lang==$language[$i]) { print "selected"; }><?=$language[$i]?></option>
  16. <?
  17. }
  18. ?>
  19. </select>
  20. <h3>Language is : <?=$lang?></h3>
  21.  
This should fix your problems, hope this is helping you learn :).
You can see the changes i made. It didn't print in h3 tags because you were still in the select tag.
Aug 15 '06 #7

iam_clint
Expert 100+
P: 1,208
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $lang = $_GET["language"];
  3. ?>
  4. <select onchange="window.location='select.php?language='+this.value">
  5. <?php
  6. $language = array(
  7.     "-- Make A Selection --",
  8.     "English",
  9.     "Deutsch",
  10.     "Magyar",
  11.     "Srpski");
  12. for ($i = 1; $i <= 4; $i++)
  13. {
  14. ?>
  15. <option value="<?=$i?>" <?if ($lang==$i) { print "selected"; }><?=$language[$i]?></option>
  16. <?
  17. }
  18. ?>
  19. </select>
  20. <h3>Language is : <?=$lang?></h3>
  21.  
my bad changed one thing. if ($lang==$language[$i]) { needed to be if ($lang==$i)
Aug 15 '06 #8

P: 10
Thank you for your efforts, you helped me a lot to learn PHP. With some corrections in your code, I have solved the problem. :)
Now I have another question, hope I'm not boring....
How to set the $language to 1 by default (when the page is being loaded for the first time)?
[php]
<?php
$lang = $_GET["language"];
?>
<select onchange="window.location='select.php?language='+t his.value">
<?php
$language = array(
"",
"English",
"Deutsch",
"Magyar",
"Srpski");
for ($i = 1; $i <= 4; $i++)
{
?>
<option value="<?=$i?>" <? if ($lang == $i) { print "SELECTED";}?>> <?=$language[$i]?></option>
<?
}
?>
</select>
<br>
<img src = " <?=$lang?>.gif" width="40" height="20">
[/php]

In that case, the field for the *.gif image is empty when the page is loaded, and after the user choice the script loads the appropriate image. How to load the 1.gif image at the start?

Best regards
Aug 16 '06 #9

iam_clint
Expert 100+
P: 1,208
ok to set it as 1 by default
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. $lang = $_GET["language"]; 
  3. if ($lang=="") {
  4.     $lang = 1;
  5. }
  6. ?> 
  7.  
for the image you would do this on your page where you want the image
Expand|Select|Wrap|Line Numbers
  1. <? if ($lang==1) { ?>
  2. what ever here for the image
  3. <? } ?>
  4.  
Aug 16 '06 #10

Post your reply

Sign in to post your reply or Sign up for a free account.