我怎样才能 AJAX 化这个脚本

发布于 2024-10-20 11:03:11 字数 677 浏览 1 评论 0原文


我使用这个脚本将链接提交到文本文件中。唯一的问题是,我需要表单在同一页面上动态发布。关于如何做到这一点有什么想法吗?我正在使用的:
发布:

<form method="POST" action="addlink.php">
Link: <input type="text name="link">
<input type="submit" name="submit">
</form>

addlink.php:

<?
$link = $_POST['link']; 
$fn = 'textfile.txt'; 
$fp = fopen($fn, "a+");
$write = fputs($fp, $link."<br>");
fclose($fp);
?>

调用:

<?
$fn = 'textfile.txt';
$fp = fopen($fn, "r+");
$info = fread($fp,filesize($fn));
fclose($fp);
echo $info;
?>

还有关于如何让脚本等待发布链接以便我可以批准/删除它们的任何想法?我正在考虑 MySQL,但我不太擅长编写数据库。我还有 jQuery 库。

I use this script which submits links into a text file. The only problem is, I need the form to post dynamically on the same page. Any ideas on how this can be done? What I'm using:
To post:

<form method="POST" action="addlink.php">
Link: <input type="text name="link">
<input type="submit" name="submit">
</form>

addlink.php:

<?
$link = $_POST['link']; 
$fn = 'textfile.txt'; 
$fp = fopen($fn, "a+");
$write = fputs($fp, $link."<br>");
fclose($fp);
?>

The call:

<?
$fn = 'textfile.txt';
$fp = fopen($fn, "r+");
$info = fread($fp,filesize($fn));
fclose($fp);
echo $info;
?>

Also any ideas on how I could make the script wait to publish the links so I can approve/delete them? I was thinking about MySQL but I'm not very great at writing DB's. I also have the jQuery lib.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2024-10-27 11:03:11

查看 jQuery 的 post() API: http://api.jquery.com/jQuery.post/

jQuery 使得在表单提交上使用 AJAX 变得相当简单。我认为您只需做您现在正在做的事情,但在幕后使用 AJAX,然后,如果成功,只需发回一些文本,让您知道它有效。

至于链接的批准/拒绝,我只会像你想的那样,设置一个至少包含以下字段的 MySQL 数据库表:id、link、approved。让我们确切地知道您还需要哪些方面的帮助。如果您有更具体的问题。

Check out jQuery's post() API: http://api.jquery.com/jQuery.post/

jQuery makes it fairly straightforward to use AJAX on a form submission. I think you simply do exactly what you're doing now, but behind the scenes with AJAX, then, if it is successful, just send back some text that will let you know that it worked.

As far as the approval/denial of links goes, I would just, as you were thinking, set up a MySQL database table with at least the following fields: id, link, approved. Let us know exactly what else you need help with. If there are more specific questions that you have.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文