验证表单字段

时间:2013-11-06 04:36:44

标签: javascript php forms post

我正在尝试使用javascript验证某些表单字段,但我似乎无法让它工作。 它只是一个简单的html表单,有一些输入..名称,电话,消息等。

Contact.html

 <form action="sent.php" method="post" id="myform" enctype="multipart/form-data">
  <div id="myform"></div>
  <fieldset class="addingenq">

  <label>Your Name</label>
  <input name="custName" type="text" size="40" >

  <label>Phone Number *</label>
  <input name="custPhone" type="text" size="40" >

  <label>Email Address <em>(Optional)</em></label>
  <input name="custEmail" type="text" size="40" >

  <label>Enquiry type</label>
  <input name="subjectType" type="text" size="40"  >

  <label>Details about Enquiry</label>
  <textarea name="messageType" cols="68" rows="5" class="msg" type="text"></textarea>

  <div class="submitbutton">
  <input type="submit" name="submit" value="Submit Message" >
  </div>
  </fieldset>
 </form>

Send.php

我试图让它返回到contact.html,但它只显示了一个空白的sent.php 虽然我收到了电子邮件。有没有办法让$ from来自custName而不是'custEmail',因为'custEmail'不是这个表单上的要求。

<?php
 if($_POST){
    $custName = $_POST ['custName'];
    $custPhone = $_POST ['custPhone'];
    $custEmail = $_POST ['custEmail'];
    $subjectType = $_POST ['subjectType'];
    $messageType = $_POST ['messageType']; 
  } 
  $subject = "$subjectType";
  $message = "
        Name: $custName \n\n
        Phone Number: $custPhone \n\n
        Enquiry type: $subjectType \n\n
        Message: $messageType \n\n
    ";
  $from = "From: $custEmail\r\n";
  //put your email address here
  mail("myemail@mail.com", $subject, $message, $from);
  exit;             
 ?>

2 个答案:

答案 0 :(得分:0)

使用HTML5 required attr验证表单字段

<form action="sent.php" method="post" id="myform" enctype="multipart/form-data">
  <div id="myform"></div>
  <fieldset class="addingenq">

<label>Your Name</label>
<input name="custName" id="name" required type="text" size="40" >

<label>Phone Number *</label>
<input name="custPhone" id="no" required type="text" size="40" >

<label>Email Address <em>(Optional)</em></label>
<input name="custEmail" type="text" size="40" >

<label>Enquiry type</label>
<input name="subjectType" id="type" type="text" size="40"  >

<label>Details about Enquiry</label>
<textarea name="messageType" id="enq" required cols="68" rows="5" class="msg" type="text"></textarea>

<div class="submitbutton">
<input type="submit" name="submit" value="Submit Message" >
</div>
</fieldset>
</form>

JQuery的

submitButton.onclick = function Validate(){


var name = $("#name").val();
var no = $("#no").val();
var enquiry =     $("#enquiry").val();
var message = $("#ErrorMessage").val();

if(name == '' || no == '' || enquiry == ''){

    message.empty().append ("Complete the registration!!!");
}
else{
    message.empty().append ("Registration Completed");
}

};

答案 1 :(得分:0)

更改 <form action="sent.php" method="post" id="myform" enctype="multipart/form-data">

<form action="sent.php" method="post" id="myform">

并使用if ($_SERVER['REQUEST_METHOD'] == 'POST') { 而不只是

if($_POST){

然后使用

查看您是否获得了任何内容
print_r($_POST)

您可能收到错误,但页面未显示,因为显示错误已关闭,在这种情况下,包括

error_reporting(E_ALL); 
ini_set('display_errors', 1);

在send.php页面上