使用ajax将表单发送到php文件 [英] send form to php file using ajax

查看:75
本文介绍了使用ajax将表单发送到php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过将表单发送到php文件来学习Ajax,例如对于此表单,我该怎么办?我需要使用ajax和js将此表单(例如)发送到php文件(例如send.php).

i want to learn ajax by sending a form to a php file, for example for this form what should i do? i need send this form (for example) to a php file (like send.php), using ajax and js.

<form name='myform' method='post'>
<input name='name' type='text'/></br>
<input name='email' type='text'/></br>
<input name='title' type='text'/></br>
<textarea name='message'></textarea></br>
<input name='send' type='submit' value='send'/>
</form>

推荐答案

简单...

将HTML更改为此...

Change your HTML to this...

<form id="myform" name='myform' method='post' action="">
   <input name='name' type='text'/></br>
   <input name='email' type='text'/></br>
   <input name='title' type='text'/></br>
   <textarea name='message'></textarea></br>
   <button id="submitbutton">SUBMIT</button>
</form>

在页面顶部包括Jquery库...

Include Jquery library at top of page...

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>

然后在页面上添加此脚本...

Then add this script on the page...

<script>
$(document).ready(function(){

 $('#submitbutton').click(function(e){
   e.preventDefault();
   form = $("#myform").serialize();

   $.ajax({
     type: "POST",
     url: "yourpage.php",
     data: form,
     success: function(data){
         alert('Successful!'); 
     }});

   return false;  //stop the actual form post !important!
  });

});
</script>

另一端的处理是PHP....我不会向您展示那一部分....因为我想您已经知道如何在服务器端处理表单.

THe processing on the other side is PHP....and Im not gonna show you that part....cuz I assume you already know how to process forms on the server side.

这篇关于使用ajax将表单发送到php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