php - 在表单中添加 Action 变量

标签 php javascript ajax

我正在尝试学习 HTML 和 PHP。在我在互联网上找到的一个示例中,我需要为提交按钮设置一个变量。因此,当按下提交按钮时,此页面将重新加载,地址栏中会出现一个变量,该变量是下拉菜单中的变量。像这样:

test.php?idneeded=$variable

其中 $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,我需要这个来完成一个项目。如果有人能在这方面帮助我,我将不胜感激。

谢谢。

最佳答案

我不太明白你的问题,但我会尽力帮助你。在 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>

提交后的网址为: 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 .     

关于php - 在表单中添加 Action 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10774691/

相关文章:

php - 在页面之间保留表单数据

javascript - 修复后如何在滚动条上居中 Bootstrap navbar-nav?

php - PHP/MySQL/Apache 中的最佳登录实现

javascript - 在使用 ajax 将 html 插入页面后选择 dom 元素

javascript - AJAX/PHP - 创建 SQL 表的滞后时间

php - 从地址字段中拆分门牌号

php - Laravel - 如何知道关系表中是否存在该属性

php - 使用 Symfony 和 Doctrine 更新最后一个数据库条目

javascript - 没有什么等待吗?使用空的等待来分解大型同步函数

javascript - 动态定位确认框