我会尽量简明扼要。
我有一个PHP表单页面,它用数组中的数据(杂志名)填充下拉框,然后,如果发布,就将它输出到另一个页面。
但是,我在表单中有第二个数组(包含订阅链接),我也需要发送到输出页面,这取决于从下拉框中选择了什么。如果这有意义的话。
例如,如果从下拉框中选择了用于$arrayOne[7]的值,则还需要将$arrayTwo[7]的值自动转移到输出页。
有人能帮忙吗?
default.php
$arrayOne[0] = "magazineA";
$arrayOne[1] = "magazineB";
etc...
$arrayTwo[0] = "subscriptionLinkA";
$arrayTwo[1] = "subscriptionLinkB";
etc...
<form action="index2.php" method="get">
<?php
echo '<select name="publication">';
foreach ($magazine as $publication)
{
echo '<option name="publication" value="' . $publication . '">' . $publication . '</option>';
}
echo '</select>';
?>
<input type="submit">
</form>index2.php
<p>
If you have subscribed to <?php echo $_GET["publication"]; ?>,
you will receive your hard-copy in due course. If you are not a current subscriber,
<a href="#">click here</a> to subscribe.
</p>发布于 2012-12-11 12:06:04
因为来自$array2的值完全依赖于$array1的值,所以$_GET['a1']可以充当另一个值的查找。你根本不需要通过$a2考试。
在index2.php上,您可以看到如下内容:
$arr2= array( 'magazineA' => 'subscriptionLinkA',
'magazineB' => 'subscriptionLinkB');
$pub= $_GET['a1']; //you'd want to sanitize this value of course
$link= $arr2[$pub];
echo <<< EOT
<p>
If you have subscribed to $pub
you will receive your hard-copy in due course. If you are not a current subscriber,
<a href="$link">click here</a> to subscribe.
</p>
EOT;发布于 2012-12-11 11:40:36
方法1
您可以使用Javascript来使用<input type="hidden"...字段的选中选项填充隐藏输入字段( <select> ),此时<select>字段中的下拉选择被更改。
default.php的一些代码
<form action="index2.php" method="get">
<?php
echo '<select id="pubfield" name="publication">';
foreach ($magazine as $publication) {
echo '<option name="publication" value="'.$publication.'">'.$publication.'</option>';
}
echo '</select>';
?>
<input type="submit" value="SEND!">
<input type="hidden" id="hid" name="arr2" value="" />
<script type="text/javascript">
// Using jQuery, include the js file with jQuery.
$('select#pubfield').change(function() {
var value = $("select#pubfield option:selected").text(); // Get the selected option from the select field.
$('input#hid').value(value);
});
$('select#pubfield').change();
</script>
</form>注意到:因为Javascript是客户端的,所以我建议您不要信任HTML发送的值。始终对其执行服务器端检查。
更新:
方法2
还可以将索引值设置为<option value="">,并将其与表单一起发送。
$magazines = array(
"magazineA", // will be index 0
"magazineB", // will be index 1
...
);
...
foreach ($i = 0; $i < count($magazines); $i++) {
echo '<option value="'.$i. '">'.$magazines[$i].'</option>';
// The 'value' attribute of the selected option will be posted.
}
...然后,您甚至不需要隐藏的输入字段,但是索引将传递给$_POST变量。
例如,如果选择“magazineB”,则1值将传递给$_POST['publication']。
https://stackoverflow.com/questions/13818688
复制相似问题