Witam mam taki problem z formularzem kontaktowym
<div id="kont"> <form name="vform" id="vform" method="get" action="mail.php" width="300px">
<div id="imie1"><input class="required,alphanum" type="text" name="imie" id="imie" autocomplete="off" /></div> <div id="imie2"><img width="16" height="16" name="imie" src="img/blank.gif" alt=""></div>
<div id="email1"><input type="text" name="email" id="email" class="email" autocomplete="off" /></div> <div id="email2"><img width="16" height="16" name="email" src="img/blank.gif" alt=""></div>
<div id="tel1"><input class="required,number" type="text" name="tel" id="tel" autocomplete="off" /></div> <div id="tel2"><img width="16" height="16" name="tel" src="img/blank.gif" alt=""></div>
<div id="wiad1"><textarea id="about" class="required,alphanum" cols="33" rows="4" style="overflow:hidden;" autocomplete="off"></textarea></div> <div id="wiad2"><img width="16" height="16" name="about" src="img/blank.gif" alt=""></div>
<div id="wyslij"> <input type="submit" name="submit" disabled="disabled" id="submit" value="wyślij" ></div>
// Load the getForm function while page is loading
window.onload = getForm;
// Set this to your validation PHP script, default is "validate.php?value="
var vUrl = "http://localhost/cmsmade/validate.php?value=";
// Set this to your form's id
var formid = "vform";
// This is the array for error handling
var vError = [];
// We attach to every input field a little js
function getForm() {
// Important: Our form has to got the "vform" id
var form = document.getElementById(formid);
if (document.getElementsByTagName) {
var vInput = document.getElementsByTagName("input");
for (var vCount=0; vCount<vInput.length; vCount++)
vInput[vCount].onblur = function() { return validateIt(this); }
}
}
// Refers to the function
http = getHTTPObject();
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(E){
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
try {
xmlhttp = new XMLHttpRequest();
}catch(e){
xmlhttp = false;
}
}
return xmlhttp;
}
// The main validation-function
function validateIt(vInput) {
// Each input field's id
vId = vInput.id;
vValue = vInput.value;
// Separate the class attr of each input field
getValue = vInput.className;
if(getValue.indexOf(",") == -1 ) {
vType = getValue;
vRequired = "";
} else {
vRules = vInput.className.split(",");
vRequired = vRules[0];
vType = vRules[1];
}
// The whole URL
var url = vUrl + (vValue) + "&required=" + (vRequired) + "&type=" + (vType);
// And finally we send it to the url we specified
http.open("GET", url, true);
// The handleHttpResponse is the function we call, when we have an
// answer back from the PHP script
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
// A function to handle the response from the PHP script
function handleHttpResponse() {
if(http.readyState == 4) {
// Refering to the PHP script, for every validation we'll get
// either true or false and apply some visual changes in order to
// get the user focusing on each field whether it's ok or not
// If one of the input fields contains an error, the submit button
// will be disabled, until the error (that means all errors) are
// solved
if(http.responseText == "false") {
var sInput = document.getElementById(vId);
var vButton = document.getElementById("submit");
document[vId].src = "http://localhost/cmsmade/img/false.png";
vButton.disabled = true;
vError.push(vId);
}
if(http.responseText == "true") {
var sInput = document.getElementById(vId);
document[vId].src = "http://localhost/cmsmade/img/true.png";
// We do a check if our element is in the error array, and if
// so, we can delete it from the array
if(vError.indexOf(vId) != -1) {
var aId = vError.indexOf(vId);
vError.splice(aId, 1);
if(vError.length > 0) {
var vButton = document.getElementById("submit");
vButton.disabled = true;
} else {
var vButton = document.getElementById("submit");
vButton.disabled = false;
}
}
}
if(http.responseText == "none") {
var sInput = document.getElementById(vId);
var vButton = document.getElementById("submit");
document[vId].src = "http://localhost/cmsmade/img/blank.gif";
vButton.disabled = false;
}
}
}
Otóz chcę zrobić żeby przycisk submit był dostępny dopiero gdy wszystkie wymagane pola będą uzupełnione poprawnie. Więc przy submit dałem disabled="disabled".
w javascript też dałem żeby przycisk się aktywował ale jest problem taki że gdy za pierwszym razem wszystko uzupełnię poprawnię to przycisk pozostaje nieaktywny. Dopiero gdy zrobię jakiś błąd w którymś polu i poprawię go przycisk się aktywuje czy jest ktoś w stanie mi pomóć
Dobra pozmieniałem trochę kod i już działa coś lepiej ale nadal to nie jest to
// Load the getForm function while page is loading
window.onload = getForm;
// Set this to your validation PHP script, default is "validate.php?value="
var vUrl = "http://localhost/cmsmade/validate.php?value=";
// Set this to your form's id
var formid = "vform";
// This is the array for error handling
var vError = [];
// We attach to every input field a little js
function getForm() {
// Important: Our form has to got the "vform" id
var form = document.getElementById(formid);
if (document.getElementsByTagName) {
var vInput = document.getElementsByTagName("input");
for (var vCount=0; vCount<vInput.length; vCount++)
vInput[vCount].onblur = function() { return validateIt(this); }
}
}
// Refers to the function
http = getHTTPObject();
function getHTTPObject() {
var xmlhttp;
/*@cc_on
@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}catch(E){
xmlhttp = false;
}
}
@else
xmlhttp = false;
@end @*/
if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
try {
xmlhttp = new XMLHttpRequest();
}catch(e){
xmlhttp = false;
}
}
return xmlhttp;
}
// The main validation-function
function validateIt(vInput) {
// Each input field's id
vId = vInput.id;
vValue = vInput.value;
// Separate the class attr of each input field
getValue = vInput.className;
if(getValue.indexOf(",") == -1 ) {
vType = getValue;
vRequired = "";
} else {
vRules = vInput.className.split(",");
vRequired = vRules[0];
vType = vRules[1];
}
// The whole URL
var url = vUrl + (vValue) + "&required=" + (vRequired) + "&type=" + (vType);
// And finally we send it to the url we specified
http.open("GET", url, true);
// The handleHttpResponse is the function we call, when we have an
// answer back from the PHP script
http.onreadystatechange = handleHttpResponse;
http.send(null);
}
// A function to handle the response from the PHP script
function handleHttpResponse() {
if(http.readyState == 4) {
// Refering to the PHP script, for every validation we'll get
// either true or false and apply some visual changes in order to
// get the user focusing on each field whether it's ok or not
// If one of the input fields contains an error, the submit button
// will be disabled, until the error (that means all errors) are
// solved
if(http.responseText == "false") {
var sInput = document.getElementById(vId);
var vButton = document.getElementById("submit");
document[vId].src = "http://localhost/cmsmade/img/false.png";
vButton.disabled = true;
vError.push(vId);
}
if(http.responseText == "true1") {
var sInput = document.getElementById(vId);
document[vId].src = "http://localhost/cmsmade/img/true.png";
}
if(http.responseText == "true2") {
var sInput = document.getElementById(vId);
document[vId].src = "http://localhost/cmsmade/img/true.png";
}
if(http.responseText == "true3") {
var sInput = document.getElementById(vId);
document[vId].src = "http://localhost/cmsmade/img/true.png";
}
if(http.responseText == "true3" && "true2" && "true1") {
var sInput = document.getElementById(vId);
var vButton = document.getElementById("submit");
vButton.disabled = false;
}
if(http.responseText == "none") {
var sInput = document.getElementById(vId);
var vButton = document.getElementById("submit");
document[vId].src = "http://localhost/cmsmade/img/blank.gif";
vButton.disabled = false;
}
}
}
a w php
<?
/*
* This is the PHP script to validate the form over AJAX
* Validations types possible:
*
* - alpha
* - alphanum
* - date
* - email
* - number
* - url
*
*/
// Start the main function
StartValidate();
function StartValidate() {
// Assign some var's for the requests
$required = $_GET["required"];
$type = $_GET["type"];
$value = $_GET["value"];
// This is the function to check if a field is even required or not
// So it's useful if you only want to check if it isn't empty
validateRequired($required, $value, $type);
switch ($type) {
case 'number':
validateNumber($value);
break;
case 'alphanum':
validateAlphanum($value);
break;
case 'email':
validateEmail($value);
break;
}
}
// The function to check if a field is required or not
function validateRequired($required, $value, $type) {
if($required == "required") {
// Check if we got an empty value
if($value == "") {
}
} else {
if($value == "") {
}
}
}
// I use regular expressions in order to check a field's input, you can
// get most of them at the Regex Library at <a href="http://www.regexlib.com" target="_blank">http://www.regexlib.com</a>
// There you can check your own regular expressions, too
// Validation of an Email Address
function validateEmail($value) {
if(ereg("^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$", $value, $regs)) { } else {
}
}
// Validation of characters and numbers
function validateAlphanum($value) {
if(ereg("[^\s0-9]", $value, $regs)) { } else {
}
}
// Validation of numbers
function validateNumber($value) {
if(ereg("^[0-9]+$", $value, $regs)) { } else {
}
}
?>
teraz submit robi się aktywny gdy jest zaznaczona wartość true3 więć nie wiem o co chodzi tak jakby sprawdzał czy tylko wartość true3 jest i wtedy aktywował submita a chciałbym żeby przy wszystkich dopiero był aktywny
Ten post edytował szlipek 20.04.2011, 14:55:19