0

私はJavaScriptを使用したフィールド検証を必要とするphpフォームに取り組んでいますが、私はこれの新しい人なので、phpでそれを使用する方法がわかりません。次のコードでそれをやろうとした方法

<?php

 session_start();
 // include_once('header.php');

?>

<div id="contentarea">

<div style="background-color:#3fa0c1;color:#FFF">
 <h2 style="color:#FFF"> Checkout </h2>
</div>
<br/>  
<h4> Welcome to Checkout. <br/>
Please fill in the fields below to complete your purchase! 
</h4><br/>


<form action=" " method="POST" name="Form" onsubmit="return ValidateForm();"  style="color:#000000; font-size:18px;" 

<?php switch ($_GET["checkoutType"]) {
        case "Guest":   echo 'action="guestCheckout.php"'; break;
        case "Register": echo 'action="registerCheckout.php"'; break;
    }
?>

>   
    <h3> BILLING ADDRESS </h3><br/>

    First Name*
    <input style="height:20px; margin-bottom:10px;" name="firstname" size="40" id="firstname"/> <br/>

    Last Name* 
    <input style="height:20px; margin-bottom:10px;"  name="lastname" size="40"/><br/>

    Email Address* 
    <input style="height:20px; margin-bottom:10px;"  name="email" size="40"/><br/>

    Confirm Email Address* 
    <input style="height:20px; margin-bottom:10px;"  name="email2" size="40"/><br/>

    Telephone* 
    <input style="height:20px; margin-bottom:10px;"  name="telephone" size="40"/><br/>

    Address* <br/>
    <input  style="height:20px; margin-bottom:10px;"  name="addr1" id="addr1" size="40"/><br/>
    <input  style="height:20px; margin-bottom:10px;"  name="addr2" id="addr2" size="40"/><br/>

    Country*  <br/>
    <!-- input  style="height:20px; margin-bottom:10px;"  name="country" size="40"/><br/-->
    <select name="country" id="country">
         <option value="select_country"> Select Country </option>
         <option value="US"> United States </option>
    </select>
    <br/>
    <br/>

    City*  <br/>
    <input  style="height:20px; margin-bottom:10px;"  name="city" size="40"/><br/>

    Zip Code* 
    <input  style="height:20px; margin-bottom:10px;" name="postcode" size="40"/><br/>

    State* <br/>
    <!-- input  style="height:20px; margin-bottom:10px;"  name="state" size="40"/><br/-->
    <select name="region" id="state">
         <option value="select_state"> Select Region</option>
         <option value="FL"> Florida </option>
         <option value="GA"> Georgia </option>
    </select>
    <br/><br/>

    Company
    <input  style="height:20px; margin-bottom:10px;" name="company" size="40"/><br/>

    <input name="ship_to_same_addr" type="checkbox"  checked="checked" /> Ship to the same address. <br/><br/>

<h3> SHIPPING METHOD </h3><br/>

    <b> Post Office: Priority Mail (USA) - First Class (International) </b> <br/>
    <input type="radio" name="shipping_method" id="shipping_method" checked="checked" />  Fixed <b> $6.95</b>. <br/><br/>

<h3> PAYMENT METHOD </h3><br/>

    <input type="radio" name="pay_method" id="pay_method" checked="checked" />  Credit Card (Authorize.net) <br/><br/>

    Credit Card Type*
    <!--input  style="height:20px; margin-bottom:10px;"  name="cc_type" size="40"/><br/ -->
    <select name="cc_type" id="cc_type">
     <option value="AE"> American Express </option>
     <option value="DI"> Discover </option>
     <option value="MC"> Master Card </option>
     <option value="VI"> Visa </option>
    </select>
    <br/><br/>

    Credit Card Number*
    <input  style="height:20px; margin-bottom:10px;"  name="cc_number" id="cc_number" size="40"/><br/>

    Credit Card Security Code*
    <input  style="height:20px; margin-bottom:10px;"  name="cc_cid" id="cc_cid" size="40"/><br/>

    Expiration Date*<br/>
    <!-- input  style="height:20px; margin-bottom:10px;"  name="cc_exp_month" size="20"/-->
     <select name="cc_exp_month" id="cc_exp_month">
         <option value="select_month"> Select Month </option>
         <option value="01"> 01 - January </option>
         <option value="02"> 02 - February </option>
         <option value="03"> 03 - March </option>
         <option value="04"> 04 - April </option>
         <option value="05"> 05 - May </option>
         <option value="06"> 06 - June </option>
         <option value="07"> 07 - July  </option>
         <option value="08"> 08 - August </option>
         <option value="09"> 09 - September </option>
         <option value="10"> 10 - October </option>
         <option value="11"> 11 - November </option>
         <option value="12"> 12 - December </option>
    </select>
    <br/>
    <!-- input  style="height:20px; margin-bottom:10px;"  name="cc_exp_year" size="20"/--><br/>
    <select name="cc_exp_year" id="cc_exp_year">
         <option value="select_year"> Select Year </option>
         <option value="2012"> 2012 </option>
         <option value="2013"> 2013 </option>
         <option value="2014"> 2014 </option>
         <option value="2015"> 2015 </option>
         <option value="2016"> 2016 </option>
         <option value="2017"> 2017 </option>
         <option value="2018"> 2018 </option>
         <option value="2019"> 2019 </option>
         <option value="2020"> 2020 </option>
    </select>
    <br/><br/>

    Coupon Code
     <input  style="height:20px; margin-bottom:10px;"  name="coupon_code" size="40"/><br/>

    Comments
    <textarea rows="6" cols="40" name="comments"></textarea><br/><br/>

    <input type="submit" value="Place Order Now" />

