English 中文(简体)
以窗体在动作中添加变量
原标题:add variable in action in a form

我试图学习 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>
";
      }
      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>
";
   }
?>
<!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, 我需要这个项目。 如果有人能帮我, 我会很感激的。

谢谢

最佳回答

i 不理解您的问题差异很大, 但我会试着帮助您。 在 html 中, 请确定您使用方法 = “ get” 来显示窗体, 这样变量就会被传送到 url 中的php 。 (在其它情况下, POST 需要 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>

the url after submiting will be : http://mypage.com/phpscript.php?just_a_test=somevalue&submit=submit_form

在另一侧的php脚本中,将使用窗体中的数据的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 .     
问题回答

如果在重定向期间发送表格,您没有使用 AJAX。在这种情况下,解决方案很简单:

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

这样的事情在每个 HTML 教程中都会被解释。 这是一个良好的起点 : < a href="http://www.w3schools.com/html/default.asp" rel="no follow">W3C school 。

您没有提及客户端或服务器上是否可用变量的值?

Variable on Client: Basically, you will have to handle the onSubmit event of the form. Here you can append the value of the variable to the action.

Variable on Server: Here you would change the action when you are rendering the HTML.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签