2015-09-26 128 views
2

我正在嘗試使用此驗證。問題是沒有檢測到POST,即使我改變了POST。結果仍顯示在URI中。任何身體都可以幫忙?我真的堅持java腳本。如何使用POST提交

這是PHP/HTML代碼:

<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST") { 
echo " Form submited....!!!!"; 
} 
?> 

<head> 
     <script src="JSFormValidation.js"></script> 
    </head> 

    <body>  
     <form id="formTest" method="get" action=""> 
     <table> 
     <tr> 
      <td><label for="txtName">Name<span class="required">*</span></label></td> 
      <td><input type="text" id="txtName" name="name"></td> 
      <td id="elmNameError" class="errorMsg">&nbsp;</td></tr> 
     <tr> 
      <td><label for="txtAddress">Address</label></td> 
      <td><input type="text" id="txtAddress" name="address"></td> 
      <td id="elmAddressError" class="errorMsg">&nbsp;</td></tr> 
     <tr> 
      <td><label for="txtZipcode">Zip Code<span class="required">*</span></label></td> 
      <td><input type="text" id="txtZipcode" name="zipcode"></td> 
      <td id="elmZipcodeError" class="errorMsg">&nbsp;</td></tr> 
     <tr> 
      <td>Country<span class="required">*</span></td> 
      <td><select id="selCountry" name="country"> 
       <option value="" selected>Please select...</option> 
       <option value="AA">AA</option> 
       <option value="BB">BB</option> 
       <option value="CC">CC</option> 
       </select></td> 
      <td id="elmCountryError" class="errorMsg">&nbsp;</td></tr> 
     <tr> 
      <td>Gender<span class="required">*</span></td> 
      <td><label><input type="radio" name="gender" value="m">Male</label> 
       <label><input type="radio" name="gender" value="f">Female</label></td> 
      <td id="elmGenderError" class="errorMsg">&nbsp;</td></tr> 
     <tr> 
      <td>Preferences<span class="required">*</span></td> 
      <td><label><input type="checkbox" name="color" value="r">Red</label> 
       <label><input type="checkbox" name="color" value="g">Green</label> 
       <label><input type="checkbox" name="color" value="b">Blue</label></td> 
      <td id="elmColorError" class="errorMsg">&nbsp;</td></tr> 
     <tr> 
      <td><label for="txtPhone">Phone<span class="required">*</span></label></td> 
      <td><input type="text" id="txtPhone" name="phone"></td> 
      <td id="elmPhoneError" class="errorMsg">&nbsp;</td></tr> 
     <tr> 
      <td><label for="txtEmail">Email<span class="required">*</span></label></td> 
      <td><input type="text" id="txtEmail" name="email"></td> 
      <td id="elmEmailError" class="errorMsg">&nbsp;</td></tr> 
     <tr> 
      <td><label for="txtPassword">Password (6-8 characters)<span class="required">*</span></label></td> 
      <td><input type="password" id="txtPassword" name="password"></td> 
      <td id="elmPasswordError" class="errorMsg">&nbsp;</td></tr> 
     <tr> 
      <td><label for="txtPWVerified">Verify Password<span class="required">*</span></label></td> 
      <td><input type="password" id="txtPWVerified" name="pwVerified"></td> 
      <td id="elmPWVerifiedError" class="errorMsg">&nbsp;</td></tr> 
     <tr> 
      <td>&nbsp;</td> 
      <td><input type="submit" value="SEND" id="btnSubmit">&nbsp; 
       <input type="reset" value="CLEAR" id="btnReset"></td> 
      <td>&nbsp;</td></tr> 
     </table> 
     </form> 

的JSFormValidation.js腳本:

window.onload = init; 

function init() { 
    document.getElementById("formTest").onsubmit = validateForm; 
    document.getElementById("btnReset").onclick = clearForm; 
    document.getElementById("txtName").focus(); 
} 

function validateForm(theForm) { 
    with(theForm) { 
     // return false would prevent default submission 
     return (isNotEmpty(txtName, "Please enter your name!", elmNameError) 
      && isNumeric(txtZipcode, "Please enter a 5-digit zip code!", elmZipcodeError) 
      && isLengthMinMax(txtZipcode, 5, 5, "Please enter a 5-digit zip code!", elmZipcodeError) 
      && isSelected(selCountry, "Please make a selection!", elmCountryError) 
      && isChecked("gender", "Please check a gender!", elmGenderError) 
      && isChecked("color", "Please check a color!", elmColorError) 
      && isNumeric(txtPhone, "Please enter a valid phone number!", elmPhoneError) 
      && isValidEmail(txtEmail, "Enter a valid email!", elmEmailError) 
      && isValidPassword(txtPassword, "Password shall be 6-8 characters!", elmPasswordError) 
      && verifyPassword(txtPassword, txtPWVerified, "Different from new password!", 
       elmPWVerifiedError) 
    ); 
    } 
} 


