Jump to content

Recommended Posts

Posted

Am nevoie urgent de o forma de contact, care sa contina 4 campuri (Nume/Adresa/Telefon/Email) si 2 casete de bifare + un buton care sa se numeasca "Trimite Comanda".

Cand se apasa pe buton, acesta trebuie sa verifice daca:

-cele 4 campuri sunt completate sau goale (daca sunt necompletate sa apara un mesaj)

-cele 2 casete de bifat trbuie NEAPARAT sa fie bifate, altfel da un mesaj.

Iar daca totul este in regula, sa-mi trimita cele 4 informatii (Nume/Adresa/Telefon/Email) pe emailul meu.

Ma poate ajuta cineva ?

Posted

Faci un fisier html cu:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title>Contact us</title>
<style>
h1
{
font-family : Arial, Helvetica, sans-serif;
font-size : 16px;
font-weight : bold;
}
label,a
{
font-family : Arial, Helvetica, sans-serif;
font-size : 12px;
}

</style>
<script language="JavaScript" src="validator.js" type="text/javascript"></script>
</head>

<body>

<h1>Contact Form</h1>
<form method="POST" name="contactform" action="contact-form.php">
<p>
<label for='name'>Nume:</label><br />
<input type="text" name="name">
</p>
<p>
<label for='adress'>Adresa:</label><br />
<input type="text" name="adress">
</p>
<p>
<label for='phone'>Telefon:</label><br />
<input type="text" name="phone">
</p>
<p>
<label for='email'>Email Address:</label><br />
<input type="text" name="email">
</p>
<p>
<label for='check1'>Ciocolata ?</label> <input type="checkbox" name="agree1" checked="checked" /><br />
<label for='check2'>Cirese ?</label> <input type="checkbox" name="agree2" checked="checked" />
</p>
<input type="submit" value="Trimite comanda"> <input type="reset" name="Submit2" value="Reset">
</form>

<script language="JavaScript">
var frmvalidator = new Validator("contactform");
frmvalidator.addValidation("name","req","please provide your name");
frmvalidator.addValidation("adress","req","please provide your adress");
frmvalidator.addValidation("phone","req","please provide your phone");
frmvalidator.addValidation("email","req","please provide your email");
frmvalidator.addValidation("email","email","please enter a valid email address");
</script>
</body>
</html>[/code] Faci o pagina [b]contact-form.php[/b] care sa contina:
[code]<?php
$errors = '';
$myemail = 'yourname@website.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}

$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];

if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";

$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";

mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>

<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>


</body>
</html>
Faci o pagina contact-form-thank-you.html, pentru multumire, care sa contina, un mesaj de genul:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
<title>Thank you!</title>
<!-- define some style elements-->
<style>
h1
{
font-family : Arial, Helvetica, sans-serif;
font-size : 16px;
font-weight : bold;
}
label,a,body
{
font-family : Arial, Helvetica, sans-serif;
font-size : 12px;
}

</style>
<!-- a helper script for vaidating the form-->
</head>
</head>

<body>
<h1>Thank you!</h1>
Thank you for submitting the form. We will contact you soon!


