使用jQuery Form Plugin获取表单信息

很多时候我们需要跟表单打交道,今天介绍一种jQuery Form Plugin获取表单信息的方法,需要引用官方的js,更多官方例程,点此直达

 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 
其他跟普通的ajax方法类似,直接放出我测试程序的全部源代码

index.html

<html> 
<head> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="http://malsup.github.com/jquery.form.js"></script> 
    <script> 
        // wait for the DOM to be loaded 
        $(document).ready(function() { 
        	// prepare Options Object 
        	var options = { 
        	    url:        'comment.php',     //覆盖表单中的action url
        	    success:    function() { 
        	        alert('Thanks for your comment!'); //成功返回信息
        	    } 
        	}; 
        	// pass options to ajaxForm 
        	$('#myForm').ajaxForm(options);
        }); 
    </script> 
</head> 
<body>
<form id="myForm" action="comment1.php" method="post"> 
    Name: <input type="text" name="name" /> 
    Comment: <textarea name="comment"></textarea> 
    <input type="submit" value="Submit Comment" /> 
</form>
</body>
</html>

comment.php

/*
 *我将获取到的结果写进文本以此确认已经接收到表单信息
 */
<?php
$name=$_POST['name'];
$fp=fopen("1.txt","w+");
fputs($fp,$name);
fclose($fp);
?>

 

发表回复

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

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据