How to get a HTML Form to query and produce results from Mysql datebase How to get a HTML Form to query and produce results from Mysql datebase database database

How to get a HTML Form to query and produce results from Mysql datebase


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>";}?>


You have to ask yourself Do you want to do this with AJAX or with normal screen refreshes..

With normal screen refreshes (The easy way)

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

HTML file form calls the PHP script from action attribute.

PHP file renders the response.

Dont mash up the view and the controlling application.

There is other ways to do this in one script if you want to use AJAX.