javascript 如何在 PHP 中获取 html <input type="hidden"> 的值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6006626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the value of html <input type="hidden"> in PHP?
提问by Newbie Coder
I am a certifid newbie so pls be patient with me if my question might sound very idiotic to you.
我是一名合格的新手,所以如果我的问题对你来说听起来很愚蠢,请耐心等待。
Hi guys. Im trying to get the value of an html <input type="hidden">
using PHP. The value of that hidden element is passed from Javascript and I need to display the value of that hidden element by using php echo. From what I understand (I hope I understood it right) when you do something like $description = isset($_GET['description']) ? $_GET['description'] : null;
and also <input type="hidden" id="description" value="<?php echo $description; ?>" />
you can access/display the value of the hidden element by using <?php echo $_GET['description']; ?>
but I am getting an undefined index: description. I am a total noob so I am really confused about this. I hope my question is not that confusing. Anyway I am very willing to answer probing questions.
嗨,大家好。我正在尝试<input type="hidden">
使用 PHP获取 html 的值。该隐藏元素的值是从 Javascript 传递的,我需要使用 php echo 显示该隐藏元素的值。据我所知(我希望我理解正确)当你做类似的事情时,你$description = isset($_GET['description']) ? $_GET['description'] : null;
也<input type="hidden" id="description" value="<?php echo $description; ?>" />
可以访问/显示隐藏元素的值,<?php echo $_GET['description']; ?>
但我得到了一个未定义的索引:描述。我是个菜鸟,所以我真的很困惑。我希望我的问题没有那么令人困惑。无论如何,我非常愿意回答探索性的问题。
BTW, here are the codes:
顺便说一句,这里是代码:
index.php
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
functions.php
<?php
function hspacer($num_of_spaces) {
$spaces = "";
if ($num_of_spaces > 0) for($i=0; $i<$num_of_spaces; $i++ ) $spaces .= " ";
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
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";
}
}
回答by Chris Bornhoft
You need to give the input a name. So:
您需要为输入命名。所以:
<input name="description" />
回答by tlunter
You would need to set the name of the <input type="hidden">
like <input type="hidden" name="hiddenfield">
您需要设置<input type="hidden">
类似的名称 <input type="hidden" name="hiddenfield">
Then you can reference this field in the PHP by $_GET['hiddenfield']
. Make sure the input is in the form.
然后你可以在 PHP 中通过$_GET['hiddenfield']
. 确保输入在表单中。
回答by Denis de Bernardy
You need to give your input a name, an id or both: <input type="hidden" id="foo">
您需要为您的输入提供名称、ID 或两者: <input type="hidden" id="foo">
A few side notes...
一些旁注...
There's a very useful javascript library that you might want to try:
有一个非常有用的 javascript 库,您可能想尝试一下:
It comes with multitudes of UI elements and plugins, and allows to do many things in a much simpler way than what you're currently writing.
它带有大量的 UI 元素和插件,并允许以比您当前编写的方式更简单的方式做很多事情。
You might also want to document yourself a bit on SQL injections, cross-site scripting and cross-site request forgery. A lot of stuff needs to be escaped in your code.
您可能还想对自己的 SQL 注入、跨站点脚本和跨站点请求伪造做一些记录。很多东西需要在你的代码中转义。
回答by exahex
Inside the show_description div replace $_GET['description'] with $description.
在 show_description div 中,将 $_GET['description'] 替换为 $description。
EDIT: I think I understand why you are confused now. If you want to display the value of the description input in the show_description div, you need to use javascript to do that. PHP is server side and it only runs once, when the page is requested by the browser. If you are doing anything that happens after the page is loaded, you need to use javascript. That said, make this change to your check_year_event() function.
编辑:我想我明白你为什么现在感到困惑了。如果要在 show_description div 中显示 description 输入的值,则需要使用 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;
}
}
And remove the echo $_GET['description']
from the index.php file
并echo $_GET['description']
从 index.php 文件中删除