Form

一个完整的 Form 标签大致如下。

<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    <input type="text" name="userName" value="<?php echo $userName; ?>" /></p>
    <span><?php echo $usrErr; ?></span>
    <input type="submit" value="Login">
</form>

Form 标签内的 method 表示提交时使用的请求方式, action 代表提交时的行为。 action 内的 $_SERVER[“PHP_SELF”] 是 PHP 中的超级全局变量,代指当前运行的 PHP 脚本的文件名。

Form 内主要发挥功能的部分由 input 标签构成。 type 表示 input 的类型。输入类 input (例如 text )的 name 属性为发送 POST 请求时的标签名称, value 则为值。这里将 value 设置为变量,是为了在提交表单后保留 input 内值(提交可视为为刷新)。

而若 input 的 type 属性为 submit ,则代表该按钮按下后会执行 form 的 action 操作。

最后,在 input 框下放置一个 span ,用于显示错误信息。若在检查输入数据有效性时出现问题,将错误信息赋值给 $usrErr 变量即可显示错误提示文本。

PHP 脚本

PHP 脚本放置在 body 标签内开头,这样 PHP 脚本将会优先执行。

<?php
    $userName = "";
    $userErr = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if (empty($_POST["userName"])) {
            $usrErr = "User name can't be empty.";
        } else {
            $userName = $_POST["userName"];
            $usrErr = "";
        }
    }
?>

首先定义两个需要用到的变量。

$_SERVER[“REQUEST_METHOD”] 同样也是 PHP 中的超级全局变量,用于获取访问页面的方式。由于提交表单时使用的是 POST ,因此这里判断到请求方式为 POST 时执行表单验证逻辑。

$_POST[“userName”] 用于访问指定名称的 POST 内容。这里为了演示,在判断到 userName 为空时赋值 $usrErr ,在判断到非空时将 $_POST[“userName”] 的值赋值给 $userName 。

对于单一页面的多表单提交

一个页面可能会存在多个表单。使用上面的方法只能对一个表单进行判断,因此需要用到 submit 类 input 的 value 和 name 属性。

修改后的 Form 标签如下,可以看到在 submit 内添加了 name 属性。

<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
    <input type="text" name="userName" value="<?php echo $userName; ?>" /></p>
    <span><?php echo $usrErr; ?></span>
    <input type="submit" value="Login" name="add_patient_submit">
</form>

而修改后的 PHP 脚本如下,可以看到在判断到 POST 请求后多加了一个判断,用于判断 submit 的 name 值。

<?php
    $userName = "";
    $userErr = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        if ($_POST['add_patient_submit'] == 'Confirm') {
            if (empty($_POST["userName"])) {
                $usrErr = "User name can't be empty.";
            } else {
                $userName = $_POST["userName"];
                $usrErr = "";
            }
        }
    }
?>

至此便完成了多表单的判断。