</body>
</html>[/code] Creezi validatorul, [b]validator.js[/b]:
[code]/*
-------------------------------------------------------------------------
JavaScript Form Validator (gen_validatorv31.js)
Version 3.1.2
Copyright (C) 2003-2008 JavaScript-Coder.com. All rights reserved.
You can freely use this script in your Web pages.
You may adapt this script for your own needs, provided these opening credit
lines are kept intact.

The Form validation script is distributed free from JavaScript-Coder.com
For updates, please visit:
http://www.javascript-coder.com/html-form/javascript-form-validation.phtml

Questions & comments please send to form.val at javascript-coder.com
-------------------------------------------------------------------------
*/
function Validator(frmname)
{
this.formobj=document.forms[frmname];
if(!this.formobj)
{
alert("Error: couldnot get Form object "+frmname);
return;
}
if(this.formobj.onsubmit)
{
this.formobj.old_onsubmit = this.formobj.onsubmit;
this.formobj.onsubmit=null;
}
else
{
this.formobj.old_onsubmit = null;
}
this.formobj._sfm_form_name=frmname;
this.formobj.onsubmit=form_submit_handler;
this.addValidation = add_validation;
this.setAddnlValidationFunction=set_addnl_vfunction;
this.clearAllValidations = clear_all_validations;
this.disable_validations = false;//new
document.error_disp_handler = new sfm_ErrorDisplayHandler();
this.EnableOnPageErrorDisplay=validator_enable_OPED;
this.EnableOnPageErrorDisplaySingleBox=validator_enable_OPED_SB;
this.show_errors_together=true;
this.EnableMsgsTogether=sfm_enable_show_msgs_together;
document.set_focus_onerror=true;
this.EnableFocusOnError=sfm_validator_enable_focus;

}

function sfm_validator_enable_focus(enable)
{
document.set_focus_onerror = enable;
}

function set_addnl_vfunction(functionname)
{
this.formobj.addnlvalidation = functionname;
}

function sfm_set_focus(objInput)
{
if(document.set_focus_onerror)
{
objInput.focus();
}
}

function sfm_enable_show_msgs_together()
{
this.show_errors_together=true;
this.formobj.show_errors_together=true;
}
function clear_all_validations()
{
for(var itr=0;itr < this.formobj.elements.length;itr++)
{
this.formobj.elements[itr].validationset = null;
}
}

function form_submit_handler()
{
var bRet = true;
document.error_disp_handler.clear_msgs();
for(var itr=0;itr < this.elements.length;itr++)
{
if(this.elements[itr].validationset &&
!this.elements[itr].validationset.validate())
{
bRet = false;
}
if(!bRet && !this.show_errors_together)
{
break;

}
}

if(this.addnlvalidation)
{
str =" var ret = "+this.addnlvalidation+"()";
eval(str);

if(!ret)
{
bRet=false;
}

}

if(!bRet)
{
document.error_disp_handler.FinalShowMsg();
return false;
}
return true;
}

function add_validation(itemname,descriptor,errstr)
{
var condition = null;
if(arguments.length > 3)
{
condition = arguments[3];
}
if(!this.formobj)
{
alert("Error: The form object is not set properly");
return;
}//if
var itemobj = this.formobj[itemname];
if(itemobj.length && isNaN(itemobj.selectedIndex) )
//for radio button; don't do for 'select' item
{
itemobj = itemobj[0];
}
if(!itemobj)
{
alert("Error: Couldnot get the input object named: "+itemname);
return;
}
if(!itemobj.validationset)
{
itemobj.validationset = new ValidationSet(itemobj,this.show_errors_together);
}
itemobj.validationset.add(descriptor,errstr,condition);
itemobj.validatorobj=this;
}
function validator_enable_OPED()
{
document.error_disp_handler.EnableOnPageDisplay(false);
}

