【问题标题】:Auto correct spelling errors in web application自动更正 Web 应用程序中的拼写错误
【发布时间】:2014-04-29 02:11:58
【问题描述】:

我开发 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; 
  } 

【问题讨论】:

    标签: php jquery mysql search autocorrect


    【解决方案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 或更复杂的东西

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2011-11-10
      • 1970-01-01
      • 2023-02-11
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-07
      • 2012-03-09
      相关资源
      最近更新 更多