0

I'm trying to learn HTML and PHP. In an example which i found over the internet i need to set a variable to the submit button. So when the submit button is pressed, this page reloads, with a variable in the address bar,the variable is the one from the drop down menu. like this :

test.php?idneeded=$variable

in which the $variable is selected by the user and then the page reloads to show specific content related to the chosen option.

For example :

test.php?idneeded=40

(40 is "MadTechie" from the drop down form)

the code i found is this :

<?php
   if( isset($_GET['ajax']) )
   {
      //In this if statement
      switch($_GET['ID'])
      {
         case "LBox2":
            $Data[1] = array(10=>"-Tom", 20=>"Jimmy"); 
            $Data[2] = array(30=>"Bob", 40=>"-MadTechie");
            $Data[3] = array(50=>"-One", 60=>"Two");
         break;

         //Only added values for -Tom, -MadTechie and -One (10,40,50)
         case "LBox3":
            $Data[10] = array(100=>"One 00", 200=>"Two 00");
            $Data[40] = array(300=>"Three 00");
            $Data[50] = array(1000=>"10000");
         break;
      }

      echo "<option value=''></option>";
      foreach($Data[$_GET['ajax']] as $K => $V)
      {
         echo "<option value='$K'>$V</option>\n";
      }
      mysql_close($dbh);
      exit; //we're finished so exit..
   }
   $Data = array(1=>"One", 2=>"Two", 3=>"Three");
   $List1 = "<option value=''></option>";
   foreach($Data as $K => $V)
   {
      $List1 .= "<option value='$K'>$V</option>\n";
   }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Simple Dymanic Drop Down</title>
<script language="javascript">
   function ajaxFunction(ID, Param)
   {
      //link to the PHP file your getting the data from
      //var loaderphp = "register.php";
      //i have link to this file
      var loaderphp = "<?php echo $_SERVER['PHP_SELF'] ?>";

      //we don't need to change anymore of this script
      var xmlHttp;
      try
       {
         // Firefox, Opera 8.0+, Safari
         xmlHttp=new XMLHttpRequest();
       }catch(e){
         // Internet Explorer
         try
         {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
         }catch(e){
            try
            {
               xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
            }catch(e){
               alert("Your browser does not support AJAX!");
               return false;
            }
         }
      }

      xmlHttp.onreadystatechange=function()
      {
         if(xmlHttp.readyState==4)
           {
              //the line below reset the third list box incase list 1 is changed
              document.getElementById('LBox3').innerHTML = "<option value=''></option>";

              //THIS SET THE DAT FROM THE PHP TO THE HTML
            document.getElementById(ID).innerHTML = xmlHttp.responseText;
           }
      }
       xmlHttp.open("GET", loaderphp+"?ID="+ID+"&ajax="+Param,true);
       xmlHttp.send(null);
   }
</script>
</head>
<body>
<!-- OK a basic form-->
<form method="post" enctype="multipart/form-data" name="myForm" target="_self">
<table border="0">
  <tr>
    <td>
      <!--
      OK here we call the ajaxFuntion LBox2 refers to where the returned date will go
      and the this.value will be the value of the select option
      -->
      <select name="list1" id="LBox1" onchange="ajaxFunction('LBox2', this.value);">
      <?php 
         echo $List1;
      ?>
      </select>
   </td>
    <td>
      <select name="list2" id="LBox2" onchange="ajaxFunction('LBox3', this.value);">
         <option value=''></option>
            <!-- OK the ID of this list box is LBox2 as refered to above -->
      </select>
   </td>
   <td>
      <select name="list3" id="LBox3">
         <option value=''></option>
            <!-- OK the ID of this list box is LBox3 Same as above -->
      </select>
   </td>
  </tr>
</table>
  <input type="submit" name="Submit" value="Submit" />
</form>
</body>
</html>

I haven't started learning JavaScript, and i need this for a project. I'll appreciate it if anyone can help me on this.

Thanks.

4

3 に答える 3

2

私はあなたの質問がよく異なることを理解していませんが、一般的にあなたを助けようとします. html では、フォームに method="get" を使用していることを確認してください。このようにして、変数は URL で php に渡されます。(それ以外の場合は POST が必要ですが、今のところ get でも問題ありません)。NAME 属性が設定されたすべての入力値が URL に渡されます。元:

<form action='phpscript.php' method='get' >
<input type='text' name='just_a_test' value='somevalue' />
<input type='submit' value='submit_form' name='submit' />
</form>

送信後の URL は次のようになります: http://mypage.com/phpscript.php?just_a_test=somevalue&submit=submit_form

反対側では、フォームからのデータを使用する php スクリプトは次のようになります。

<?php

if (isset($_GET['submit']) ) { 

                              if (isset($_GET['just_a_test']) )
                                  {
                                  $variable1 = $_GET['just_a_test'];
                                  //do something with variable 1 and output results
                                  //based on the value of this variable. 
                                  }
                             } 

?>

you can do the same thing for ass many variables as you want . i hope this was a help to you because i cant undestand your question better than this .     
于 2012-05-27T14:56:38.827 に答える
1

リダイレクト中にフォームが送信されることになっている場合は、AJAXを使用していません。この場合、解決策は簡単です。

<form name="myForm" action="test.php" method="GET">
    <select name="idneeded">
        <option value="40">MadTechie</option>
        <option>...</option>
    </select>
</form>

このようなことは、すべてのHTMLチュートリアルで説明されています。これは良い出発点です:W3Cスクール

于 2012-05-27T14:45:55.533 に答える
1

変数の値がクライアントまたはサーバーで利用可能かどうかについては言及していませんか?

クライアントの変数: 基本的に、フォームのonSubmitイベントを処理する必要があります。ここで、変数の値をアクションに追加できます。

サーバー上の変数: ここでは、HTMLをレンダリングするときのアクションを変更します。

于 2012-05-27T14:47:12.263 に答える