自动更正Web应用程序中的拼写错误

时间:2014-03-21 15:48:36

标签: php jquery mysql search autocorrect

我开发Web应用程序我有搜索引擎。我从数据库中读取数据。我建立了可以帮助完成单词的搜索引擎,但需要帮助纠正拼写,例如我写拼写这个单词是错的我想帮助并找到正确的单词,比如你是说拼写如谷歌搜索发动机。请帮助我看到很多链接,找不到合适的解决方案。

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

1 个答案:

答案 0 :(得分:1)

您可以在要拼写检查的输入文本中使用jQuery keypress。然后使用.ajax将输入文本中的值发送到服务器,并检查接收到的值是否与数据库中的任何值类似。关于最后一部分,这里有一个类似的question

所以一般来说你在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>

在php中你需要用这个值做点什么。根据你的想法。如果你想显示建议,那就像我在(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
  ?>

当然这是一个非常简单的例子。您可以使用LIKE operator或更复杂的内容

希望有所帮助

相关问题