Well, i need to check if some fields are empty when submiting get an echo "fail";
For now i have this:
$campos = array('nome', 'email');
foreach ($campos as $a) {
$$a = $_REQUEST[$a];
if(isset($$a) && $$a != ""){
$post_vars = array('iphone3g1', 'iphone3g2', 'nome', 'iphone41', 'postal');
$post_values = array();
foreach($post_vars as $var) {
$post_values[$var] = "'" . mysql_real_escape_string($_POST[$var]). "'";
}
$sql = "INSERT INTO clientes (" . implode(',', array_keys($post_values)) . ") VALUES (" . implode(',', array_values($post_values)) . ")";
$query = mysql_query($sql);
if($query==TRUE){
$to=$apelido;
$subject="iRepairApple";
$header="from: iRepairApple <geral@iRepairApple.com> ";
$message="Caro $nome , obrigado por preferir a iRepairApple! \r\n\n";
$message.="O seu pedido de reparação foi registado. \r\n";
$message.="Modelo p/ Reparação: $modelo; \r\n \n\n Deverá proceder à entrega do equipamento numa das nossas lojas (Ver Moradas) \r\n";
$message.="O seu código de reparação é o : \n $codigounico; \n\n Ao contactar a iRepairApple faça sempre referência a este código.";
$message.="\n Com os melhores cumprimentos, \n A equipa iRepairApple.";
$enviado开发者_高级运维 = mail($to,$subject,$message,$header);
header('Location: ../index.php?mensagem=2');}
}else{
echo ("error");
}
}
?>
(Would be the same as !empty)
It works, if none of the fields is filled it will retrieve the error, but if ONLY one is fulfilled it will tell it is ok :(. I will later put a header where i have echo ("error");
So what you want to do is to check wheter the $_REQUEST
array contains all the fields denoted in $campos
? The relevant part of your code is missing, but it looks like the if
clause is supposed to do the check.
Assuming I'm understanding you correctly, here's one way to do it:
foreach ($campos as $a) {
if (!isset($_REQUEST[$a]) ||
($$a = $_REQUEST[$a]) == "") {
die("Field {$a} not set.");
}
}
echo "Ok.";
Note that you have to use isset()
on the $_REQUEST
array and not on the $aa
variable. Maybe you also want to use $_GET
or $_POST
instead of the $_REQUEST
superglobal.
So you just reverse the condition to check if one or more of the fields are empty and echo failure in that case.
$campos = array('nome', 'email');
foreach ($campos as $a)
{
$$a = $_REQUEST[$a];
if(!isset($$a) || $$a == "")
{
echo "fail";
exit;
}
}
精彩评论