2012-05-27 6 views
0

HTML과 PHP를 배우려고합니다. 내가 인터넷을 통해 발견 한 예제에서 제출 버튼에 변수를 설정해야합니다. 따라서 제출 버튼을 누르면이 페이지가 다시로드되며 주소 표시 줄에 변수가 있으면 변수가 드롭 다운 메뉴의 변수가됩니다. 은 $ 변수는 사용자가 선택한 후 페이지가 선택한 옵션에 관련된 특정 내용을 표시하기 위해 다시로드됩니다형태로 작동중인 변수 추가

test.php?idneeded=$variable 

있는이있다. 예를 들어

:

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

내가 시작하지 않은 학습 :

test.php?idneeded=40 

내가 찾은 코드는 이것이다 (40 양식 드롭 다운에서 "MadTechie"입니다) 자바 스크립트, 그리고이 프로젝트에 필요합니다. 나는 누군가가 이것에 저를 도울 수있는 경우에 그것을 평가할 것이다.

감사합니다.

+0

질문이 너무 모호 할 것이다. 게시 된 샘플 코드는 질문과 관련이 거의 없습니다. –

답변

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 .  
+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