PHP 和jquery:渐进增强

发布于 2024-08-13 23:15:54 字数 266 浏览 5 评论 0原文

我有几个 php 页面,其中有用于提交数据的表单。我的典型设置是让页面发布到自身,以便我可以验证输入,并在必要时使用用户提交的数据(以及显示错误所在的标记)重新加载页面。如果一切正常,我将重定向到感谢页面。

我想开始使用 ajax (jquery) 提交表单,但如果客户端不使用 javascript,则可以使用原始的 php 提交。我对 jquery 还很陌生,并且发现了几个使用 jquery 提交的示例,但没有一个使用我上面描述的设置。如果可能的话,有人可以给出一个简短的例子或指出一个页面。谢谢

I have several php pages that have forms used to submit data. My typical setup is to have the page post to itself so that I can validate the input and if necessary reload the page with the user submitted data (and markup to show where the error is). If everything is OK I redirect to a thank you page.

I would like to start using ajax (jquery) to submit the forms, but have the original php submit to fall back on incase the client is not using javascript. I'm still very new to jquery and have found several examples to submit using jquery but none using the setup I described above. If possible could someone give a brief example or point to a page that does. Thanks

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

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

发布评论

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

评论(3

心作怪 2024-08-20 23:15:54

您可以在此处使用 jquery 表单插件 http://jquery.malsup.com/form/

这将是转换现有页面的简单方法。

You could possibly use the jquery form plugin here http://jquery.malsup.com/form/

that would be an easy way convert existing pages.

裸钻 2024-08-20 23:15:54

您可以使用 POST 方法通过 jQuery 提交 POST 数据:

$.post("verify.php", {"name":"jonathan"}, function(results) {
  if (results.response == "ok") {
    alert(results.message);
    $("#form").remove();
  } else {
    alert(results.message);
  }
}, "json");

示例脚本可能如下:

if ($_POST["name"] == "jonathan")
{
  print json_encode(array(
    "response" => "ok",
    "message"  => "You have been verified.";
  ));
}
else
{
  print json_encode(array(
    "response" => "fail",
    "message"  => "Your name is incorrect.";
  ));
}

You can submit POST data with jQuery by using the POST method:

$.post("verify.php", {"name":"jonathan"}, function(results) {
  if (results.response == "ok") {
    alert(results.message);
    $("#form").remove();
  } else {
    alert(results.message);
  }
}, "json");

An example-script may be the following:

if ($_POST["name"] == "jonathan")
{
  print json_encode(array(
    "response" => "ok",
    "message"  => "You have been verified.";
  ));
}
else
{
  print json_encode(array(
    "response" => "fail",
    "message"  => "Your name is incorrect.";
  ));
}
空气里的味道 2024-08-20 23:15:54

如果响应正常,则 $.post 回调中的 window.location.href =thankyou_page_url 。

window.location.href = thankyou_page_url in your $.post callback if the response is OK.

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