</form>


<script>
function ValidateForm()
{
    var firstname = document.Form.firstname;
    var lastname = document.Form.lastname;

    var email = document.Form.email;
    var email2 = document.Form.email2;   
    var telephone = document.Form.telephone;

    var address = document.Form.addrl;
    var address = document.Form.addr2;
     var country = document.Form.country;
     var city = document.Form.city;

     var postcode = document.Form.postcode;
     var state = document.Form.state;
     var company = document.Form.company;
     var creditcardtype = document.Form.cc_type;
     var creditcardnumber = document.Form.cc_number;
     var creditcardsecuritycode = document.Form.cc_cid;
     var expirationdate = document.Form.cc_exp_month;
     var expirationdate = document.Form.cc_exp_month;


    if (firstname.value == "")
    {
        window.alert("Please enter your last name.");
        firstname.focus();
        return false;
    }  


    if (lastname.value == "")
    {
        window.alert("Please enter your last name.");
        lastname.focus();
        return false;
    }  

    if (email.value == "")
    {
        window.alert("Please enter e-mail address.");
        email.focus();
        return false;
    }
    if (email.value.indexOf("@", 0) < 0)
    {
        window.alert("Please enter a valid e-mail address.");
        email.focus();
        return false;
    }
    if (email2.value == "")
    {
        window.alert("Please enter confirm e-mail address.");
        email2.focus();
        return false;
    }
    if (email2.value.indexOf("@", 0) < 0)
    {
        window.alert("Please enter a valid e-mail address.");
        email2.focus();
        return false;
    }
    if(email.value != email2.value)
    {
      window.alert("Please enter email & confirm email both same");
      return false;
    }

    if  (telephone.value == "")
    {
        window.alert("Please enter your telephone number.");
        telephone.focus();
        return false;
    }

    if (addr1.value == "")
    {
        window.alert("Please enter your address1.");
        add1.focus();
        return false;
    }
     if (addr2.value == "")
    {
        window.alert("Please enter your address2.");
        add2.focus();
        return false;
    }

   if (country.selectedIndex < 1)
    {
        alert("Please select country.");
        country.focus();
        return false;
    }
     if (city.value == "")
    {
        window.alert("Please enter city.");
        city.focus();
        return false;
    }
     if (postcode.value == "")
    {
        window.alert("Please enter zip code.");
        postcode.focus();
        return false;
    }
     if (state.selectedIndex < 1)
    {
        window.alert("Please enter state.");
        state.focus();
        return false;
    }

    if (cc_type.selectedIndex < 1)
    {
        window.alert("Please enter credit card type.");
        cc_type.focus();
        return false;
    }
    if (cc_number.value == "")
    {
        window.alert("Please enter credit card number.");
        cc_number.focus();
        return false;
    }
    if (cc_cid.value == "")
    {
        window.alert("Please enter credit card security code.");
        cc_cid.focus();
        return false;
    }
    if (cc_exp_month.selecedIndex < 1)
    {
        window.alert("Please enter expiry date.");
        cc_exp_montht.focus();
        return false;
    }
    if (cc_exp_year.selectedIndex < 1)
    {
        window.alert("Please enter expiry date.");
        cc_exp_year.focus();
        return false;
    }


    return true;
}


</script>

</div>
<?php 

//include_once('footer.php')

?>

正常に動作しているように見えますが、アラートボックスを表示する代わりに、各フィールドの検証が完了した後にエラーメッセージを表示する必要があります。

4

2 に答える 2

2

Validation Pluginを使用して、フォームの検証を効果的に管理できます。 http://bassistance.de/jquery-plugins/jquery-plugin-validation/

于 2012-05-01T05:01:40.117 に答える
0

JavaScript の検証を試してみたい場合はadd div or span with display property none. そしてif error then in that span/div display block and show error in that div/span。たとえば、以下のコードを参照してください。

HTMLで

 <input  style="height:20px; margin-bottom:10px;"  name="name" size="40"/><br/>
<span id="name_error" style="display:none"></span>     

JavaScriptで

<script>
function ValidateForm()
{
   var name = document.Form.name;

  if(name.value == ''){
    document.getElementById('name_error').innerHTML = 'Please provide name!';
    document.getElementById('name_error').style.display = 'block';        
    name.focus();
    return false;
  }else{
    document.getElementById('name_error').innerHTML = '';
    document.getElementById('name_error').style.display = 'none';        
  }
 }
</script>

これを試してください。うまくいくことを願っています。ここで、そのフィールドにエラーが存在する場合は、そのスパンにエラーを表示します。それ以外の場合は、何も表示しません。

タク。

于 2012-05-01T04:53:41.383 に答える