function validator_enable_OPED_SB()
{
document.error_disp_handler.EnableOnPageDisplay(true);
}
function sfm_ErrorDisplayHandler()
{
this.msgdisplay = new AlertMsgDisplayer();
this.EnableOnPageDisplay= edh_EnableOnPageDisplay;
this.ShowMsg=edh_ShowMsg;
this.FinalShowMsg=edh_FinalShowMsg;
this.all_msgs=new Array();
this.clear_msgs=edh_clear_msgs;
}
function edh_clear_msgs()
{
this.msgdisplay.clearmsg(this.all_msgs);
this.all_msgs = new Array();
}
function edh_FinalShowMsg()
{
this.msgdisplay.showmsg(this.all_msgs);
}
function edh_EnableOnPageDisplay(single_box)
{
if(true == single_box)
{
this.msgdisplay = new SingleBoxErrorDisplay();
}
else
{
this.msgdisplay = new DivMsgDisplayer();
}
}
function edh_ShowMsg(msg,input_element)
{

var objmsg = new Array();
objmsg["input_element"] = input_element;
objmsg["msg"] = msg;
this.all_msgs.push(objmsg);
}
function AlertMsgDisplayer()
{
this.showmsg = alert_showmsg;
this.clearmsg=alert_clearmsg;
}
function alert_clearmsg(msgs)
{

}
function alert_showmsg(msgs)
{
var whole_msg="";
var first_elmnt=null;
for(var m=0;m < msgs.length;m++)
{
if(null == first_elmnt)
{
first_elmnt = msgs[m]["input_element"];
}
whole_msg += msgs[m]["msg"] + "\n";
}

alert(whole_msg);

if(null != first_elmnt)
{
sfm_set_focus(first_elmnt);
}
}
function sfm_show_error_msg(msg,input_elmt)
{
document.error_disp_handler.ShowMsg(msg,input_elmt);
}
function SingleBoxErrorDisplay()
{
this.showmsg=sb_div_showmsg;
this.clearmsg=sb_div_clearmsg;
}

function sb_div_clearmsg(msgs)
{
var divname = form_error_div_name(msgs);
show_div_msg(divname,"");
}

function sb_div_showmsg(msgs)
{
var whole_msg="<ul>\n";
for(var m=0;m < msgs.length;m++)
{
whole_msg += "<li>" + msgs[m]["msg"] + "</li>\n";
}
whole_msg += "</ul>";
var divname = form_error_div_name(msgs);
show_div_msg(divname,whole_msg);
}
function form_error_div_name(msgs)
{
var input_element= null;

for(var m in msgs)
{
input_element = msgs[m]["input_element"];
if(input_element){break;}
}

var divname ="";
if(input_element)
{
divname = input_element.form._sfm_form_name + "_errorloc";
}

return divname;
}
function DivMsgDisplayer()
{
this.showmsg=div_showmsg;
this.clearmsg=div_clearmsg;
}
function div_clearmsg(msgs)
{
for(var m in msgs)
{
var divname = element_div_name(msgs[m]["input_element"]);
show_div_msg(divname,"");
}
}
function element_div_name(input_element)
{
var divname = input_element.form._sfm_form_name + "_" +
input_element.name + "_errorloc";

divname = divname.replace(/[\[\]]/gi,"");

return divname;
}
function div_showmsg(msgs)
{
var whole_msg;
var first_elmnt=null;
for(var m in msgs)
{
if(null == first_elmnt)
{
first_elmnt = msgs[m]["input_element"];
}
var divname = element_div_name(msgs[m]["input_element"]);
show_div_msg(divname,msgs[m]["msg"]);
}
if(null != first_elmnt)
{
sfm_set_focus(first_elmnt);
}
}
function show_div_msg(divname,msgstring)
{
if(divname.length<=0) return false;

if(document.layers)
{
divlayer = document.layers[divname];
if(!divlayer){return;}
divlayer.document.open();
divlayer.document.write(msgstring);
divlayer.document.close();
}
else
if(document.all)
{
divlayer = document.all[divname];
if(!divlayer){return;}
divlayer.innerHTML=msgstring;
}
else
if(document.getElementById)
{
divlayer = document.getElementById(divname);
if(!divlayer){return;}
divlayer.innerHTML =msgstring;
}
divlayer.style.visibility="visible";
}

