从 PHP 文件中获取 $_session 变量并在 AJAX 中使用

发布于 2024-12-12 06:11:50 字数 1561 浏览 3 评论 0原文

在使用脚本语言方面我还很陌生,所以我很难理解这些概念。

基本上我有这个表单

<form method="post" action="/list/process.php">
 <input type="email" name="address" value ="Email" />
 <input type="hidden" name="lists[]" value="122" />
 <input type="submit" name="submit" value="Join" />
</form>

process.php 验证电子邮件地址的语法是否正确,如果成功则根据数据库检查地址以查看它是否已经存在(“取消订阅”)或不存在(“订阅”)

我将这段 PHP 代码片段包含在表单的页面

<div id="result">
<?php
   if ($_SESSION['result'] == ("fail"))
    {
    echo "<p>Please enter a valid email address!</p>";
    }

   if ($_SESSION['result'] == ("success") && $_SESSION['action'] == ("subscribe"))
    {
    echo "<p>Thankyou for joining, you will shortly receive an email at $_SESSION[address]' with a link to your free download.</p><p>Note: if you do not receive an email, please check your junk folder and mark the message as safe.</p>";
    }

   if ($_SESSION['result'] == ("success") && $_SESSION['action'] == ("unsubscribe"))
    {
    echo "<p>You will shortly receive an email with a link to unsubscribe from the mailing list.</p>";
    }
?>
</div>

不确定我的 PHP 语法是否符合标准(我是新手),但它可以工作。当然 div#result 显示为“请输入有效的电子邮件地址!”当您最初加载页面时,因为 $_SESSION['result'] 返回“失败”。当我提交表格时,所有其他可能性都有效。

现在我该如何在 AJAX 中复制这个 PHP 片段?我想这将涉及使用 AJAX 将表单发布到 process.php,取回三个会话变量,然后以上述方式(但在 JavaScript 中)处理它们,以在最初隐藏的 div 中显示三个消息之一(#结果)以弹出/气泡方式。

问题是如何?我没有使用 AJAX 的经验,这似乎是我实现预期结果的唯一方法。

感谢您的帮助。

亚当

I'm fairly new when it comes to using scripting languages so I'm struggling to get my head round the concepts.

Basically I have this form

<form method="post" action="/list/process.php">
 <input type="email" name="address" value ="Email" />
 <input type="hidden" name="lists[]" value="122" />
 <input type="submit" name="submit" value="Join" />
</form>

process.php validates the email address for the correct syntax and if successful checks the address against a database to see if it already exists ("unsubscribe") or not ("subscribe")

I included this snippet of PHP to the page of the form

<div id="result">
<?php
   if ($_SESSION['result'] == ("fail"))
    {
    echo "<p>Please enter a valid email address!</p>";
    }

   if ($_SESSION['result'] == ("success") && $_SESSION['action'] == ("subscribe"))
    {
    echo "<p>Thankyou for joining, you will shortly receive an email at $_SESSION[address]' with a link to your free download.</p><p>Note: if you do not receive an email, please check your junk folder and mark the message as safe.</p>";
    }

   if ($_SESSION['result'] == ("success") && $_SESSION['action'] == ("unsubscribe"))
    {
    echo "<p>You will shortly receive an email with a link to unsubscribe from the mailing list.</p>";
    }
?>
</div>

Not sure if my PHP syntax is up to scratch (I'm new to it) but it works. Of course div#result shows up as "Please enter a valid email address!" when you initially load up the page because $_SESSION['result'] comes back with "fail". All of the other possibilities work when I submit the form.

Now how would I go about replicating this PHP snippet in AJAX? I imagine that it would involve posting the form to process.php using AJAX, getting the three session variables back, and then processing them in the above manner (but in JavaScript) to display one of the three messages in an initially hidden div (#result) in a popup/bubble manner.

The question is how? I have no experience in using AJAX and it seems like the only way I can achieve the desired outcome.

Thankyou for your help.

Adam

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

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

发布评论

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

评论(1

画骨成沙 2024-12-19 06:11:50

我认为在这种情况下可能没有必要使用会话。

这就是我要做的:

  • 当表单首次加载时,使用 JavaScript 隐藏 div#result
  • 当有人通过 process.php 提交表单时,处理表单并返回一个 JSON 数组,其中包含错误消息列表和表示提交是否成功的变量。
  • AJAX 端应该接收响应,查看 success 变量,如果它为 false,则将这些消息插入到 div#result 中并取消隐藏 div。

I think the use of sessions is probably unnecessary in this instance.

Here's what I would do:

  • When the form first loads, hide div#result with javascript.
  • When someone submits the form through process.php, process the form and return a JSON array containing a list of error messages and a variable denoting whether submission was successful.
  • The AJAX side should recieve the response, look at the success variable and if it is false, insert those messages into div#result and unhide the div.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文