我在网站上有这个表格。我无法从select标签中获取值,以便通过php上的变量收集。无论选择哪个选项,它都会在PHP中显示为空白。
其他输入值可以通过它的php变量准确收集。
我相信我已经检查了足够的错别字,未封闭的标签或缺少字符。它会是某种关于<的规则吗?选择>我不知道的标签?
HTML
<form method="post" action="contact.php" name="contactform" id="contactform" autocomplete="off">
<fieldset>
<label id="sendtolabel"><span>Send To:</span></label>
<select name="select" id="select">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>
<label for="name" accesskey="U"><span class="required">Name</span></label>
<input name="name" type="text" id="name" size="30" title="name" />
<label for="email" accesskey="E"><span class="required">Email</span></label>
<input name="email" type="text" id="email" size="30" title="email" />
<label for="comments" accesskey="C"><span class="required">Comments</span></label>
<textarea name="comments" cols="40" rows="3" id="comments" title="comments"></textarea>
<input type="submit" class="submit" id="submit" value="Submit" />
</fieldset>
</form>
PHP
$select = $_POST['select'];
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments']
答案 0 :(得分:3)
它在这里工作......你可以试试这个:
<?php
if ( isset( $_POST['select'] ) ) {
var_dump( $_POST['select'] );
$select = $_POST['select'];
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
echo "THE SELECT VAR's Value is: __".$select;
}
?>
<form method="post" action="" name="contactform" id="contactform" autocomplete="off">
<fieldset>
<label id="sendtolabel"><span>Send To:</span></label>
<select name="select" id="select">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>
<label for="name" accesskey="U"><span class="required">Name</span></label>
<input name="name" type="text" id="name" size="30" title="name" />
<label for="email" accesskey="E"><span class="required">Email</span></label>
<input name="email" type="text" id="email" size="30" title="email" />
<label for="comments" accesskey="C"><span class="required">Comments</span></label>
<textarea name="comments" cols="40" rows="3" id="comments" title="comments"></textarea>
<input type="submit" class="submit" id="submit" value="Submit" />
</fieldset>
</form>
注意:您可以在phpfiddle中测试上面的代码。
1)转到http://phpfiddle.org/
2)然后将代码粘贴到Editor
中 按运行[F9]
点击此处测试小提琴中的代码:http://phpfiddle.org/main/code/tuc-6jm
答案 1 :(得分:0)
当我遇到这样的问题时,我只打印所有$ _POST全局以查看已返回的内容
print_r($_POST);
而且没有..你的代码是正确的
<html>
<head></head>
<body>
<form method="post" action="test.php" name="contactform" id="contactform" autocomplete="off">
<fieldset>
<label id="sendtolabel"><span>Send To:</span></label>
<select name="select" id="select">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>
<label for="name" accesskey="U"><span class="required">Name</span></label>
<input name="name" type="text" id="name" size="30" title="name" />
<label for="email" accesskey="E"><span class="required">Email</span></label>
<input name="email" type="text" id="email" size="30" title="email" />
<label for="comments" accesskey="C"><span class="required">Comments</span></label>
<textarea name="comments" cols="40" rows="3" id="comments" title="comments"></textarea>
<input type="submit" class="submit" id="submit" value="Submit" />
</fieldset>
</form>
<?php
print_r( $_POST );
echo $_POST['select'];
$select = $_POST['select'];
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
echo $select;
?>
</body>
</html>
选择返回它必须返回的内容...(1,2,3,4)
答案 2 :(得分:0)
对我来说很好。
试试这种方式。将此代码放在contact.php之上,以测试是否所有值都正确检索。
<?php
if ( $_POST ) {
echo '<pre>';
print_r( $_POST );
die;
}
?>
请参阅下面的输出:http://d.pr/i/dgN3