如何在PHP中获取html <input type =“hidden”/>的值?

时间:2011-05-15 05:15:59

标签: php javascript html

我是一名认证新手,所以如果我的问题对你来说听起来很愚蠢,请耐心等待。

你好。我试图使用PHP获取html <input type="hidden">的值。隐藏元素的值从Javascript传递,我需要使用php echo显示该隐藏元素的值。根据我的理解(我希望我理解正确),当您执行$description = isset($_GET['description']) ? $_GET['description'] : null;<input type="hidden" id="description" value="<?php echo $description; ?>" />之类的操作时,您可以使用<?php echo $_GET['description']; ?>访问/显示隐藏元素的值,但我是得到一个未定义的索引:description。我是一个总菜鸟,所以我真的很困惑。我希望我的问题不那么令人困惑。无论如何,我非常愿意回答探究性问题。

顺便说一句,这是代码:

index.php

<?php include('functions.php'); ?>

<?php 
 $yr = isset($_GET['year_list']) ? $_GET['year_list'] : null;
 $evnt = isset($_GET['event_list']) ? $_GET['event_list'] : null; 
 $description = isset($_GET['description']) ? $_GET['description'] : null; 
?>

<html>
<head>
 <script type="text/javascript" src="myscripts.js"></script>
 <style type='text/css'>
  #show_description {
   min-height: 100px;
   min-width: 500px;
   max-height: 100px;
   max-width: 500px;
   background-color: #000;
   color: #fff;
   padding: 10px;
  }
 </style>
</head>
<body onload="check_year_event();">
 <div>
  <form name="myform" action="index.php" method="get" >
   <input type="hidden" id="hidden_year_list" value="<?php echo $yr; ?>" />
   <input type="hidden" id="hidden_event_list" value="<?php echo $evnt; ?>" />
   <input type="hidden" id="description" value="<?php echo $description; ?>" />

   Select Year: <?php echo hspacer(1); ?>
   <select id="year_list" name="year_list" onchange="check_year_event();" >
   <?php  
    for($year = (date('Y') - 100); $year <= (date('Y') + 100); $year++ ) {
     if ($year == date('Y'))  echo "<option value='$year' name='$year' selected='' >" . $year . "</option>";
     else echo "<option value='$year' name='$year' >" . $year . "</option>";
    }
   ?>
   </select>
   <?php echo hspacer(5); ?>
   Select Event:  <?php echo hspacer(1); ?>
   <select id="event_list" name="event_list" onchange="check_year_event();" >
   <?php  
    $events = array("Karate Tournament", "Beauty Pageant", "Film Festival", "Singing Contest", "Wedding");

    foreach($events as $event) echo "<option value='$event' name='$event' >" . $event . "</option>";
   ?>
   </select>
   <?php echo vspacer(2); echo hspacer(22); ?>
   <input type="submit" id="send_notice" value="Send Notice" onclick="check_year_event(); return false; "/> 
  </form>
 </div>

 <div id="show_description" >
  <?php echo $_GET['description']; ?>
 </div>

</div>
</body>
</html>

functions.php

<?php
 function hspacer($num_of_spaces) {
  $spaces = "";
  if ($num_of_spaces > 0)  for($i=0; $i<$num_of_spaces; $i++ )  $spaces .= "&nbsp;";

  return $spaces;
 }

 function vspacer($num_of_linefeeds) {
  $linefeeds = "";
  if ($num_of_linefeeds > 0)  for($i=0; $i<$num_of_linefeeds; $i++ )  $linefeeds .= "<br />";

  return $linefeeds;
 }
?>

myscripts.js

function create2DArray(row, col){
 var array2D = new Array(row);

 for (var i = 0; i < row; i++) {
  array2D[i] = new Array(col);
 }

 return array2D;
}


