How to get HTML form for querying and retrieving results from a Mysql database

I created a form in which there is one text box and one check box, I also created a button. Now I want this to happen when the button is pressed. I want the string in the forms to be matched to my database, and then I want the results of these matches to be returned and displayed on the screen.

For example, if I were to enter “History” in the text field, and I also selected “level 2” from my selection window when the button was pressed (the “Send” button), I would like to return everything that matches the page with word history and level 2 choices in my database.

I know that I need to connect my page to my database via PHP, and I did it successfully, but what I don't understand is how then to get my HTML form to query the database and return the results to the screen

For an example web site that is very similar to the concept I would like to create, take a look at this web page. http://search.ucas.com/cgi-bin/hsrun/search/search/search.hjx;start=search.HsSearch.run?y=2013&w=H (search for the UCAS course) on this website there are several text fields and selection fields and the submit button is exactly the same as I'm trying to create, and the results are provided only with results that match the search results.

The code below works in terms of linking my text box to my database, but I cannot get the text box and the select box to link and query the database, only one or the other. I want them both to work with the same button (search button)

<form  method="post" action="Webpage here"  id="searchform">
  <input  type="text" name="name">
  <input  type="submit" name="submit" value="Search">
</form>
<form id="form1" name="ExamBoard" method="post" action="Webpage here">
  <label for="select"></label>
  <select name="ExamBoard" id="select">
    <option value="EB1" selected="selected">EB1</option>
    <option value="EB2">EB2</option>
    <option value="EB3">EB3</option>
  </select>
  <input  type="submit" name="submit" value="Search">
</form>
<p>&nbsp;</p>
<p>
<?php
 if(isset($_POST['submit'])){
 if(isset($_GET['go'])){
 if(preg_match("/^[  a-zA-Z]+/", $_POST['name'])){
 $name=$_POST['name'];
  //connect  to the database
 $db=mysql_connect  ("Name", "User",  "Password*") or die ('I cannot connect to the      database  because: ' . mysql_error());
 //-select  the database to use
 $mydb=mysql_select_db("Table Name");
  //-query  the database table
  $sql="SELECT  ID, CourseName, ExamBoard FROM subjects WHERE CourseName LIKE '%" . $name .  "%' ";
   //-run  the query against the mysql query function
   $result=mysql_query($sql);
    //-create  while loop and loop through result set
    while($row=mysql_fetch_array($result)){
      $CourseName  =$row['CourseName'];
      $ID=$row['ID'];
      $ExamBoard=$row['ExamBoard'];
  //-display the result of the array
  echo "<ul>\n";
  echo "<li>" . "<a  href=\"search.php?id=$ID\">"   .$CourseName . " " .  "</a></li>\n" ;
  echo  $ExamBoard . " " .  "</a>\n";
  echo "</ul>";
  }
  }
  }
  }
  ?>
+5
source share
2 answers

New HTML file

newHTML.htm

<form  method="post" action="Webpage here"  id="searchform">
  <input  type="text" name="name">
  <input  type="submit" name="submit" value="Search">
</form

New PHP file

newPHP.pfp

<?php
 if(preg_match("/^[  a-zA-Z]+/", $_REQUEST['name'])){
 $name=$_REQUEST['name'];
  //connect  to the database
 $db=mysql_connect  ("Name", "User",  "Password*") or die ('I cannot connect to the      database  because: ' . mysql_error());
 //-select  the database to use
 $mydb=mysql_select_db("Table Name");
  //-query  the database table
  $sql="SELECT  ID, CourseName, ExamBoard FROM subjects WHERE CourseName LIKE '%" . $name .  "%' ";
   //-run  the query against the mysql query function
   $result=mysql_query($sql);
    //-create  while loop and loop through result set
    while($row=mysql_fetch_array($result)){
      $CourseName  =$row['CourseName'];
      $ID=$row['ID'];
      $ExamBoard=$row['ExamBoard'];
  //-display the result of the array
  echo "<ul>\n";
  echo "<li>" . "<a  href=\"search.php?id=$ID\">"   .$CourseName . " " .  "</a></li>\n" ;
  echo  $ExamBoard . " " .  "</a>\n";
  echo "</ul>";
}
?>
+1
source

You must ask yourself. Do you want to do this with AJAX or with a normal screen refresh.

With normal screen refresh (easy way)

Take your HTML and put it in one file (HTMLfile.html) Take your PHP and put it in another file (PHPCode.php)

The HTML file form calls the PHP script from the action attribute.

PHP file displays the response.

.

script, AJAX.

0

All Articles