1、html代码
<h1>诚邀表单</h1>
<div class="row">
<label for="username">宾客姓名:</label>
<input type="text" id="username">
</div>
<div class="row">
<label for="phone">宾客电话:</label>
<input type="text" id="phone">
</div>
<div class="row">
<label for="howmany">宾客人数:</label>
<input type="text" id="howmany">
</div>
<div class="row">
<label for="message">宾客留言:</label>
<input type="text" id="message">
</div>
<div class="row">
<button id="submit">提交</button>
</div>
2、jQuery ajax代码:
$('#submit').click(function(){
var username = $('#username').val();
var phone = $('#phone').val();
var howmany = $('#howmany').val();
var message = $('#message').val();
$.ajax({
type:'get',
url:'input.php?username='+username+'&phone='+phone+'&howmany='+howmany+'&message='+message,
async:true,
success : function(r){
alert(r);
$('.slide8 input').val('');
}
});
})
3、php代码:
<?php
header('Content-type:text/html;charset=utf-8');
$username = $_GET['username'];
$phone = $_GET['phone'];
$howmany = $_GET['howmany'];
$message = $_GET['message'];
if($username==''){
echo '姓名不能不填写啊!';
}else if($phone==''){
echo '手机号码最好填写下!';
}else if($howmany==''){
echo '来几个人不知道啊!';
}else{
$myfile = fopen("u.txt", "a+") or die("Unable to open file!");
$txt = '';
$txt .= '姓名:'.$_GET['username']."\n";
$txt .= '电话:'.$_GET['phone']."\n";
$txt .= '人数:'.$_GET['howmany']."\n";
$txt .= '留言:'.$_GET['message']."\n\n";
fwrite($myfile, $txt);
fclose($myfile);
echo '提交成功';
}
?>
