gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 00:17:11 25 4
gpt4 key购买 nike

我开发网络应用程序,其中有搜索引擎。我从数据库中读取数据。我构建了可以帮助完成单词的搜索引擎,但需要帮助纠正拼写,例如我写的拼写错误,我想帮助找到正确的单词,例如您的意思是拼写吗,例如在 Google 搜索中引擎 。请帮助我看到了很多链接,但找不到正确的解决方案。

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

最佳答案

您可以使用 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或更复杂的东西

希望对你有帮助

关于php - 自动更正 Web 应用程序中的拼写错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22563355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com