【发布时间】:2011-10-26 22:46:03
【问题描述】:
在使用脚本语言方面我还是个新手,所以我很难理解这些概念。
基本上我有这个表格
<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 的 sn-p 包含到表单的页面中
<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 sn-p?我想这将涉及使用 AJAX 将表单发布到 process.php,取回三个会话变量,然后以上述方式(但在 JavaScript 中)处理它们以在最初隐藏的 div 中显示三个消息之一(#结果)以弹出/气泡的方式。
问题是如何?我没有使用 AJAX 的经验,这似乎是我可以达到预期结果的唯一方法。
感谢您的帮助。
亚当
【问题讨论】:
-
进入 ajax 的良好起点:api.jquery.com/jQuery.ajax
-
您不需要 AJAX 来使其工作。如果用户第一次加载页面 $_POST 数组应该为空, $_SESSION['result'] 不应该在 $_POST 为空的情况下设置为失败,只有在 $_POST['email'] 设置的情况下,但无效。
-
啊哈你是对的!当我昨天尝试它时,浏览器一定在缓存中有一个剩余的会话!我在页面顶部添加了 并在其上添加了表单,我假设在第一个实例中没有数据会导致 $_SESSION['result'] 失败,而不是在一切似乎!!我还是新手,但从我的尝试和错误中学到了很多东西,谢谢大家,再次为@DigitalPrecision 的帮助欢呼!
标签: php ajax session-variables