Question

I develop web application i have search engine in it . I read the data from database . I built the search engine that can help to complete the word, but need to help to correct the spelling such as i write spelling this word is wrong i want to help and find the correct word such as do you mean spelling such as in Google search engine . help please I saw many links and can't find the right solution.

 <h2>Search</h2> 
  <form name="search" method="post" action="<?=$PHP_SELF?>">
  Seach for: <input type="text" name="find" /> in 
  <Select NAME="field">
  <Option VALUE="fname">diseasename</option>
  <Option VALUE="lname">genename</option>
  </Select>
  <input type="hidden" name="searching" value="yes" />
  <input type="submit" name="search" value="Search" />
  </form>
  </html>
  <?php 
  //This is only displayed if they have submitted the form 
  if ($searching =="yes") 
  { 
  echo "<h2>Results</h2><p>"; 

  //If they did not enter a search term we give them an error 
  if ($find == "") 
  { 
  echo "<p>You forgot to enter a search term"; 
  exit; 
  } 
Was it helpful?

Solution

You could use jQuery keypress in the input text that you want to spell check. Then using .ajax send the value from the input text to the server and check if the value received it's similar to anything that you have in your database. Here is a similar question regarding this last part.

So in general you do this in the html

<h2>Search</h2> 
<form name="search" method="post" action="<?=$PHP_SELF?>">
Seach for: <input type="text" name="find" id="find" /> in 
<Select NAME="field">
<Option VALUE="fname">diseasename</option>
<Option VALUE="lname">genename</option>
</Select>
<input type="hidden" name="searching" value="yes" />
<input type="submit" name="search" value="Search" />
</form>
</html>

<script>
  $( "#search" ).keypress(function(event) {
      var value = $(this).val();
      $.ajax({
           type:"POST" // or GET ,
           url: "search.php", // the url to the php file
           data: {value:value}, //send the value
           success:function( msg){
                //update your input text to indicate if you have an error
           },

      });
  });

<script>

And in the php you need to do something with this value. Depending what you want to do. If you want to show suggestion then it's something like the link I put before (this). But if you only want to check if the word exists then it's only something like this:

 <?php
  //some code
  $value = $_POST['value'];
  //connect to the data base and make stuff to prevent sql injection      
  $sql = "SELECT name FROM myTable WHERE name = '".$value."'"; //this is just an example
  ?>

Of course this is an extremely simple example. You could use LIKE operator or more complex stuff

Hope it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top