function postValidate(isValid, errMsg, errElm, inputElm) { 
    if (!isValid) { 
     if (errElm !== undefined && errElm !== null 
      && errMsg !== undefined && errMsg !== null) { 
     errElm.innerHTML = errMsg; 
     } 

     if (inputElm !== undefined && inputElm !== null) { 
     inputElm.classList.add("errorBox"); // Add class for styling 
     inputElm.focus(); 
     } 
    } else { 

     if (errElm !== undefined && errElm !== null) { 
     errElm.innerHTML = ""; 
     } 
     if (inputElm !== undefined && inputElm !== null) { 
     inputElm.classList.remove("errorBox"); 
     } 
    } 
} 


function isNotEmpty(inputElm, errMsg, errElm) { 
    var isValid = (inputElm.value.trim() !== ""); 
    postValidate(isValid, errMsg, errElm, inputElm); 
    return isValid; 
} 

function isNumeric(inputElm, errMsg, errElm) { 
    var isValid = (inputElm.value.trim().match(/^\d+$/) !== null); 
    postValidate(isValid, errMsg, errElm, inputElm); 
    return isValid; 
} 

function isAlphabetic(inputElm, errMsg, errElm) { 
    var isValid = (inputElm.value.trim().match(/^[a-zA-Z]+$/) !== null) ; 
    postValidate(isValid, errMsg, errElm, inputElm); 
    return isValid; 
} 

function isAlphanumeric(inputElm, errMsg, errElm) { 
    var isValid = (inputElm.value.trim().match(/^[0-9a-zA-Z]+$/) !== null); 
    postValidate(isValid, errMsg, errElm, inputElm); 
    return isValid; 
} 


function isLengthMinMax(inputElm, minLength, maxLength, errMsg, errElm) { 
    var inputValue = inputElm.value.trim(); 
    var isValid = (inputValue.length >= minLength) && (inputValue.length <= maxLength); 
    postValidate(isValid, errMsg, errElm, inputElm); 
    return isValid; 
} 

function isValidEmail(inputElm, errMsg, errElm) { 
    var isValid = (inputElm.value.trim().match(
     /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) !== null); 
    postValidate(isValid, errMsg, errElm, inputElm); 
    return isValid; 
} 

function isSelected(selectElm, errMsg, errElm) { 
    var isValid = (selectElm.value !== ""); // value in selected <option> 
    postValidate(isValid, errMsg, errElm, selectElm); 
    return isValid; 
} 


function isChecked(inputName, errMsg, errElm) { 
    var elms = document.getElementsByName(inputName); 
    var isChecked = false; 
    for (var i = 0; i < elms.length; ++i) { 
     if (elms[i].checked) { 
     isChecked = true; 
     break; 
     } 
    } 
    postValidate(isChecked, errMsg, errElm, null); // no focus element 
    return isChecked; 
} 

function isValidPassword(inputElm, errMsg, errElm) { 
    var isValid = (inputElm.value.trim().match(/^\w{6,8}$/) !== null); 
    postValidate(isValid, errMsg, errElm, inputElm); 
    return isValid; 
} 


function verifyPassword(pwElm, pwVerifiedElm, errMsg, errElm) { 
    var isTheSame = (pwElm.value === pwVerifiedElm.value); 
    postValidate(isTheSame, errMsg, errElm, pwVerifiedElm); 
    return isTheSame; 
} 


function clearForm() { 
    // Remove class "errorBox" from input elements 
    var elms = document.querySelectorAll('.errorBox'); // class 
    for (var i = 0; i < elms.length; i++) { 
     elms[i].classList.remove("errorBox"); 
    } 

    // Remove previous error messages 
    elms = document.querySelectorAll('[id$="Error"]'); // id ends with Error 
    for (var i = 0; i < elms.length; i++) { 
     elms[i].innerHTML = ""; 
    } 

    // Set initial focus 
    document.getElementById("txtName").focus(); 
} 

的源代碼:http://www3.ntu.edu.sg/home/ehchua/programming/webprogramming/JavaScript_Examples.html

+2

您的表單方法設置爲GET,並且您的操作中沒有URL。 – Jason

+1

據我所見,您可以阻止表單提交,但實際上並沒有在檢查後提交表單。我沒有閱讀代碼,所以我可能錯過了一些東西。如果是這種情況,您需要在檢查完成後使用'submit();'函數來提交表單。 – Script47

+0

Script47我確實添加了這個 - > form.submit(); return true;經過驗證。但仍然無法正常工作,因爲表單可能未提交任何請求即可提交。 :( – user3706926

回答

0

你可以改變形式method屬性「後「而不是」get「:

<form id="formTest" method="post" action="">...</form> 

這裏假定帖子應該在相同的頁面上。如果不同,請將action更改爲目標頁面。

+0

我改變了發佈。但仍然沒有工作,沒有區別。這就是爲什麼我被卡住了...但沒有Java腳本驗證,表單正在使用方法後(不是方法得到如上所述 – user3706926

+0

@ user3706926檢查我的評論上面的OP。 – Script47

相關問題