galmat
galmat

Reputation: 13

clear form after submission

when I submit the form below, it search works the first time, but if I try another search, it gives me the same answer, example: First I search "operat"---> it returns for example "operational suffix" Then if I search "flight" ---> it returns "operational suffix" again Below you will find the code, Thank you in advance!

<form name="form1" id="form1" class="form-inline" method="GET" action="index.php">
<select name="select">
<option value="" selected="selected"></option>    
<option value="Data Element Name">Data Element Name</option>    
<option value="Term">Term</option>    
<option value="Diagram">Diagram</option>  
</select>
<input type="text" name="search" id="search">
<input type="submit" class="btn">
</form>
<table id="resultTable"></table> 

<?php
function connection(){
/*****************************************************************************/
//Get values from the form
$search = $_GET['search'];
$select = $_GET['select'];
/*****************************************************************************/
//Define needed values to log in
$dbhost='localhost';
$dbusername='username';
$dbuserpass='pass';
$dbname = 'db_name';
/*****************************************************************************/
// connect to the mysql database server
$connection=mysql_connect ($dbhost, $dbusername, $dbuserpass);
/*****************************************************************************/
//select the database
mysql_select_db($dbname) or die('Cannot select database');
/*****************************************************************************/
//Create the Query
$result="SELECT name from tbl_name WHERE stereotype LIKE 'Term' AND name LIKE '%$search%' ";
/*****************************************************************************/
//Sending the request
$req =mysql_query($result,$connection);
if (!$req )
{
die(mysql_error());
}
/*****************************************************************************/
//Loop to stock results on an array 
$array=array();
$i=0;
while($data = mysql_fetch_assoc($req))
{ 
$array[$i]=$data['name'];
$i=$i+1;
}
/*****************************************************************************/
//return the result to javascript
return json_encode($array);
/*****************************************************************************/
}
?>
<script>
$('#form1').submit(function() {  
$('#resultTable').empty();
alert(document.forms["form1"]["search"].value);
if (document.forms["form1"]["select"].value=="Term" ){
var tab=<?php echo connection();?>;
for(i=0;i<tab.length;i++) {$('#resultTable').append('<tr><td>'+tab[i]+'</td></tr>');}
}
return false;
});
</script>

Upvotes: 0

Views: 496

Answers (1)

Chris Visser
Chris Visser

Reputation: 1647

It's because the html and php are in the wrong order. Normal flow:

  1. Set variables
  2. Create db connection
  3. Insert / update stuff in db
  4. Select stuff from db
  5. Display stuff

In your case, the stuff is inserted after your form is rendered

If it's not your case, try checking if you use javascript or offline storage stuff.

Upvotes: 1

Related Questions