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条)

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记