想要使用Javascript验证php中的联系表单

时间:2017-02-03 05:20:41

标签: javascript php html

This  is my Javascript code
playing-cards-quote.php

function MM_validateForm()
        { //v4.0
          if (document.getElementById)
          {
            var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
            for (i=0; i<(args.length-2); i+=3) 
            { 
        test = args[i+2];
        val=document.getElementById(args[i]);
              if (val)
                  {
                      nm = val.name;
                      if ((val=val.value)!="")
                          {
                if (test.indexOf('isEmail')!=-1)
                    {
                        p=val.indexOf('@');
                  if (p<1 || p==(val.length-1))
                      errors+='- '+nm+' must contain an e-mail address.\n';
                } 
                else 
                    if (test!='R')
                        {
                            num = parseFloat(val);
                  if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
                  if (test.indexOf('inRange') != -1) 
                  {
                      p=test.indexOf(':');
                    min=test.substring(8,p); 
                    max=test.substring(p+1);
                    if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
              }
              } 
              } 
              else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n';
              }
            } 
            if (errors) alert('The following error(s) occurred:\n'+errors);
            document.MM_returnValue = (errors == '');
        }
        }



This is my HTMl code name on the same page
playing-cards-quote.php 


                <h2>Your Contact Information :</h2>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> First Name</label>
                <div class="col-sm-3">
                <input class="form-control" name="fname" type="text" id="fname"  />
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Last  Name</label>
                <div class="col-sm-3">
            <input class="form-control" name="lname"  type="text" id="lname" />
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Email Id</label>
                <div class="col-sm-3">
                <input class="form-control" name="email" type="text" id="email"  />
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <div class="col-sm-6">
                </div>
                <div class="col-sm-5">(Please type in a correct email address , as the quotes will be forwarded there)</div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Telephone Number</label>
                <div class="col-sm-3">
                <input class="form-control" name="telno" type="text" id="telno"  />
                </div>
                <div class="col-sm-3">( Do not enter space)
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Mobile Number</label>
                <div class="col-sm-3">
                <input class="form-control" name="mobileno" type="text" id="mobileno"  />
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6">&nbsp;Company Name</label>
                <div class="col-sm-3">
                <input class="form-control" name="cname" type="text" id="cname"  /></div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Address</label>
                <div class="col-sm-3"><input class="form-control" name="address" type="text" id="address"  />
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Zip Code :</label>
                <div class="col-sm-3">
                <input class="form-control" name="zipcode" type="text" id="zipcode" />
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div> 
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> City</label>
                <div class="col-sm-3">
                <input class="form-control" name="city" type="text" id="city"  />
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> State</label>
                <div class="col-sm-3">
                <input class="form-control" name="state" type="text" id="state"  />
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Country</label>
                <div class="col-sm-3">
                <input class="form-control" name="country" type="text" id="country"  />
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6">&nbsp;Fax</label>
                <div class="col-sm-3">
                <input class="form-control" name="fax" type="text" id="fax"  />
                </div>
                <div class="col-sm-3">
                </div>
                </div>
                </div>
                <div>&nbsp;</div>
                <div class="row">
                <div class="form-group">
                <label class="control-label col-sm-6"><i>*</i> Captcha</label>

            <div class="col-sm-2">
            <?php echo '<img src="captcha.php" alt="captcha">'; ?>
            </div>
            <div class="col-sm-3">
            <input class="form-control" type="text" name="captcha"/></div>
            </div>
            </div>
            <div>&nbsp;</div>
            <div class="row">
            <div class="form-group">
            <div class="col-sm-12">
            <center>
            <input type="submit" value="Submit" class="btn1" name="submit" id="send">
            </center>
            </div>
            </div>
            </div>


