PDA

View Full Version : Validation rule in PHP doesn't seem to be working


txusm
02-04-2006, 12:39 PM
Hello everybody:

I have a registration form in Flash that sends the data to a PHP file which in turn sends it to a database. The problem is that I have two validation rules for the name (can't be less than 3 characters) and the email ([email protected]). When I insert the data the PHP file has to check if the rules are complied with and if not returns a variable to Flash in order to display a message saying "your data couldn't be inserted in our database". Please have a look at both the PHP and ActionScript code below and let me know what I'm doing wrong with the rules or maybe it's something else I can't figure out.

PHP:

<?php
$conexion=@mysql_connect(localhost,root,"Mckako01") or die (mysql_error());
$select=@mysql_select_db("formulario");
//reglas para validar nombre, email

$valNombre='/^[a-z]{3,}$/i';
$valEmail='/^\w[-.\w]*@([-a-z0-9]+\.)+[a-z]{2,4}$/i';

//recibir variables
$nombre=$_POST['nombre'];
$apellido=$_POST['apellido'];
$direccion=$_POST['direccion'];
$email=$_POST['email'];
$fuente=$_POST['fuente'];
$comentarios=$_POST['comentarios'];
$noticias=$_POST['noticias'];
//insert data only if validation rule is ok
if(preg_match($valNombre,$nombre) && preg_match($valEmail,$email) ){
$insertar="INSERT INTO datos (nombre,apellido,direccion,email,fuente,comentario s,noticias) VALUES ('$nombre','$apellido','$direccion','$email','$fue nte','$comentarios','$noticias')";
$consulta=@mysql_query($insertar);
echo "&exito=true";
}else{
echo "&exito=false";
}


?>

txusm
02-04-2006, 12:42 PM
sorry, I couldn't attach the file, is too big:(

Billy T
02-04-2006, 12:48 PM
you have a mysql column called comentario s?

txusm
02-04-2006, 12:50 PM
you have a mysql column called comentario s?

no, actually I don't, I left it there to put it in the future

txusm
02-04-2006, 12:54 PM
you have a mysql column called comentario s?

Actually I do!, I was looking at the wrong db...:eek: but not with the space before the "s", that was a typo...

txusm
02-04-2006, 01:22 PM
I can see that the ActionScript didn't upload in the first place...

var enviar:LoadVars = new LoadVars();
enviar.onLoad = function(){
if(this.exito=="true"){
mensajeTxt.text="Sus datos han sido enviados correctamente a nuestra base de datos";
}else{
mensajeTxt.text="Sus datos no han sido enviados";
}
}
enviar.nombre=nameTxt.text;
enviar.apellido=surnameTxt.text;
enviar.direccion=addressTxt.text;
enviar.email=emailTxt.text;

enviar.fuente=myCb.value;
if(myCheckbox.selected){
enviar.noticias=true;
}else{
enviar.noticias=false;
}
enviar.comentarios=commentTxt.text;

enterBtn.onRelease=function(){
enviar.sendAndLoad("insertar.php",enviar,"POST");
}

txusm
02-04-2006, 05:10 PM
Hello everybody:

I have a registration form in Flash that sends the data to a PHP file which in turn sends it to a database. The problem is that I have two validation rules for the name (can't be less than 3 characters) and the email ([email protected]). When I insert the data the PHP file has to check if the rules are complied with and returns a variable (true or false) to Flash in order to display a success or failure message . The thing is that no matter what I do I always get the failure message and the data is not sent to the db. Please have a look at both the PHP and ActionScript code below and let me know what I'm doing wrong with the rules or maybe it's something else I can't figure out cause I will have to repaint the wall in my room if I don't stop banging my head against it...I have uploaded the code in a txt file

txusm
02-04-2006, 08:31 PM
Hello again,

maybe I need to reformulate my question again because the validation rules seem to be ok...so with the code below, why doesn't the data load into the database. The PHP file seems to be sending the variable "false" to Flash.

$conexion=@mysql_connect(localhost,root,"Mckako01" ) or die (mysql_error());
$select=@mysql_select_db("formulario");
//reglas para validar nombre, email

$valNombre='/^[a-z]{3,}$/i';
$valEmail='/^\w[-.\w]*@([-a-z0-9]+\.)+[a-z]{2,4}$/i';

//recibir variables
$nombre=$_POST['nombre'];
$apellido=$_POST['apellido'];
$direccion=$_POST['direccion'];
$email=$_POST['email'];
$fuente=$_POST['fuente'];
$comentarios=$_POST['comentarios'];
$noticias=$_POST['noticias'];
//insert data only if validation rule is ok
if(preg_match($valNombre,$nombre) && preg_match($valEmail,$email) ){
$insertar="INSERT INTO datos (nombre,apellido,direccion,email,fuente,comentario s,noticias) VALUES ('$nombre','$apellido','$direccion','$email','$fue nte','$comentarios','$noticias')";
$consulta=@mysql_query($insertar);
echo "&exito=true";
}else{
echo "&exito=false";
}


?>

actionscript:
var enviar:LoadVars = new LoadVars();
enviar.onLoad = function(){
if(this.exito=="true"){
mensajeTxt.text="Sus datos han sido enviados correctamente a nuestra base de datos";
}else{
mensajeTxt.text="Sus datos no han sido enviados";
}
}
enviar.nombre=nameTxt.text;
enviar.apellido=surnameTxt.text;
enviar.direccion=addressTxt.text;
enviar.email=emailTxt.text;

enviar.fuente=myCb.value;
if(myCheckbox.selected){
enviar.noticias=true;
}else{
enviar.noticias=false;
}
enviar.comentarios=commentTxt.text;

enterBtn.onRelease=function(){
enviar.sendAndLoad("insertar.php",enviar,"POST");
}

Million thanks,:confused:

txusm