如何将其转换为 AJAX 函数
目前我有一个表单按钮,单击该按钮会重新加载页面并发布数据,然后运行 mySQL 命令,这可以找到,但我需要立即显示数据而不是再次刷新,我可以在查询后重新加载页面,但是这会耗尽我的资源。我尝试阅读 AJAX,但我无法理解它(或无法让它工作!!)
任何人都可以告诉我如何解决这个问题吗?我当前的代码是...
elseif(isset($_POST['int1'])) {
$interest = $_POST['int1'];
$qResult= mysql_query("DELETE from user_interests WHERE interest = '" . mysql_real_escape_string($interest) . "' and user_id = " . $usersClass->userID());
if(! $qResult )
{
die('Could not delete data: ' . mysql_error());
}
}
if($interest1 != ''){
print $interest1 . "<form method='post' action='#'><input type='hidden' value='".$interest1."' name='int1' id='int1'/><input type='submit' value='delete' /></form><br />";
}
At the moment I have a form button which when clicked reloads the page and posts data which then runs a mySQL command, this works find but I need to show the data right away instead of refreshing again, I could reload the page after the query but that then drains my resources. Ive tried reading up on AJAX but I cant understand it (or get it to work!!)
Can anybody show me how to go about this? My current code is...
elseif(isset($_POST['int1'])) {
$interest = $_POST['int1'];
$qResult= mysql_query("DELETE from user_interests WHERE interest = '" . mysql_real_escape_string($interest) . "' and user_id = " . $usersClass->userID());
if(! $qResult )
{
die('Could not delete data: ' . mysql_error());
}
}
if($interest1 != ''){
print $interest1 . "<form method='post' action='#'><input type='hidden' value='".$interest1."' name='int1' id='int1'/><input type='submit' value='delete' /></form><br />";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您刚刚开始使用 AJAX,我建议您使用 jQuery —— 它将使您的一切都能跨浏览器工作。
然后,您的表单信息(数据)将位于您放入 PHP 的文件中名为
$_GET['some_info']
的变量中。echo
输出您想要返回的内容到文档,它将出现在您想要信息所在位置的 ID 中(请参阅代码中的名称。)I would suggest using jQuery if you're just getting into AJAX -- it will make everything work cross-browser for you.
Then your form info (data) will be in a variable called
$_GET['some_info']
in the file you put your PHP in.echo
out what you want to return to the document, and it will appear in the ID of where you want info to go (see names in code.)尝试浏览此页面 http://roshanbh.com。 np/2008/04/ajax-login-validation-php-jquery.html ...我认为这正是您现在所需要的。如果您有任何困惑,请随时在这里回复。
Try going through this page http://roshanbh.com.np/2008/04/ajax-login-validation-php-jquery.html ... i think this is precisely what you need now. If you have any confusion feel free to reply here.
首先:了解如何在网页中实现 jQuery。
然后你可以使用 jQuery 框架来处理你的 ajax 查询。
首先,创建一个如下所示的代码块:
从那里创建一个 Ajax 函数:
jQuery 在其网站上有很多文档,因此您应该轻松了解如何使用它。
First: Read up on how to implement jQuery into your webpage.
Then you can use the jQuery framework to handle your ajax query.
First, create a block of code like this:
From there, create an Ajax function:
jQuery has a lot of documentation on their website so you should easily figure out how to use it.
首先,使用 jQuery 库 http://www.jquery.com!它极大地简化了 JavaScript 的使用。然后给你的
引用 jQuery 库(最好将其放在 html 的最底部,标签的正上方),
然后在其后面添加一段像这样的 jQuery 代码(您可能需要更改它以满足您的需求,但我尝试非常接近)
您可以使用 AJAX 创建一些非常时髦的桌面感觉网页,所以享受了解它的乐趣。我希望这有帮助!
First of all, use the jQuery library http://www.jquery.com! It greatly simplifies working with javascript. Then give your
Reference the jQuery library (best to put it at the very bottom of your html, right above the tag)
Then follow it with a snippet of jQuery code like this (you will probably need to alter it to suit your needs, but I tried to get pretty close)
You can create some really snazzy desktop-feeling web pages with AJAX, so enjoy getting to know it. I hope this helps!