I am having some issues returning data from on page, using jQuery, PHP and MySQL. I would like to show the query results on the index.php page. The results are fetched from the database using getResults.php.
我在使用jQuery,PHP和MySQL从页面返回数据时遇到了一些问题。我想在index.php页面上显示查询结果。使用getResults.php从数据库中获取结果。
When I manually run the getResults.php?id=123 file, all works fine. In this case i'm seeing the results I would like to. However, when I want to post the 'id' 123 in a submit form on index.php, I don't see any results returned by jQuery / getResults.php. Only thing that changed is the URL: index.php?id=123. However, I'm not seeing any results or an error message...
当我手动运行getResults.php?id = 123文件时,一切正常。在这种情况下,我看到了我想要的结果。但是,当我想在index.php上的提交表单中发布'id'123时,我看不到jQuery / getResults.php返回的任何结果。唯一改变的是URL:index.php?id = 123。但是,我没有看到任何结果或错误消息......
Any one an idea?
任何一个想法?
getResults.php file
$search = mysql_real_escape_string( isset ($_POST['id']));
if ($search) {
$friend = mysql_query( " SELECT * FROM reviews WHERE fbuserid = '$search' ORDER BY datumtijd DESC" );
if ( $friend ) {
while ($row = mysql_fetch_array ($friend) ) {
echo "Show some results...";
}
} else {
echo "No matches found";
}
} else {
echo "Query not succesfull";
}
index.php file
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
$("#submit").click(function) {
event.preventDefault();
$.ajax({
url:"getResults.php",
type:"GET",
data: "id="+this.value,
success: function(data) {
$("#results").html(data);
}
});
}
return false;
});
</script>
<div>
<form>
<input type="text" name="id">
<input type="submit" value="submit" id="submit">
</form>
</div>
<div id="results"></div>
EDIT: Thanks, all for your input. Nevertheless, I'm still not there... As you might notice, I'm not quite experienced with jQuery and PHP. See changes in scripts above. In short: - I added the false statement if ($query) is not succesfull; - Changed the $.post method into $.ajax; - Changed the GET into POST in the PHP file;
编辑:谢谢,所有的输入。尽管如此,我仍然不在那里......你可能会注意到,我对jQuery和PHP并不熟悉。请参阅上面脚本中的更改。简而言之: - 如果($ query)不成功,我添加了false语句; - 将$ .post方法更改为$ .ajax; - 在PHP文件中将GET更改为POST;
Any other suggestions?
还有其他建议吗?
4 个解决方案
#1
1
You are POSTing data but your script is looking for it in GET.
您正在发布数据,但您的脚本正在GET中查找它。
Edit in response to massive rewrite of the code in the question:
编辑以响应问题中代码的大量重写:
- The first argument of
$.ajax
should be the URL. - You are missing the
{
from your settings object. - You are using a
=
instead of:
for yourtype
parameter. - You aren't doing anything to stop the normal submission of the form, so the form gets submitted and a new page loaded before the HTTP request sent by the Ajax returns.
$ .ajax的第一个参数应该是URL。
您缺少{来自您的设置对象。
您使用a =而不是:作为您的类型参数。
您没有做任何事情来阻止表单的正常提交,因此表单将被提交并在Ajax发送的HTTP请求返回之前加载新页面。
#2
1
In the first line:
在第一行:
$search = mysql_real_escape_string( isset ($_POST['id']));
Try changing $_POST
to $_GET
:
尝试将$ _POST更改为$ _GET:
$search = mysql_real_escape_string( isset ($_GET['id']));
^^^^^
#3
0
Its much better to use the jQuery ajax method to implement the ajax functionality in the script. As it is more generalized rather than specific to one METHOD. Below is the sample code to use the ajax method.
使用jQuery ajax方法在脚本中实现ajax功能要好得多。因为它更普遍而不是特定于一种方法。下面是使用ajax方法的示例代码。
$(document).ready(function()
{
$("#submit").click(function)
{
$.ajax(
type="GET",
url:"getResults.php",
data: "id="+this.value,
success: function(msg)
{
$("#results").html(msg);
}
);
}
});
To check out more examples please refer to the JQUERY API Reference.
要查看更多示例,请参阅JQUERY API参考。
J
#4
0
in if ($query) {
is $query !== false ???
in if($ query){is $ query!== false ???
#1
1
You are POSTing data but your script is looking for it in GET.
您正在发布数据,但您的脚本正在GET中查找它。
Edit in response to massive rewrite of the code in the question:
编辑以响应问题中代码的大量重写:
- The first argument of
$.ajax
should be the URL. - You are missing the
{
from your settings object. - You are using a
=
instead of:
for yourtype
parameter. - You aren't doing anything to stop the normal submission of the form, so the form gets submitted and a new page loaded before the HTTP request sent by the Ajax returns.
$ .ajax的第一个参数应该是URL。
您缺少{来自您的设置对象。
您使用a =而不是:作为您的类型参数。
您没有做任何事情来阻止表单的正常提交,因此表单将被提交并在Ajax发送的HTTP请求返回之前加载新页面。
#2
1
In the first line:
在第一行:
$search = mysql_real_escape_string( isset ($_POST['id']));
Try changing $_POST
to $_GET
:
尝试将$ _POST更改为$ _GET:
$search = mysql_real_escape_string( isset ($_GET['id']));
^^^^^
#3
0
Its much better to use the jQuery ajax method to implement the ajax functionality in the script. As it is more generalized rather than specific to one METHOD. Below is the sample code to use the ajax method.
使用jQuery ajax方法在脚本中实现ajax功能要好得多。因为它更普遍而不是特定于一种方法。下面是使用ajax方法的示例代码。
$(document).ready(function()
{
$("#submit").click(function)
{
$.ajax(
type="GET",
url:"getResults.php",
data: "id="+this.value,
success: function(msg)
{
$("#results").html(msg);
}
);
}
});
To check out more examples please refer to the JQUERY API Reference.
要查看更多示例,请参阅JQUERY API参考。
J
#4
0
in if ($query) {
is $query !== false ???
in if($ query){is $ query!== false ???