function ValidationDesc(inputitem,desc,error,condition)
{
this.desc=desc;
this.error=error;
this.itemobj = inputitem;
this.condition = condition;
this.validate=vdesc_validate;
}
function vdesc_validate()
{
if(this.condition != null )
{
if(!eval(this.condition))
{
return true;
}
}
if(!validateInput(this.desc,this.itemobj,this.error))
{
this.itemobj.validatorobj.disable_validations=true;

sfm_set_focus(this.itemobj);

return false;
}
return true;
}
function ValidationSet(inputitem,msgs_together)
{
this.vSet=new Array();
this.add= add_validationdesc;
this.validate= vset_validate;
this.itemobj = inputitem;
this.msgs_together = msgs_together;
}
function add_validationdesc(desc,error,condition)
{
this.vSet[this.vSet.length]=
new ValidationDesc(this.itemobj,desc,error,condition);
}
function vset_validate()
{
var bRet = true;
for(var itr=0;itr<this.vSet.length;itr++)
{
bRet = bRet && this.vSet[itr].validate();
if(!bRet && !this.msgs_together)
{
break;
}
}
return bRet;
}
function validateEmail(email)
{
var splitted = email.match("^(.+)@(.+)$");
if(splitted == null) return false;
if(splitted[1] != null )
{
var regexp_user=/^\"?[\w-_\.]*\"?$/;
if(splitted[1].match(regexp_user) == null) return false;
}
if(splitted[2] != null)
{
var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
if(splitted[2].match(regexp_domain) == null)
{
var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
if(splitted[2].match(regexp_ip) == null) return false;
}// if
return true;
}
return false;
}

