我对PHP有点新意,我在这里和那里遇到了一些困难。我正在开发一个表单,并希望在按下'提交' 按钮时,如果任何字段为空,则显示错误框。我尝试过以下代码,但回声仍然没有出现。有什么建议吗?
表格代码:
<div style="padding-top:40px">
<div style="text-center; padding-right:25%; padding-left:25%">
<div class="form-area">
<form role="form" method="$_POST" action="searchEmployee.php">
<br style="clear:both">
<h3 style="margin-bottom:25px; text-align: center;">Visitor Form</h3>
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required>
</div>
<div class="form-group">
<label>Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required>
</div>
<div class="form-group">
<label>ID Card:</label>
<input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
</div>
<div class="form-group">
<label>Visitor Card Number:</label>
<input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
</div>
<div class="form-group">
<intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
</div>
</form>
</div>
</div>
</div>
PHP代码:
<?php
if (isset($_POST['submit'])) {
$required = array('name', 'surname', 'ID', 'visitorCard');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo "All fields are required.";
}
}
?>
答案 0 :(得分:3)
您在表单属性中使用方法$_POST
,
它应该只有POST
。
因此,请用
替换您的表单行<form role="form" method="POST" action="searchEmployee.php">
并且还将提交按钮行更改为,
<input type="submit" name="submit" value="Sign In Visitor" class="btn btn-primary pull-right" />
答案 1 :(得分:2)
以下是一些错误:
$_POST
,它应该只是POST
。您的表单不会提交,因为您的按钮不是提交类型。它应该
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor" />
或者
<button type="submit" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
答案 2 :(得分:0)
更改此
<intput type="button" id="submit" name="submit" class="btn btn-primary pull-right">Sign In Visitor</button>
要
<input type="button" id="submit" name="submit" value="submit" class="btn btn-primary pull-right" />Sign In Visitor
还有 这个
<form role="form" method="$_POST" action="searchEmployee.php">
要
<form role="form" method="POST" action="searchEmployee.php">
如果你甚至没有设置,你将如何得到POST['submit']
值,这意味着你的if代码不会执行它,所以它不会回应或提醒它。
对于调试的最佳实践,只要在编码时发生此类实例,就始终尝试执行此操作。
print_r($_POST);
这将显示一系列POST变量
答案 3 :(得分:0)
您需要进行2次更改
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor">
<form role="form" method="POST" action="searchEmployee.php">
答案 4 :(得分:0)
将$_POST
更改为POST
并将<input type="submit"
代替<input type = "button"
答案 5 :(得分:0)
首先考虑更改按钮type="submit"
。
第二个想改变PHP数组
$required = array('name', 'surname', 'idCard', 'cardNumber');
答案 6 :(得分:0)
var countries= document.getElementById("countries").getContext("2d");
var pieData =
{
labels: ['India', 'Germany', 'Netherland'],
datasets: [{
backgroundColor: '#337ab7',
data: ['100', '99' ,'98'],
borderWidth:1,
}]
};
var pieOptions =
{
responsive: true,
maintainAspectRatio:false,
legend: {
display: false,
position: 'top'
}
};
var chart = new Chart(countries,{
type: 'pie',
data: pieData,
options: pieOptions
});
Php Code ::: searchEmployee.php
<div style="padding-top:40px">
<div style="text-center; padding-right:25%; padding-left:25%">
<div class="form-area">
<form role="form" method="post" action="searchEmployee.php"> <br style="clear:both"> <h3 style="margin-bottom:25px; text-align: center;">Visitor Form</h3> <div class="form-group"> <label>Name:</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" required> </div>
<div class="form-group"> <label>Surname:</label>
<input type="text" class="form-control" id="surname" name="surname" placeholder="Surname" required> </div> <div class="form-group"> <label>ID Card:</label>
<input type="text" class="form-control" id="idCard" name="idCard" placeholder="ID Card No" required>
</div>
<div class="form-group">
<label>Visitor Card Number:</label> <input type="text" class="form-control" id="cardNumber" name="cardNumber" placeholder="Card No" required>
</div> <div class="form-group">
<input type="submit" id="submit" name="submit" class="btn btn-primary pull-right" value="Sign In Visitor">
</div> </form> </div>
</div> </div>