我有一个表单,其中包含一个搜索记录的字段,并根据搜索结果完成表单的其余字段。
搜索是通过搜索按钮的操作获得的方法。
在这种情况下,填写表单字段,并收集有关locador 106000的数据的数据。
http://localhost/sfi/index.php?number_locator=106000&search-locator=
我的问题是,当我在没有进行任何搜索的情况下访问主页时,所有其他字段都会消失。
这是因为它们属于foreach?
如果我离开foreach而没有关闭,表单会在字段中返回错误。
<?php foreach ($new_array as $dataForm) ?>
<?php
if (isset($_GET['search-locator'])){
// Connect to DB
$conex = mysql_connect("localhost","root","");
// Select DB
$selectDB = mysql_select_db("payment_locations",$conex);
// Check that the search field is not empty
if (empty($_GET['number_locator'])) {
$message = "El campo n° de locador está vacio";
echo "<script type='text/javascript'>
alert('$message');
</script>";
}
// Start search
else {
// Save field content
$search = $_GET['number_locator'];
// Query
$sql = "SELECT * FROM without_retention WHERE id_locator like '$search'";
// Execute query
$result = mysql_query($sql);
if (mysql_num_rows($result) < 1) {
echo "No hay resultados";
}
else {
while ($row = mysql_fetch_array($result)){
$new_array[] = $row;
}
}
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="get">
<?php foreach ($new_array as $dataForm): ?>
<div class="row col-centered-form-input">
<!--
<div class="col-md-12">
<div class="form-group">
<label class="form-font" for="inputEmail">Lugar y fecha de pago:</label>
<input type="text" id="inputDate" class="form-control input-borders form-font input-size" value="<?php echo $fecha; ?>">
</div>
</div>
-->
<div class="col-md-12">
<div class="form-group">
<label class="form-font input-size" for="inputEmail">Lugar y fecha de pago:</label>
<div class="input-group">
<input id="date-picker-2" type="text" class="date-picker form-control input-borders" />
<label for="date-picker-2" class="input-borders input-group-addon btn">
<span class="glyphicon glyphicon-calendar"></span>
</label>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="form-font" for="inputEmail">Recibí de Publicartel la suma de:</label>
<input type="text" class="form-control input-borders input-size" maxlength="15" value="<?php echo $dataForm['payment']; ?>" disabled>
</div>
</div>
</div>
<?php endforeach; ?>
<div class="form-group">
<div class="row col-centered-form-input">
<div class="col-xs-6">
</div>
<div class="col-xs-6">
<label class="form-font input-medium-size-right" for="inputEmail">Firma:</label>
<input class="form-control input-medium-size-right input-borders" type="text">
</div>
</div>
</div>
我没有把所有字段放在逻辑上,不填写代码主题:)
答案 0 :(得分:0)
可以这样吗?您可以检查数据,如果数据为空,则显示默认表单。
<?php
if(!empty($new_array)){
foreach ($new_array as $dataForm){
//that your form elements
}
}else{
// here the default form elements
}
?>
答案 1 :(得分:0)
在字段中应用的解决方案。
<input type="text" class="form-control input-borders input-medium-size" value="<?php echo isset($dataForm['period_to']) ? $dataForm['period_to'] : ''; ?>" disabled>