function check_year_event() {
 var years_and_events = create2DArray(10, 3);


 years_and_events[0][0] = 2001; 
 years_and_events[0][1] = "Karate Tournament"; 
 years_and_events[0][2] = "Annual karate tournament held globally"; 
 years_and_events[1][0] = 2002; 
 years_and_events[1][1] = "Beauty Pageant"; 
 years_and_events[1][2] = "Beauty pageant held globally"; 
 years_and_events[2][0] = 2003; 
 years_and_events[2][1] = "Film Festival"; 
 years_and_events[2][2] = "Film festival held globally"; 
 years_and_events[3][0] = 2004; 
 years_and_events[3][1] = "Singing Contest"; 
 years_and_events[3][2] = "Singing contest tournament held globally"; 
 years_and_events[4][0] = 2005; 
 years_and_events[4][1] = "Wedding"; 
 years_and_events[4][2] = "Wedding tournament held globally"; 
 years_and_events[5][0] = 2007; 
 years_and_events[5][1] = "Karate Tournament"; 
 years_and_events[5][2] = "Annual karate tournament held globally"; 
 years_and_events[6][0] = 2008; 
 years_and_events[6][1] = "Beaty Pageant"; 
 years_and_events[6][2] = "Beauty pageant held globally"; 
 years_and_events[7][0] = 2009; 
 years_and_events[7][1] = "Film Festival"; 
 years_and_events[7][2] = "Film festival held globally"; 
 years_and_events[8][0] = 2010; 
 years_and_events[8][1] = "Singing Contest"; 
 years_and_events[8][2] = "Singing contest tournament held globally"; 
 years_and_events[9][0] = 2011; 
 years_and_events[9][1] = "Wedding"; 
 years_and_events[9][2] = "Wedding tournament held globally"; 


 var year = document.getElementById('year_list').value;
 var event = document.getElementById('event_list').value;
 var found = false;

 for (var i = 0; i < years_and_events.length; i++) {
  if ((year == years_and_events[i][0]) && (event == years_and_events[i][1])) {
    document.getElementById('description').value = years_and_events[i][2];
    document.getElementById('send_notice').style.visibility = "hidden";
    document.getElementById('show_description').style.visibility = "visible";
    found = true;
    break;
   }
  }

 if (found == false) {
  document.getElementById('show_description').style.visibility = "hidden";
  document.getElementById('send_notice').style.visibility = "visible";
 }
}

4 个答案:

答案 0 :(得分:2)

您需要为输入命名。所以:

<input name="description" />

答案 1 :(得分:1)

您需要设置<input type="hidden">的名称,如<input type="hidden" name="hiddenfield">

然后您可以通过$_GET['hiddenfield']在PHP中引用此字段。确保输入在表单中。

答案 2 :(得分:1)

您需要为输入指定名称,ID或两者:<input type="hidden" id="foo">

一些附注......

您可能想尝试一个非常有用的JavaScript库:

http://jquery.com/

它包含大量的UI元素和插件,并且允许以比您当前编写的更简单的方式执行许多操作。

您可能还希望自己记录一下SQL注入,跨站点脚本和跨站点请求伪造。需要在代码中转义很多东西。

答案 3 :(得分:1)

在show_description div中,用$ description替换$ _GET ['description']。

编辑:我想我明白为什么你现在感到困惑。如果要在show_description div中显示描述输入的值,则需要使用javascript来执行此操作。 PHP是服务器端,当浏览器请求页面时,它只运行一次。如果您正在执行加载页面后发生的任何事情,则需要使用javascript。也就是说,对check_year_event()函数进行此更改。

 for (var i = 0; i < years_and_events.length; i++) {
  if ((year == years_and_events[i][0]) && (event == years_and_events[i][1])) {
    document.getElementById('description').value = years_and_events[i][2];
    document.getElementById('show_description').innerHTML = years_and_events[i][2];
    document.getElementById('send_notice').style.visibility = "hidden";
    document.getElementById('show_description').style.visibility = "visible";
    found = true;
    break;
  }
 }

从index.php文件中删除echo $_GET['description']