[英]Why won't the HTML select form element $_POST multiple values in PHP?
if (isset($_POST['ids']) && $_POST['ids'] === 'ford') {
echo "yah!!!";
print_r($_POST);
$filename = "ford.txt";
} elseif (isset($_POST['ids']) && $_POST['ids'] === 'toyota') {
print_r($_POST);
} elseif (isset($_POST['landrover']) && $_POST['ids'] === 'landrover') {
} elseif (isset($_POST['algonquin']) && $_POST['ids'] === 'algonquin') {
} elseif (isset($_POST['errors']) && $_POST['ids'] === 'errors') {
}
testing to see if $_POST superglobal array is posting.测试以查看 $_POST 超全局数组是否正在发布。 It is only posting one value 'ford'.
它只发布一个值“福特”。 Not sure why none of the other values aren't posting.
不知道为什么其他值都没有发布。
<form name="form" method="post" action="<?php echo $filename; ?>" target="box" >
<select name="ids">
<option value="ford">Ford</option>
<option value="toyota">Toyota</option>
<option value="landrover">Land Rover</option>
<option value="algonquin">Algonquin</option>
<option value="errors">Errors</option>
</select>
<input type="submit" name="submit" value="View File" />
</form>
This is the form with all of the options and their values.这是包含所有选项及其值的表单。 It has a variable $filename which links to a text file which is meant to be opened in a target box.
它有一个变量 $filename 链接到一个文本文件,该文件将在目标框中打开。 The if statement above is supposed to determine what file to open.
上面的 if 语句应该确定要打开的文件。
<iframe src="" name="box" width="100%" height="100%" frameborder="0"></iframe>
The iframe or target box the file is supposed to open in. <iframe src="" name="box" width="100%" height="100%" frameborder="0"></iframe>
文件应该在其中打开的 iframe 或目标框。
If you wish to POST
multiple values from your <SELECT>
, you will need to make a few modifications.如果您希望从
<SELECT>
POST
多个值,则需要进行一些修改。
Let's start with your form...让我们从你的表格开始......
<form name="form" method="post" action="selections.php" target="box" >
<select name="ids[]" multiple>
<option value="ford">Ford</option>
<option value="toyota">Toyota</option>
<option value="landrover">Land Rover</option>
<option value="algonquin">Algonquin</option>
<option value="errors">Errors</option>
</select>
<input type="submit" name="submit" value="View File" />
</form>
<iframe name="box" width="100%" height="100%" frameborder="0"></iframe>
.php
file, not a .txt
file..php
文件接收,而不是.txt
文件。 I'm not sure what you are trying to do with $filename
.$filename
做什么。ids[]
and write multiple
inside the <select>
.ids[]
并在<select>
写入multiple
。submit
value on your POST receiving file, then you don't need to write a name
attribute on <input type="submit">
submit
值,那么你不需要在<input type="submit">
上写一个name
属性On your submission receiving page (let's call it selections.php
), the `$_POST array can look like the following:在您的提交接收页面(我们称之为
selections.php
)上,`$_POST 数组可能如下所示:
If you selected Ford
, then:如果您选择了
Ford
,则:
$_POST=array('ids'=>array('ford'),'submit'=>'View File');
If you selected Toyota
and Errors
, then:如果您选择了
Toyota
和Errors
,则:
$_POST=array('ids'=>array('toyota','errors'),'submit'=>'View File');
If no selections are made, then :如果没有进行选择,则:
$_POST=array('submit'=>'View File');
To check this SUPERGLOBAL data, you can use the following:要检查此 SUPERGLOBAL 数据,您可以使用以下内容:
<?php
if(!isset($_POST['submit'])){
echo "No submission data";
}elseif($_POST['submit']=='View File'){
echo "Invalid/Unexpected submit data";
}elseif(empty($_POST['ids'])){
echo "No ids selected";
}else{
$valid_ids=['ford','toyota','landrover','algonquin','errors'];
$_POST['ids']=array_intersect($_POST['ids'],$valid_ids); // validate
foreach($_POST['ids'] as $id){
// do whatever you like with $id , perhaps $filename=$id.".txt" or something
}
}
This is presumably happening because you're not checking the $_POST['ids'] value in the following if conditions.这可能是因为您没有在以下 if 条件中检查 $_POST['ids'] 值。 I would wager that if you selected 'toyota' it would work.
我敢打赌,如果您选择“丰田”,它会起作用。
This is what your code should look like这就是你的代码应该是什么样子
if (isset($_POST['ids']) && $_POST['ids'] === 'ford') {
echo "yah!!!";
print_r($_POST);
$filename = "ford.txt";
} elseif (isset($_POST['ids']) && $_POST['ids'] === 'toyota') {
print_r($_POST);
} elseif (isset($_POST['ids']) && $_POST['ids'] === 'landrover') {
print_r($_POST);
} elseif (isset($_POST['ids']) && $_POST['ids'] === 'algonquin') {
print_r($_POST);
} elseif (isset($_POST['ids']) && $_POST['ids'] === 'errors') {
print_r($_POST);
}
Edit: as mentioned above, it seems like you're implying that the form should go to ford.txt?编辑:如上所述,您似乎暗示该表单应该转到 Ford.txt?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.