首先早上好。我正在处理一个搜索表单,它调用一个连接到 mysql 数据库的 php 来获取结果。
我的问题是我试图从我的 php 中获取结果...当我在 php 上搜索并调用该函数时,它会显示在另一个页面上...有没有办法知道如何从 php 获取数据到 javascript?
我的 html 搜索表单:
<div class="quicky-search pesquisaa-form">
<form id="pesquisaa" action="" method="POST" autocomplete="off">
<fieldset>
<input type="text" name="query" placeholder="Pesquisar"/>
<button type="submit" value="Search" class="submity-search"><span class="icon-search"></span></button>
</fieldset>
</form>
</div>
我在html中的脚本:
<script>
$(function(){
$('#pesquisaa').validate({
submitHandler: function(form) {
$(form).ajaxSubmit({
url: 'assets/email/pesquisar-cdd.php',
success: function() {
$('#pesquisaa').hide();
$('#pesquisaa-form').append(PS: I NEED THE RESULT INSIDE HERE TO SHOW ON MY HTML PAGE)
}
});
}
});
});
</script>
现在我的PHP:
<?php
mysql_connect("localhost", "root", "password") or die("Error connecting to database: ".mysql_error());
/*
localhost - it's location of the mysql server, usually localhost
root - your username
third is your password
if connection fails it will stop loading the page and display an error
*/
mysql_select_db("atuacdd") or die(mysql_error());
/* tutorial_search is the name of database we've created */
?>
<?php
$query = $_POST['query'];
// gets value sent over search form
$min_length = 3;
// you can set minimum length of the query if you want
if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to >
$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection
$raw_results = mysql_query("SELECT * FROM cidades
WHERE (`title` LIKE '%".$query."%') OR (`text` LIKE '%".$query."%')") or die(mysql_error());
// * means that it selects all fields, you can also write: `id`, `title`, `text`
// articles is the name of our table
// '%$query%' is what we're looking for, % means anything, for example if $query is Hello
// it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
// or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'
if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop
echo "<p><h3>".$results['title']."</h3>".$results['text']."</p>";
// posts results gotten from database(title and text) you can also show id ($results['id'])
}
}
else{ // if there is no matching rows do following
echo "Desculpe não atuamos nesta Cidade desejada!";
}
}
else{ // if query length is less than minimum
echo "Tamanho Minimo é ".$min_length;
}
?>