PHP select form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dujmovicv
    New Member
    • Jul 2006
    • 10

    #1

    PHP select form

    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!
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    Code:
    <?
    $language = $_GET["language"];
    ?>
    if ($language = 1) {
    ?>
    this picture changes cause language is set to English
    <?
    }
    if ($language = 2) {
    ?>
    this picture is something cause the language is set to chinese
    <?
    }
    ?>
    <select onchange="window.location='whatever.php?language='+this.value">
    <option value="1">English</option>
    <option value="2">Chinese</option>
    </select>
    small sample of how this would be done whatever.php = the page your on.
    Last edited by iam_clint; Jul 17 '06, 11:52 PM.

    Comment

    • dujmovicv
      New Member
      • Jul 2006
      • 10

      #3
      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="windo w.location='sel ect.php?languag e='+this.value" >
      <option value="1">Engli sh</option>
      <option value="2">Chine se</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....

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        you need a better javascript then what i gave you it was an example
        Code:
        <select onchange="window.location='select.php?language='+this.value">
        <option value="0">- Select Language -</option>
        <option value="1" <?if ($language == 1) {?>selected<?}?>>English</option>
        <option value="2" <?if ($language == 2) {?>selected<?}?>>Chinese</option>
        </select>

        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.

        Comment

        • dujmovicv
          New Member
          • Jul 2006
          • 10

          #5
          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="JavaS cript">

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

          </script>

          <?
          print <<<HERE
          <select name="select" onchange="jumpm enu(this)">
          <option value="1" selected>Srpski </option>
          <option value="2">Magya r</option>
          <option value="3">Engli sh</option>
          <option value="4">Deuts ch</option>
          </select>
          HERE;
          ?>

          As you can suppose it doesn't works...

          Comment

          • dujmovicv
            New Member
            • Jul 2006
            • 10

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

            [php]
            <?php
            $language = $_GET["language"];
            ?>
            <select onchange="windo w.location='sel ect.php?languag e='+this.value" >
            <?php
            $language = array(
            "",
            "English",
            "Deutsch",
            "Magyar",
            "Srpski");
            for ($i = 1; $i <= 4; $i++)
            {
            print "<option value=$i>$langu age[$i]</option>";
            print "<h3>Langua ge 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?

            Comment

            • iam_clint
              Recognized Expert Top Contributor
              • Jul 2006
              • 1207

              #7
              Code:
              <?php
              $lang = $_GET["language"];
              ?>
              <select onchange="window.location='select.php?language='+this.value">
              <?php
              $language = array(
                  "-- Make A Selection --",
                  "English",
                  "Deutsch",
                  "Magyar",
                  "Srpski");
              for ($i = 1; $i <= 4; $i++)
              {
              ?>
              <option value="<?=$i?>" <?if ($lang==$language[$i]) { print "selected"; }><?=$language[$i]?></option>
              <?
              }
              ?>
              </select>
              <h3>Language is : <?=$lang?></h3>
              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.

              Comment

              • iam_clint
                Recognized Expert Top Contributor
                • Jul 2006
                • 1207

                #8
                Code:
                <?php
                $lang = $_GET["language"];
                ?>
                <select onchange="window.location='select.php?language='+this.value">
                <?php
                $language = array(
                    "-- Make A Selection --",
                    "English",
                    "Deutsch",
                    "Magyar",
                    "Srpski");
                for ($i = 1; $i <= 4; $i++)
                {
                ?>
                <option value="<?=$i?>" <?if ($lang==$i) { print "selected"; }><?=$language[$i]?></option>
                <?
                }
                ?>
                </select>
                <h3>Language is : <?=$lang?></h3>
                my bad changed one thing. if ($lang==$langua ge[$i]) { needed to be if ($lang==$i)

                Comment

                • dujmovicv
                  New Member
                  • Jul 2006
                  • 10

                  #9
                  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="windo w.location='sel ect.php?languag e='+this.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?>.gi f" 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

                  Comment

                  • iam_clint
                    Recognized Expert Top Contributor
                    • Jul 2006
                    • 1207

                    #10
                    ok to set it as 1 by default
                    Code:
                    <?php 
                    $lang = $_GET["language"]; 
                    if ($lang=="") {
                        $lang = 1;
                    }
                    ?>
                    for the image you would do this on your page where you want the image
                    Code:
                    <? if ($lang==1) { ?>
                    what ever here for the image
                    <? } ?>

                    Comment

                    Working...