2012-05-27 32 views
0

我在學習HTML和PHP。在我通過互聯網找到的一個例子中,我需要爲提交按鈕設置一個變量。所以當提交按鈕被按下時,這個頁面重新加載,在地址欄中有一個變量,變量是下拉菜單中的變量。像這樣:在表單中添加變量

test.php?idneeded=$variable 

其中$變量是由用戶選擇,然後重新加載頁面,顯示有關所選選項具體內容。

例如:

test.php?idneeded=40 

(40是從下拉表單 「MadTechie」)

我找到的代碼是這樣的:

<?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> 

我還沒有開始學習JavaScript,我需要這個項目。如果有人能幫助我,我會很感激。

謝謝。

+0

問題太含糊。發佈的示例代碼與問題很少有關。 –

回答

2

我不明白你的問題各不相同,但我會盡力幫助你。在html中,請確保您使用方法=「get」作爲表單,並以這種方式將變量傳遞給url中的php。 (在其他情況下需要POST,但現在即使獲得也可以)。所有具有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> 

submiting後的網址是: 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 .  
+0

謝謝。你剛剛解決了我的問題。 – Siwa

1

如果表單應該在重定向期間發送,則表示您沒有使用AJAX。在這種情況下,解決方案很簡單:

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

這樣的事情在每個HTML教程中都有解釋。這是一個很好的起點:W3C Schools

1

您還沒有提到變量的值是否可以在客戶端或服務器上使用?

客戶端變量: 基本上,您將不得不處理窗體的onSubmit事件。在這裏,您可以將該變量的值附加到該操作。

服務器上的變量: 在這裏,您將更改當您呈現HTML時的操作。

+0

謝謝,我剛剛從你的文章中學到了另一條提示。我的變量在服務器上可用。 @Albi Patozi剛剛回答我的問題並解決了我的問題。再次感謝您的解釋。 – Siwa