I have a form that has a field to search records, and from the results, complete the remaining fields of the form with the search results.
The search is performed by the method get from the action of a search button.
In this case the form fields are completed with data collected on the number of locador 106000.
http://localhost/sfi/index.php?number_locator=106000&search-locator=
My problem is that when I access the home page without having made any search, all other fields disappear.
This is because they are within the foreach?
If I leave the foreach without closing, the form returns errors in the fields.
<?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>
I have not put all fields for a logical reason not to fill the topic of code :)
Can be like this? You can check the data and if the data is empty show default form.
<?php
if(!empty($new_array)){
foreach ($new_array as $dataForm){
//that your form elements
}
}else{
// here the default form elements
}
?>
A solution to apply in the fields.
<input type="text" class="form-control input-borders input-medium-size" value="<?php echo isset($dataForm['period_to']) ? $dataForm['period_to'] : ''; ?>" disabled>