dpnru86024 2015-08-08 21:48
浏览 58
已采纳

如何在没有ajax,json的情况下完成javascript验证时运行php文件

Currently for school I have to make a contact form with Javascript validation that will check whether name, email etc is filled in. If so, it has to run a php code to send the email ofcourse.

Right now I finished all the javascript validation, but I'm a bit stuck regarding on how to activate php code to send the email after the javascript validation is finished..

I'm not allowed to use anything else like AJAX, Jquery.. I've seen many posts about this, but they were all about these languages..

I wasn't planning about redirecting and using a landing page after submitting the form.

  • 写回答

2条回答 默认 最新

  • duancashi1362 2015-08-08 22:00
    关注

    For this one, you need to reach back in history a bit and use techniques that predate AJAX. Basically, old-school forms from the 90's.

    1. In your HTML, make sure your form has id and name attributes. The typical convention is to give them the same value and make them unique on the page
    2. Make sure all your form fields are inside the form element in your HTML
    3. Set the form's action attribute to the address of your PHP page. The address will vary depending on how your application is set up
    4. Set the form's method to POST. In your PHP page, you should use the standard mechanism to read POST data from the form
    5. Lastly, you need something in your javascript code to cause the form to activate. A typical low-tech way of doing this is to use getElementByID() to get a reference to your form in the DOM, and then call submit() on it. This technique does not require any javascript libraries like jquery. Of course, this should be done after you run your validation code.

    If you need help on how to do the steps, just do a bit more googling and you'll find plenty of details.

    The AJAX techniques that libraries like JQuery use are also doing something similar to a form POST, however it's smarter in that you don't need to leave the page you're on. This was a new feature that browsers started to adopt over 10 years ago. My guess is your professor wants to teach you the fundamentals first before diving into the sophisticated stuff. Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站