0

I am trying to verify some form fields using javascript but I cant seem to get it to work. Its just a simple html form with a few inputs.. name, phone, message, etc.

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

I attempted to get it return back to contact.html but it just showed a blank sent.php although I did receive the email. Is there a way to make $From something like the custName instead of 'custEmail', as 'custEmail' will not be a requirement on this form.

<?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;             
 ?>
4

2 回答 2

0

Use HTML5 required attr to verify the form fields

<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");
}

};
于 2013-11-06T04:39:01.593 回答
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 页面顶部

于 2013-11-06T04:47:23.383 回答