This is my thank-you.php code
<?php
    $message = "<table border='1'>";
    $message .= "<tr>
    <th>Plastic Coated Paper</th>
    <td>".($_POST['plastic_coated_paper'] == 'Select Paper' ? 'NA' : $_POST['plastic_coated_paper'])."</td>
    <th>100% Pure Plastic</th>
    <td>".($_POST['pure_plastic'] == 'Select Plastic' ? 'NA' : $_POST['pure_plastic'])."</td>
    </tr>"; 
    $message .= "<tr>
    <th>Quantity (Number of decks)</th>
    <td>".$_POST['quantity']."</td><th>Size Specifications</th><td>".$_POST['size_specifications']."</td>
    </tr>";
    $message .= "<tr><th>Enter your Custom Size and Shape</th><td>".($_POST['custom_size_shape'] == '' ? 'NA' :$_POST['custom_size_shape']).
                "</td><th>Number of cards per deck</th><td>".($_POST['cards_per_deck'] == '' ? 'NA' : $_POST['cards_per_deck'])."</td></tr>";
    $message .= "<tr><th>Playing Card Faces</th><td>".($_POST['playing_card_faces'] == 'Select Faces' ? 'NA' : $_POST['playing_card_faces']).
                "</td><th>Colors to be printed on Front</th><td>".($_POST['colors_to_be_printed_on_front'] == 'Select Color' ? 'NA' : $_POST['colors_to_be_printed_on_front'])."</td></tr>";    
    $message .= "<tr><th>Colors to be printed on Back</th><td>".($_POST['colors_to_be_printed_on_back'] == 'Select Color' ? 'NA' : $_POST['colors_to_be_printed_on_back']).
                "</td><th>Panton Special Color Requirement</th><td>".($_POST['panton_special_color_requirement'] == '' ? 'NA' : $_POST['panton_special_color_requirement'])."</td></tr>";   
    $message .= "<tr><th>Wrapping</th><td>".($_POST['wrapping'] == 'Select Wrapping Options' ? 'NA' : $_POST['wrapping']).
                "</td><th>Coating Finish</th><td>".($_POST['coating_finish'] == 'Select Coating' ? 'NA' : $_POST['coating_finish'])."</td></tr>";   
    $message .= "<tr><th>Gold Foil Options</th><td>".(($_POST['gold_foil_option'] != 'Gold / Silver Printing' && $_POST['gold_foil_option1'] != 'Gold Foil Stamping' ) ? 'NA' : $_POST['gold_foil_option'].'<br/>'.$_POST['gold_foil_option1']).
                "</td><th>Feel Of The Cards</th><td>".(($_POST['feel_of_card'] != 'Non Embossed/Smooth Finished' && $_POST['feel_of_card1'] != 'Embossed/Linen Finished/Textured Cards') ? 'NA' : $_POST['feel_of_card'].$_POST['feel_of_card1'])."</td></tr>"; 
    $message .= "<tr><th>Leather Cases</th><td>".($_POST['leather_cases'] == 'Select Leather Cases' ? 'NA' : $_POST['leather_cases']).
                "</td><th>Instruction Sheet Paper</td><td>".($_POST['instruction_sheet_paper'] == 'Select Instruction Sheet Paper' ? 'NA' : $_POST['instruction_sheet_paper'])."</th></tr>";    
    $message .= "<tr><th>Instruction Sheet Printing Options</th><td>".($_POST['instruction_sheet_printing_options'] == 'Select Printing Options' ? 'NA' : $_POST['instruction_sheet_printing_options']).
                "</td><th>No. of colors for the instruction sheet</th><td>".($_POST['no_of_colors_for_instruction_sheet'] == 'Select Instruction Sheet Color' ? 'NA' : $_POST['no_of_colors_for_instruction_sheet'])."</td></tr>";  
    $message .= "<tr><th>Packaging</th><td>".($_POST['packaging'] == 'Select Packaging' ? 'NA' : $_POST['packaging']).
                "</td><th>Delivery Time</th><td>".($_POST['delivery_time'] == 'Select Delivery Time' ? 'NA' : $_POST['delivery_time'])."</td></tr>";    
    $message .= "<tr><th>Display Pack Option</th><td>".($_POST['display_pack_option'] == 'Select Display Pack Option' ? 'NA' : $_POST['display_pack_option']).
                "</td><th>Mode of Delivery</th><td>".( $_POST['mode_of_delivery'] == 'Select Mode of Delivery' ? 'NA' : $_POST['mode_of_delivery'])."</td></tr>";   
    $message .= "<tr><th>Custom Packaging Enter Your Details</th><td colspan=3>".($_POST['details'] == '' ? 'NA' : $_POST['details'])."</td></tr>"; 
    $message .= "<tr><th>First Name</th><td>".$_POST['fname']."</td><th>Last Name</th><td>".$_POST['lname']."</td></tr>";
    $message .= "<tr><th>Email</th><td>".$_POST['email']."</td><th>Telephone number</th><td>".$_POST['telno']."</td></tr>";
    $message .= "<tr><th>Company Name</th><td>".$_POST['cname']."</td><th>Mobile number</th><td>".$_POST['mobileno']."</td></tr>";
    $message .= "<tr><th>Address</th><td>".$_POST['address']."</td><th>City</th><td>".$_POST['city']."</td></tr>";
    $message .= "<tr><th>Zip Code</th><td>".$_POST['zipcode']."</td><th>State</th><td>".$_POST['state']."</td></tr>";
    $message .= "<tr><th>Country</th><td>".$_POST['country']."</td><th>Fax</th><td>".($_POST['fax'] == '' ? 'NA' : $_POST['fax'])."</td></tr>";
    $message .= "<table>";
    $subject = "Order Detail Summary of alltypesofplayingcards.com";
    $header = "MIME-Version: 1.0\r\n";
    $header .= "Content-type: text/html\r\n";

    //Mail for TMCards
    $mail_tmcards = new PHPMailer();
    $mail_tmcards->IsSMTP();  // set mailer to use SMTP 
    $mail_tmcards->Host = "pro.turbo-smtp.com";       //192.168.1.10 localhost // specify main and backup server
    $mail_tmcards->SMTPAuth = true;           // turn on SMTP authentication
    $mail_tmcards->Username   = "ashok@tmcards.com"; // SMTP account username example
    $mail_tmcards->Password   = "89HHRu738nWqdd"; 
    $mail_tmcards->From = $_POST['email'];
    $mail_tmcards->FromName = $_POST['fname']. " ".$_POST['lname'];
    $mail_tmcards->AddAddress('yogesh@tmcards.com');

    //$mail_user->AddReplyTo($fromemail, 'TMCARDS DOT COM Crm System');
    //$mail_user->ConfirmReadingTo='sales@tmcards.com';
    $mail_tmcards->IsHTML(true);
    $mail_tmcards->Subject=$subject;
    $mail_tmcards->Body=$message;   
    if(!$mail_tmcards->Send())
    {
       echo "Message could not be sent. <p>";
       echo "Mailer Error: ".$mail_tmcards->ErrorInfo;
       exit;
    }
?>

我想使用JavaScript在PHP表单中验证我的表单。 HTML表单还可以,但我不想更改JavaScript代码,更重要的是,我想使用PHP函数在PHP中验证表单。我将在文本框中插入的值应该将其提交给服务器。

0 个答案:

没有答案