Javascript 联系方式不包括';不要发送数据
我在Javascript 联系方式不包括';不要发送数据,javascript,php,contact-form,Javascript,Php,Contact Form,我在contact.php上有一个简单的联系人表单,但提交时没有任何数据进入相应的php文件(mail.php) 表单insidecontact.php <form id="contact" action="" method="post"> <input id="name" type="text" name="name" width="250" size="35" placeholder="Your name"> <br><br> <inpu
contact.php
上有一个简单的联系人表单,但提交时没有任何数据进入相应的php文件(mail.php)
表单insidecontact.php
<form id="contact" action="" method="post">
<input id="name" type="text" name="name" width="250" size="35" placeholder="Your name">
<br><br>
<input id="email" type="text" name="email" width="250" size="35" placeholder="Your mail">
<br><br>
<textarea id="message" name="message" rows="6" cols="40" placeholder="Your message"></textarea>
<br><br>
<input type="button" value=" SEND " id="submit" />
<input type="reset" value="Reset" name="reset">
</form>
$('#submit').click(function(){
$.post("mail.php", $("#contact").serialize(), function(response) {
$('#success').html(response);
});
return false;
});
mail.php
print_r( $_POST );
if((empty($_POST['name'])) || (empty($_POST['email'])) || (empty($_POST['message']))){
die("All fields must be filled !");
}
结果:Array()必须填写所有字段代码>尝试一下,将阻止您的表单自然提交:
$('#submit').click(function(e){
e.preventDefault();
});
您可以尝试的另一个更改是:
$("#contact").serializeArray()
工作示例:
<html>
<head>
<title>Example</title>
<style type="text/css">
div { color:blue; margin:3px; cursor:pointer; }
</style>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#submit').click(function(e){
$.post("mail.php", $("#contact").serialize(), function(response) {
$('#success').html(response);
});
return false;
});
});
</script>
</head>
<body>
<div id="success"></div>
<form name="contact" id="contact" method="post">
<?php
for($i=1;$i<=10;$i++)
{?>
Text Box <?php echo $i; ?> <input type="text" name="in<?php echo $i; ?>"/><br/><br/>
<?php
}
?>
<input type="submit" name="submit" id="submit"/>
</form>
</body>
</html>
例子
div{颜色:蓝色;边距:3px;光标:指针;}
$(文档).ready(函数(){
$(“#提交”)。单击(函数(e){
$.post(“mail.php”,$(“#contact”).serialize(),函数(响应){
$('#success').html(回复);
});
返回false;
});
});
文本框对我来说很有用
这是一个.html文件
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
$(function(){
$('#submit').click(function(){
$.post("x.php", $("#contact").serialize(), function(response) {
$('#success').html(response);
});
});
});
</script>
</head>
<body>
<form id="contact" action="" method="post">
<input id="name" type="text" name="name" width="250" size="35" placeholder="Your name">
<br><br>
<input id="email" type="text" name="email" width="250" size="35" placeholder="Your mail">
<br><br>
<textarea id="message" name="message" rows="6" cols="40" placeholder="Your message"></textarea>
<br><br>
<input type="button" value=" SEND " id="submit" />
<input type="reset" value="Reset" name="reset">
</form>
</body>
</html>
$(函数(){
$(“#提交”)。单击(函数(){
$.post(“x.php”,$(“#contact”).serialize(),函数(响应){
$('#success').html(回复);
});
});
});
这是x.php文件
<?php
print_r( $_POST );
if((empty($_POST['name'])) || (empty($_POST['email'])) || (empty($_POST['message']))){
die("All fields must be filled !");
}
?>
哪些数据被发送到服务器?使用Fiddler或您喜爱的浏览器调试工具的“网络”选项卡来确定this@Kippie,我和firebug核实过Post
tab为空,Response
tab在我的Post中有响应内容,即Array()所有字段都必须填写代码>@SunSky您的输入字段是否正确命名?例如,
等@Fred ii-,是的,我检查了两次,你可以试试死(''.print\r($\u POST,true)。'代码>然后使用网络选项卡查看脚本的内容?Dalim,您的代码可以正常工作。谢谢你能检查一下我的密码有什么问题吗?和Jenson M John的答案一样。您应该使用$(function(){\\jquery codes…})模式您应该在$(document).ready(function(){或$(function())中包含您的js代码{
<?php
print_r( $_POST );
if((empty($_POST['name'])) || (empty($_POST['email'])) || (empty($_POST['message']))){
die("All fields must be filled !");
}
?>