【问题标题】:post form with jquery validate使用 jquery 验证发布表单
【发布时间】:2013-11-17 14:37:03
【问题描述】:

我正在拼命尝试使用 POST 方法和 jquery validate plugin 发送表单。

我有一个简单的 php 文件,它应该发送带有数据表单的电子邮件。

作为 Mac 用户,我使用MAMP 来测试 php 服务器部分。

当我单击表单的提交按钮时,我查看了 Web 控制台并且状态正常:

[15:32:50,638] POST http://localhost:8888/contact.php [HTTP/1.1 200 OK 36ms]

但是什么也没发生。看来我的contact.php 文件永远无法访问!

我尝试将文件上传到服务器,但结果相同:没有。

这是我的表单,在 html 中:

<form class="cmxform" id="commentForm" method="" action="">
<fieldset class="myfield">
<legend>For all information, please contact us with the following form :</legend>
<p>
<label class="mylabel" for="iname"><span style="color:red;">* </span>Nom</label>
<input class="myinput" id="iname" name="iname" minlength="2" maxlength="50" type="text" required/>
</p>
<p>
<label class="mylabel" for="imail"><span style="color:red;">* </span>Email</label>
<input class="myinput" id="imail" name="imail" minlength="3" maxlength="100" type="email" required email/>
</p>
<p>
<label class="mylabel" for="isubject"><span style="color:red;">* </span>Sujet</label>
<input class="myinput" id="isubject" name="isubject" minlength="3" maxlength="130" type="text" required/>
</p>
<label class="mylabel" for="imessage" style="vertical-align: top;"><span style="color:red;">* </span> Message</label>
<textarea class="mytextarea" id="imessage" name="imessage" minlength="3" maxlength="2000" type="text" required></textarea>
</p>
<p>
<input class="submit" type="submit" value="Send"/>
</p>
</fieldset>
</form>

对于 jquery 部分,我使用的是post 方法:

$('#commentForm').validate({

 errorClass : "myerror",
 errorElement : 'em',
 submitHandler: function() {       
       $.post( 'contact.php', { name : 'john' } ); //just for testing, i would normally pass the form data
        }
  });

还有 php 部分,这是一个简单的电子邮件转发:

<?php

header('Content-type: text/html; charset=utf-8');

$message = <<<TEXT
name: {$_POST['name']}
email: {$_POST['email']}
subject: {$_POST['subject']}
message: {$_POST['message']}    
TEXT;

$header ='From: contact@xxxxxx.com'."\n";
$header .='Reply-To: '.$_POST['name']."\n";
$header .='Content-Type: text/plain; charset="iso-8859-1"'."\n"; 
$header .='Content-Transfer-Encoding: 8bit';

$to = 'yyyyyy@contact.com';

$subject = $_POST['subject'];

if( ! mail($to, $subject, $message, $header) ) 
{ 
   die('Error sending email.');
}else {
  die(    'Email sent!'.$to .
        'sub:' .$subject .
        'message:' .$message .
        'header:' .$header);
}

我做错了什么?作为 html 开发的新手,我很困惑。我已经浏览过 SO 以找到类似的问题,并尝试复制/粘贴示例,但我仍然遇到问题。

感谢您的帮助..

【问题讨论】:

    标签: php jquery html forms post


    【解决方案1】:

    您的html文件中输入的名称与php文件不同

     <input class="myinput" id="iname" name="iname" minlength="2" maxlength="50" type="text" required/>
    

    这里的名字是"iname"

    在php文件中

    name: {$_POST['name']}
    

    名字是name

    纠正这个并查看结果

    【讨论】:

    • 它也不起作用:(实际上,我可以在 jquery post() 函数中传输任何东西,它不会改变任何东西。我尝试编辑contact.php和添加一个简单的 'phpinfo()' 并且似乎无法访问该 php 文件。
    • 也许你应该检查你的 php 配置
    猜你喜欢
    • 1970-01-01
    • 2010-12-02
    • 1970-01-01
    • 2014-07-11
    • 2011-11-24
    • 2011-01-20
    • 2013-04-10
    • 1970-01-01
    • 2017-07-17
    相关资源
    最近更新 更多