387 个评论

  1. Having read this I thought it was rather enlightening. I appreciate you spending some time and energy to put this short article together. I once again find myself personally spending a lot of time both reading and leaving comments. But so what, it was still worthwhile!

  2. After I initially commented I seem to have clicked the -Notify me when new comments are added- checkbox and from now on each time a comment is added I get 4 emails with the exact same comment. Is there an easy method you are able to remove me from that service? Thank you!

  3. Oh my goodness! Amazing article dude! Thank you so much, However I am encountering issues with your RSS. I don’t understand why I am unable to subscribe to it. Is there anybody else having similar RSS problems? Anyone that knows the answer will you kindly respond? Thanks!!

  4. I really love your website.. Pleasant colors & theme. Did you build this website yourself? Please reply back as I’m trying to create my very own site and would love to know where you got this from or just what the theme is called. Thanks!

  5. After looking over a handful of the articles on your web site, I really like your technique of blogging. I saved as a favorite it to my bookmark webpage list and will be checking back in the near future. Please visit my web site as well and tell me what you think.

  6. After looking over a number of the blog articles on your site, I really appreciate your technique of blogging. I saved it to my bookmark website list and will be checking back soon. Take a look at my website too and tell me your opinion.

  7. Right here is the perfect blog for everyone who wants to find out about this topic. You know a whole lot its almost tough to argue with you (not that I really would want to…HaHa). You definitely put a new spin on a subject that’s been discussed for many years. Great stuff, just excellent!

  8. The next time I read a blog, Hopefully it won’t disappoint me as much as this particular one. After all, I know it was my choice to read, nonetheless I truly believed you would have something interesting to talk about. All I hear is a bunch of whining about something that you can fix if you weren’t too busy seeking attention.

  9. I have to thank you for the efforts you’ve put in penning this site. I am hoping to view the same high-grade blog posts by you in the future as well. In truth, your creative writing abilities has inspired me to get my own site now 😉

  10. An impressive share! I have just forwarded this onto a friend who has been doing a little homework on this. And he in fact ordered me dinner due to the fact that I discovered it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanks for spending some time to talk about this matter here on your website.

  11. I’m amazed, I must say. Rarely do I encounter a blog that’s both educative and interesting, and without a doubt, you have hit the nail on the head. The issue is something not enough men and women are speaking intelligently about. I am very happy that I came across this during my search for something regarding this.

  12. Hi there! I could have sworn I’ve been to this blog before but after looking at a few of the articles I realized it’s new to me. Nonetheless, I’m certainly delighted I stumbled upon it and I’ll be book-marking it and checking back frequently!

  13. Greetings, I do believe your website might be having internet browser compatibility problems. When I look at your website in Safari, it looks fine however, if opening in I.E., it has some overlapping issues. I simply wanted to give you a quick heads up! Aside from that, excellent blog!

  14. An outstanding share! I’ve just forwarded this onto a co-worker who had been doing a little research on this. And he actually bought me lunch due to the fact that I found it for him… lol. So let me reword this…. Thank YOU for the meal!! But yeah, thanx for spending the time to talk about this topic here on your web page.

  15. After looking at a number of the blog posts on your website, I seriously like your technique of blogging. I book marked it to my bookmark webpage list and will be checking back soon. Take a look at my web site too and let me know your opinion.

  16. After I originally left a comment I appear to have clicked on the -Notify me when new comments are added- checkbox and from now on every time a comment is added I recieve four emails with the exact same comment. Is there an easy method you are able to remove me from that service? Thanks a lot!

  17. This is the perfect web site for everyone who hopes to find out about this topic. You understand a whole lot its almost hard to argue with you (not that I actually would want to…HaHa). You certainly put a fresh spin on a topic that’s been written about for many years. Excellent stuff, just wonderful!

  18. I’m impressed, I must say. Rarely do I come across a blog that’s both equally educative and amusing, and let me tell you, you have hit the nail on the head. The problem is something not enough folks are speaking intelligently about. Now i’m very happy I came across this in my search for something concerning this.

  19. An impressive share! I’ve just forwarded this onto a colleague who was conducting a little research on this. And he in fact ordered me breakfast due to the fact that I discovered it for him… lol. So allow me to reword this…. Thank YOU for the meal!! But yeah, thanx for spending some time to talk about this topic here on your site.

  20. Having read this I believed it was really enlightening. I appreciate you finding the time and effort to put this information together. I once again find myself spending a lot of time both reading and posting comments. But so what, it was still worth it!

  21. Hello, I think your blog might be having internet browser compatibility issues. Whenever I take a look at your blog in Safari, it looks fine however, when opening in IE, it’s got some overlapping issues. I just wanted to give you a quick heads up! Other than that, great blog!

  22. An impressive share! I’ve just forwarded this onto a colleague who had been doing a little homework on this. And he actually ordered me dinner due to the fact that I found it for him… lol. So let me reword this…. Thanks for the meal!! But yeah, thanks for spending the time to discuss this topic here on your blog.

  23. Hi there! I could have sworn I’ve visited this site before but after browsing through some of the articles I realized it’s new to me. Nonetheless, I’m certainly happy I found it and I’ll be bookmarking it and checking back regularly!

  24. Hello! I could have sworn I’ve been to your blog before but after looking at a few of the posts I realized it’s new to me. Anyways, I’m definitely delighted I found it and I’ll be bookmarking it and checking back regularly!

  25. I blog often and I genuinely thank you for your content. Your article has truly peaked my interest. I’m going to book mark your site and keep checking for new information about once per week. I opted in for your Feed as well.

  26. Hello there, There’s no doubt that your web site might be having browser compatibility problems. Whenever I look at your web site in Safari, it looks fine but when opening in Internet Explorer, it’s got some overlapping issues. I just wanted to provide you with a quick heads up! Aside from that, fantastic blog!

  27. Having read this I believed it was extremely enlightening. I appreciate you taking the time and effort to put this informative article together. I once again find myself spending a lot of time both reading and commenting. But so what, it was still worthwhile!

  28. May I simply say what a comfort to discover an individual who really knows what they are talking about over the internet. You actually realize how to bring a problem to light and make it important. A lot more people ought to read this and understand this side of the story. I was surprised you’re not more popular since you surely have the gift.

  29. Hello there, I think your web site may be having web browser compatibility issues. Whenever I look at your website in Safari, it looks fine however when opening in I.E., it’s got some overlapping issues. I just wanted to provide you with a quick heads up! Apart from that, excellent website!

  30. Next time I read a blog, I hope that it won’t disappoint me as much as this particular one. I mean, Yes, it was my choice to read, however I actually thought you would have something helpful to talk about. All I hear is a bunch of complaining about something you can fix if you were not too busy seeking attention.

  31. Greetings, I think your site could possibly be having internet browser compatibility issues. When I take a look at your website in Safari, it looks fine however when opening in Internet Explorer, it has some overlapping issues. I simply wanted to provide you with a quick heads up! Besides that, excellent website!

  32. You’re so interesting! I do not think I’ve read anything like that before. So good to find somebody with a few genuine thoughts on this issue. Really.. thanks for starting this up. This site is something that is needed on the internet, someone with a little originality!

  33. Oh my goodness! Incredible article dude! Thank you so much, However I am experiencing difficulties with your RSS. I don’t know why I cannot subscribe to it. Is there anybody else getting similar RSS issues? Anyone who knows the solution can you kindly respond? Thanx!!

  34. Oh my goodness! Impressive article dude! Thank you so much, However I am encountering difficulties with your RSS. I don’t know why I am unable to join it. Is there anybody else getting identical RSS issues? Anyone that knows the solution will you kindly respond? Thanx!!

  35. Go to 1this website to see how to get diamonds in cooking fever for free

    k23lartds11
    Cooking Fever is a lot more fun when you have unlimited free diamonds.If you enjoy mobile games like this you need to check out the link above

    JimDof
  36. Hello there! This post couldn’t be written any better! Going through this article reminds me of my previous roommate! He constantly kept preaching about this. I’ll forward this information to him. Fairly certain he’ll have a good read. Many thanks for sharing!

  37. I’m impressed, I must say. Rarely do I come across a blog that’s both educative and entertaining, and let me tell you, you have hit the nail on the head. The issue is something which too few people are speaking intelligently about. I am very happy that I stumbled across this during my hunt for something concerning this.

  38. This is the right website for anybody who hopes to find out about this topic. You understand a whole lot its almost hard to argue with you (not that I personally would want to…HaHa). You definitely put a new spin on a topic which has been discussed for ages. Excellent stuff, just wonderful!

  39. After checking out a handful of the articles on your blog, I really appreciate your way of writing a blog. I saved as a favorite it to my bookmark site list and will be checking back soon. Please check out my website as well and let me know how you feel.

  40. May I just say what a comfort to uncover someone who truly understands what they’re discussing on the web. You definitely realize how to bring an issue to light and make it important. More people really need to read this and understand this side of the story. It’s surprising you’re not more popular because you definitely possess the gift.

  41. I’d like to thank you for the efforts you have put in penning this site. I really hope to view the same high-grade blog posts by you in the future as well. In fact, your creative writing abilities has motivated me to get my own, personal website now 😉

  42. After I initially commented I appear to have clicked on the -Notify me when new comments are added- checkbox and from now on whenever a comment is added I receive 4 emails with the same comment. There has to be an easy method you are able to remove me from that service? Many thanks!

  43. Right here is the right website for everyone who wants to find out about this topic. You realize so much its almost tough to argue with you (not that I really will need to…HaHa). You definitely put a new spin on a topic which has been written about for decades. Excellent stuff, just wonderful!

  44. Having read this I thought it was rather informative. I appreciate you finding the time and effort to put this information together. I once again find myself personally spending a lot of time both reading and commenting. But so what, it was still worthwhile!

  45. Hi, I do think this is an excellent website. I stumbledupon it 😉 I am going to return once again since i have book marked it. Money and freedom is the greatest way to change, may you be rich and continue to guide others.

  46. Oh my goodness! Awesome article dude! Thank you so much, However I am going through troubles with your RSS. I don’t understand the reason why I cannot join it. Is there anybody else having identical RSS problems? Anyone that knows the solution will you kindly respond? Thanx!!

  47. Oh my goodness! Amazing article dude! Thanks, However I am encountering difficulties with your RSS. I don’t understand the reason why I can’t join it. Is there anyone else getting identical RSS problems? Anyone who knows the solution can you kindly respond? Thanx!!

  48. You are so cool! I do not believe I’ve read something like this before. So good to discover somebody with genuine thoughts on this issue. Seriously.. thanks for starting this up. This site is something that is needed on the web, someone with some originality!

  49. After looking at a few of the blog articles on your web page, I honestly like your technique of writing a blog. I saved it to my bookmark site list and will be checking back soon. Please visit my website as well and tell me how you feel.

  50. Hmm it looks like your site ate my first comment (it
    was super long) so I guess I’ll just sum it up what I had
    written and say, I’m thoroughly enjoying your blog.
    I as well am an aspiring blog blogger but I’m still new to the whole thing.
    Do you have any points for inexperienced blog writers?
    I’d definitely appreciate it.

  51. I’d like to thank you for the efforts you’ve put in penning this site. I’m hoping to check out the same high-grade content from you later on as well. In fact, your creative writing abilities has motivated me to get my very own site now 😉

  52. May I simply say what a comfort to discover someone who genuinely knows what they’re talking about online. You definitely understand how to bring a problem to light and make it important. A lot more people should read this and understand this side of the story. I was surprised that you’re not more popular since you most certainly possess the gift.

  53. I’m impressed, I have to admit. Rarely do I encounter a blog that’s both equally educative and engaging, and let me tell you, you have hit the nail on the head. The issue is an issue that not enough men and women are speaking intelligently about. I’m very happy I found this during my hunt for something relating to this.

  54. Hi there, just became alert to your blog through Google, and
    found that it’s truly informative. I’m going to watch out for brussels.
    I will appreciate if you continue this in future.
    Numerous people will be benefited from your writing. Cheers!

  55. The very next time I read a blog, Hopefully it does not disappoint me just as much as this one. After all, I know it was my choice to read through, nonetheless I actually believed you’d have something helpful to say. All I hear is a bunch of crying about something you could fix if you weren’t too busy looking for attention.

  56. Right here is the right site for anybody who wishes to find out about this
    topic. You realize a whole lot its almost hard to
    argue with you (not that I personally will need to…HaHa).
    You certainly put a new spin on a topic that’s been written about for ages.
    Wonderful stuff, just wonderful!

  57. Can I just say what a relief to uncover an individual who actually understands what they’re discussing on the web. You certainly understand how to bring an issue to light and make it important. More and more people must look at this and understand this side of the story. I was surprised you aren’t more popular since you definitely have the gift.

  58. Hello there, I believe your web site might be having internet browser compatibility problems. When I take a look at your web site in Safari, it looks fine however, when opening in I.E., it has some overlapping issues. I simply wanted to give you a quick heads up! Other than that, great site!

  59. I must thank you for the efforts you have put in writing this site. I am hoping to see the same high-grade blog posts from you in the future as well. In fact, your creative writing abilities has motivated me to get my own website now 😉

  60. Hey I am so thrilled I found your site, I really found
    you by mistake, while I was looking on Aol for something else, Nonetheless I am here now and would just like to say thanks a lot for
    a remarkable post and a all round interesting blog (I also love the theme/design), I don’t have time to read it all
    at the minute but I have saved it and also added your RSS
    feeds, so when I have time I will be back to read more, Please do keep up the great work.

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注