function IsCheckSelected(objValue,chkValue)
{
var selected=false;
var objcheck = objValue.form.elements[objValue.name];
if(objcheck.length)
{
var idxchk=-1;
for(var c=0;c < objcheck.length;c++)
{
if(objcheck[c].value == chkValue)
{
idxchk=c;
break;
}//if
}//for
if(idxchk>= 0)
{
if(objcheck[idxchk].checked=="1")
{
selected=true;
}
}//if
}
else
{
if(objValue.checked == "1")
{
selected=true;
}//if
}//else

return selected;
}
function TestDontSelectChk(objValue,chkValue,strError)
{
var pass = true;
pass = IsCheckSelected(objValue,chkValue)?false:true;

if(pass==false)
{
if(!strError || strError.length ==0)
{
strError = "Can't Proceed as you selected "+objValue.name;
}//if
sfm_show_error_msg(strError,objValue);

}
return pass;
}
function TestShouldSelectChk(objValue,chkValue,strError)
{
var pass = true;

pass = IsCheckSelected(objValue,chkValue)?true:false;

if(pass==false)
{
if(!strError || strError.length ==0)
{
strError = "You should select "+objValue.name;
}//if
sfm_show_error_msg(strError,objValue);

}
return pass;
}
function TestRequiredInput(objValue,strError)
{
var ret = true;
var val = objValue.value;
val = val.replace(/^\s+|\s+$/g,"");//trim
if(eval(val.length) == 0)
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : Required Field";
}//if
sfm_show_error_msg(strError,objValue);
ret=false;
}//if
return ret;
}
function TestMaxLen(objValue,strMaxLen,strError)
{
var ret = true;
if(eval(objValue.value.length) > eval(strMaxLen))
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : "+ strMaxLen +" characters maximum ";
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestMinLen(objValue,strMinLen,strError)
{
var ret = true;
if(eval(objValue.value.length) < eval(strMinLen))
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : " + strMinLen + " characters minimum ";
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestInputType(objValue,strRegExp,strError,strDefaultError)
{
var ret = true;

var charpos = objValue.value.search(strRegExp);
if(objValue.value.length > 0 && charpos >= 0)
{
if(!strError || strError.length ==0)
{
strError = strDefaultError;
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestEmail(objValue,strError)
{
var ret = true;
if(objValue.value.length > 0 && !validateEmail(objValue.value) )
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": Enter a valid Email address ";
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestLessThan(objValue,strLessThan,strError)
{
var ret = true;
if(isNaN(objValue.value))
{
sfm_show_error_msg(objValue.name +": Should be a number ",objValue);
ret = false;
}//if
else
if(eval(objValue.value) >= eval(strLessThan))
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : value should be less than "+ strLessThan;
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestGreaterThan(objValue,strGreaterThan,strError)
{
var ret = true;
if(isNaN(objValue.value))
{
sfm_show_error_msg(objValue.name+": Should be a number ",objValue);
ret = false;
}//if
else
if(eval(objValue.value) <= eval(strGreaterThan))
{
if(!strError || strError.length ==0)
{
strError = objValue.name + " : value should be greater than "+ strGreaterThan;
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestRegExp(objValue,strRegExp,strError)
{
var ret = true;
if( objValue.value.length > 0 &&
!objValue.value.match(strRegExp) )
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": Invalid characters found ";
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}//if
return ret;
}
function TestDontSelect(objValue,dont_sel_index,strError)
{
var ret = true;
if(objValue.selectedIndex == null)
{
sfm_show_error_msg("ERROR: dontselect command for non-select Item");
ret = false;
}
if(objValue.selectedIndex == eval(dont_sel_index))
{
if(!strError || strError.length ==0)
{
strError = objValue.name+": please Select one option ";
}//if
sfm_show_error_msg(strError,objValue);
ret = false;
}
return ret;
}
function TestSelectOneRadio(objValue,strError)
{
var objradio = objValue.form.elements[objValue.name];
var one_selected=false;
for(var r=0;r < objradio.length;r++)
{
if(objradio[r].checked)
{
one_selected=true;
break;
}
}
if(false == one_selected)
{
if(!strError || strError.length ==0)
{
strError = "please select one option from "+objValue.name;
}
sfm_show_error_msg(strError,objValue);
}
return one_selected;
}

function validateInput(strValidateStr,objValue,strError)
{
var ret = true;
var epos = strValidateStr.search("=");
var command = "";
var cmdvalue = "";
if(epos >= 0)
{
command = strValidateStr.substring(0,epos);
cmdvalue = strValidateStr.substr(epos+1);
}
else
{
command = strValidateStr;
}
switch(command)
{
case "req":
case "required":
{
ret = TestRequiredInput(objValue,strError)
break;
}//case required
case "maxlength":
case "maxlen":
{
ret = TestMaxLen(objValue,cmdvalue,strError)
break;
}//case maxlen
case "minlength":
case "minlen":
{
ret = TestMinLen(objValue,cmdvalue,strError)
break;
}//case minlen
case "alnum":
case "alphanumeric":
{
ret = TestInputType(objValue,"[^A-Za-z0-9]",strError,
objValue.name+": Only alpha-numeric characters allowed ");
break;
}
case "alnum_s":
case "alphanumeric_space":
{
ret = TestInputType(objValue,"[^A-Za-z0-9\\s]",strError,
objValue.name+": Only alpha-numeric characters and space allowed ");
break;
}
case "num":
case "numeric":
{
ret = TestInputType(objValue,"[^0-9]",strError,
objValue.name+": Only digits allowed ");
break;
}
case "dec":
case "decimal":
{
ret = TestInputType(objValue,"[^0-9\.]",strError,
objValue.name+": Only numbers allowed ");
break;
}
case "alphabetic":
case "alpha":
{
ret = TestInputType(objValue,"[^A-Za-z]",strError,
objValue.name+": Only alphabetic characters allowed ");
break;
}
case "alphabetic_space":
case "alpha_s":
{
ret = TestInputType(objValue,"[^A-Za-z\\s]",strError,
objValue.name+": Only alphabetic characters and space allowed ");
break;
}
case "email":
{
ret = TestEmail(objValue,strError);
break;
}
case "lt":
case "lessthan":
{
ret = TestLessThan(objValue,cmdvalue,strError);
break;
}
case "gt":
case "greaterthan":
{
ret = TestGreaterThan(objValue,cmdvalue,strError);
break;
}//case greaterthan
case "regexp":
{
ret = TestRegExp(objValue,cmdvalue,strError);
break;
}
case "dontselect":
{
ret = TestDontSelect(objValue,cmdvalue,strError)
break;
}
case "dontselectchk":
{
ret = TestDontSelectChk(objValue,cmdvalue,strError)
break;
}
case "shouldselchk":
{
ret = TestShouldSelectChk(objValue,cmdvalue,strError)
break;
}
case "selone_radio":
{
ret = TestSelectOneRadio(objValue,strError);
break;
}
}//switch
return ret;
}
function VWZ_IsListItemSelected(listname,value)
{
for(var i=0;i < listname.options.length;i++)
{
if(listname.options[i].selected == true &&
listname.options[i].value == value)
{
return true;
}
}
return false;
}
function VWZ_IsChecked(objcheck,value)
{
if(objcheck.length)
{
for(var c=0;c < objcheck.length;c++)
{
if(objcheck[c].checked == "1" &&
objcheck[c].value == value)
{
return true;
}
}
}
else
{
if(objcheck.checked == "1" )
{
return true;
}
}
return false;
}
/*
Copyright (C) 2003-2009 JavaScript-Coder.com . All rights reserved.
*/

P.S: Nu am reusit sa fac in asa fel incat daca checkbox-urile nu sunt bifate sa returneze un mesaj, si sa nu mearga mai departe, din cauza validatorului, si, nu am timp sa ma uit mai indetaliat...

Separat merge: dar daca integrez codurile de acolo in contact form, nu au niciun efect. Intra in conflict cu validatorul...

Te-ai inregistrat? Ne-ar placea sa te prezinti.

Cum pot sustine forumul?
Cumpara de la eMag folosind acest link.
--------------------
oG2BN9d.gifse1WdXd.gifQG6MtmI.gifRHYjDzD.gifG5p1wui.gif

Posted


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">

<head>

<title>Validating User Input.</title>

<style type="text/css">

<!--


label,input {

        display: block;

        width: 150px;

        float: left;

        margin-bottom: 10px;

}


label {

        text-align: right;

        width: 75px;

        padding-right: 20px;

}


br {

        clear: left;

}


-->

</style>


</head>

<body>

<h3>* denotes required field!</h3>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

<p>

<label for="name">Name</label>

<input id="name" type="text" name="userName" maxlength="25" />*<br />


<label for="address">Address</label>

<input id="address" type="text" name="userAddress" maxlength="100" /><br />


<label for="city">City</label>

<input id="city" type="text" name="userCity" maxlength="25" /><br />


<label for="zip">Zip</label>

<input id="zip" type="text" name="userZip" maxlength="5" /><br />


<label for="email">Email</label>

<input id="email" type="text" name="userEmail" maxlength="50" />*<br />


<label for="submit">Submit</label>

<input id="submit" type="submit" value="Mail It!" /><br />

</p>

</form>


<?php

  /**

  * This function can be used to check the sanity of variables

  *

  * @access private

  *

  * @param string $type  The type of variable can be bool, float, numeric, string, array, or object

  * @param string $string The variable name you would like to check

  * @param string $length The maximum length of the variable

  *

  * return bool

  */


  function sanityCheck($string, $type, $length){


  // assign the type

  $type = 'is_'.$type;


  if(!$type($string))

    {

    return FALSE;

    }

  // now we see if there is anything in the string

  elseif(empty($string))

    {

    return FALSE;

    }

  // then we check how long the string is

  elseif(strlen($string) > $length)

    {

    return FALSE;

    }

  else

    {

    // if all is well, we return TRUE

    return TRUE;

    }

}



 /**

  * This function if the $_POST vars are set 

  *

  * @access private

  *

  * return bool

  */

  function checkSet(){

  return isset($_POST['userName'], $_POST['userAddress'], $_POST['userCity'], $_POST['userZip'], $_POST['userEmail']);

}


  /**

  * This function checks a number is greater than zero

  * and exactly $length digits. returns TRUE on success.

  *

  * @access private

  *

  * @param int $num The number to check

  * @param int $length The number of digits in the number

  *

  * return bool

  */

  function checkNumber($num, $length){

  if($num > 0 && strlen($num) == $length)

        {

        return TRUE;

        }

}


  /**

  * This function checks if an email address in a valid format

  *

  * @access private

  *

  * @param string $email The email address to check

  *

  * return bool

  */


function checkEmail($email){

  return preg_match('/^\S+@[\w\d.-]{2,}\.[\w]{2,6}$/iU', $email) ? TRUE : FALSE;

}



  // check all our variables are set

  if(checkSet() != FALSE)

        {

        // check the POST variable userName is sane, and is not empty

        if(empty($_POST['userName'])==FALSE && sanityCheck($_POST['userName'], 'string', 25) != FALSE)

                {

        //If all is well we can  assign the value of POST field to a variable

                $userName = $_POST['userName'];

                }

        else

                {

        // if all is not well, we echo an error and exit the script

                echo 'Username is not set';

                exit();

                }

        // here we test for the sanity of userAddress, we dont need to stop the

        // the script if it is empty as it is not a required field.

        if(sanityCheck($_POST['userAddress'], 'string', 100) != FALSE)

                {

        // if all is well we assign the userAddress to a variable

                $userAddress = $_POST['userAddress'];

                }

        else

                {

        // if all is not well, we simply give the userAddress a blank value

                $userAddress = '';

                }

        // here we test for the sanity of userCity, we dont need to stop the

        // the script if it is empty as it is not a required field.

        if(sanityCheck($_POST['userCity'], 'string', 25) != FALSE)

                {

        // again we assign the POSTed value to a variable

                $userCity = $_POST['userCity'];

                }

        else

                {

        // or give the variable a blank value

                $userCity = '';

                }

        // check the sanity of the number and that it is greater than zero and 5 digits long

        if(sanityCheck($_POST['userZip'], 'numeric', 5) != FALSE && checkNumber($_POST['userZip'], 5) == TRUE)

                {

        // if the number is valid, we assign it to a variable

                $userZip = $_POST['userZip'];

                }

        else

                {

        // or give the variable a blank value

                $userZip='';

                }

    // check the sanity of the userEmail sent from the form

       if(sanityCheck($_POST['userEmail'], 'string', 5) != FALSE && checkEmail($_POST['userEmail']) != FALSE)

                {

        // if the checks are ok for the email we assign the email address to a variable

                $userEmail = $_POST['userEmail'];

                }

        else

                {

        // if all is not well we echo an error message

                echo 'Invalid Email Address Supplied';

        // and exit the script

                exit();

                }


    // Connect to the MySQL

    $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

    if (!$link)

            {

            die('Not connected : ' . mysql_error());

            }


    // select test as the current db

    $db_selected = mysql_select_db('test', $link);

    if (!$db_selected)

            {

            die ("Database not selected : " . mysql_error());

            }


    // Build our query here and check each variable with mysql_real_escape_string()

    $query = sprintf("INSERT INTO people (userName, userAddress, userCity, userZip, userEmail)

            VALUES( '%s', '%s','%s','%s','%s')",

            mysql_real_escape_string($userName),

            mysql_real_escape_string($userAddress),

            mysql_real_escape_string($userCity),

            mysql_real_escape_string($userZip),

            mysql_real_escape_string($userEmail));


    // run the query

    if(!mysql_query($query))

            {

            echo 'Query failed '.mysql_error();

            exit();

            }

      else

            {

        // if all is well we mail off a little thank you email. We know it is

        // safe to do so because we have validated the email address.

            $subject = 'Submission';

            $msg= 'Thank you for submitting your information';

            if(!mail($userEmail,$subject,$msg, "From: $userEmail\nReply-To: $userEmail\nX-Mailer: PHP/" . phpversion()))

            {

            echo 'Unable to send confirmation mail';

            }

        else

            {

            echo 'Thank you for your submission, a confirmation email has bee sent to '.$userEmail;

            }

        }

    }

  else

        {

        // this will be the default message if the form accessed without POSTing

        echo '<p>please fill in the form above</p>';

        }



?>


</body>

</html>

ptr cele doua checkbox-uri faci la fel ca pentru validarea numelui...inlocuiesti doar campurile.

  • Upvote 1
  • Downvote 1

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.