Logo Hardware.com.br
Reginaldo Feitoza
Reginaldo Fe... Novo Membro Registrado
18 Mensagens 0 Curtidas

Ajuda em formulario php, definir campos obrigatorios

#1 Por Reginaldo Fe... 16/06/2008 - 10:34
";
if (count($_GET) >0) {
reset($_GET);
while(list($key, $val) = each($_GET)) {
$GLOBALS[$key] = $val;
if (is_array($val)) {
$w4fMessage .= "$key: ";
foreach ($val as $vala) {
$vala =stripslashes($vala);
$w4fMessage .= "$vala, ";
}
$w4fMessage .= "
";
}
else {
$val = stripslashes($val);
if (($key == "Submit") || ($key == "submit")) { }
else { if ($val == "") { $w4fMessage .= "$key: -
"; }
else { $w4fMessage .= "$key: $val
"; }
}
}
} // end while
}//end if
else {
reset($_POST);
while(list($key, $val) = each($_POST)) {
$GLOBALS[$key] = $val;
if (is_array($val)) {
$w4fMessage .= "$key: ";
foreach ($val as $vala) {
$vala =stripslashes($vala);
$w4fMessage .= "$vala, ";
}
$w4fMessage .= "
";
}
else {
$val = stripslashes($val);
if (($key == "Submit") || ($key == "submit")) { }
else { if ($val == "") { $w4fMessage .= "$key: -
"; }
else { $w4fMessage .= "$key: $val
"; }
}
}
} // end while
}//end else
$w4fMessage = "".$w4fMessage."
Responder
Desbravador_
Desbravador_ Membro Senior Registrado
164 Mensagens 5 Curtidas
#2 Por Desbravador_
16/06/2008 - 14:20
Uma dica:

Visite uma página de email gratis.Ex: www.ibest.com.br

Depois clique ni link para criar uma conta de emial.
Quando a página carregar, clique com o botão direito do mause "exibir código fonte" e então vc salva e estuda o código fonte.

Pois nessa página, se vc deixar de preencher um campo, daí aparece um aviso de campo obrigatório.
OoDragooNoO
OoDragooNoO Geek Registrado
3.5K Mensagens 61 Curtidas
#3 Por OoDragooNoO
16/06/2008 - 15:55
1 - coloca o(s) teu(s) codigo(s) entre as tags php, se nao fica impossivel de ler :S 2 - pra ti definir um campo como "obrigatorio", basta tu nao realizar nenhuma açao ateh que os campos necessarios estejam preenchidos exemplo: index.php
  
<form action="algo.php" method="post">
nome : <input type="text" name="nome"><br />
telefone : <input type="text" name="telefone"><br />
email : <input type="text" name="email"><br />
<input type="submit" value="Cadastrar">
acima eu fiz um formulario MEGA simples, soh pra exemplo feliz.png a pagina que trata os dados vem abaixo :
algo.php
[php] if(empty($_POST['nome']) || empty($_POST['email']))
{
echo "Os campos nome e email sao obrigatorios";
exit();
}
else
{
cadastra();
}
[/php]nao vou me ater à funcao de cadastrar ou ateh mesmo como cadastrar, pois nao eh esse o foco da pergunta wink.png

como pode ver, no meu exemplo acima, o nome e email sao obrigatorios, telefone nao eh, portanto, enquanto a pessoa nao informar nome E email, o cadastro nao acontece feliz.png

referencia das funçoes :
empty : http://br2.php.net/manual/pt_BR/function.empty.php
exit : http://br2.php.net/manual/pt_BR/function.exit.php

flw xD/
DragaO~
Reginaldo Feitoza
Reginaldo Fe... Novo Membro Registrado
18 Mensagens 0 Curtidas
#4 Por Reginaldo Fe...
16/06/2008 - 22:04
";
if (count($_GET) >0) {
reset($_GET);
while(list($key, $val) = each($_GET)) {
$GLOBALS[$key] = $val;
if (is_array($val)) {
$w4fMessage .= "$key: ";
foreach ($val as $vala) {
$vala =stripslashes($vala);
$w4fMessage .= "$vala, ";
}
$w4fMessage .= "
";
}
else {
$val = stripslashes($val);
if (($key == "Submit") || ($key == "submit")) { }
else { if ($val == "") { $w4fMessage .= "$key: -
"; }
else { $w4fMessage .= "$key: $val
"; }
}
}
} // end while
}//end if
else {
reset($_POST);
while(list($key, $val) = each($_POST)) {
$GLOBALS[$key] = $val;
if (is_array($val)) {
$w4fMessage .= "$key: ";
foreach ($val as $vala) {
$vala =stripslashes($vala);
$w4fMessage .= "$vala, ";
}
$w4fMessage .= "
";
}
else {
$val = stripslashes($val);
if (($key == "Submit") || ($key == "submit")) { }
else { if ($val == "") { $w4fMessage .= "$key: -
"; }
else { $w4fMessage .= "$key: $val
"; }
}
}
} // end while
}//end else
$w4fMessage = "".$w4fMessage."
Reginaldo
Barton_Simpson
Barton_Simps... Veterano Registrado
1K Mensagens 18 Curtidas
#5 Por Barton_Simps...
17/06/2008 - 00:43
Isso de olhar código fonte você pode achar uma verificação em javascript.
Verificar erro em js ajuda na rapidez, mas não dependa disso nunca, você precisa de verificação feita pelo servidor (.net , php , jsp , ...), não tem como controlar se a pessoa (ou robô) que vai enviar o post pro seu site vai mesmo exucar o javascript...


Um coisa legal é fazer a página em PHP enviar o form para se mesma, form2mail.php

caso tenha erro mostra o form novamente mas JÁ preenchido (faz um include);
caso esteja tudo ok faz o cadastro e aí redireciona com o header(Location: )
Pentium 100s || Intel i430SX || 32MB EDORAM || vídeo offboard SiS630 Imagem

"Quando todos os especialistas estão de acordo, aí que a cautela é impresindível." B. Russell
OoDragooNoO
OoDragooNoO Geek Registrado
3.5K Mensagens 61 Curtidas
#6 Por OoDragooNoO
19/06/2008 - 10:16
";
if (count($_GET) >0) {
reset($_GET);
while(list($key, $val) = each($_GET)) {
$GLOBALS[$key] = $val;
if (is_array($val)) {
$w4fMessage .= "$key: ";
foreach ($val as $vala) {
$vala =stripslashes($vala);
$w4fMessage .= "$vala, ";
}
$w4fMessage .= "
";
}
else {
$val = stripslashes($val);
if (($key == "Submit") || ($key == "submit")) { }
else { if ($val == "") { $w4fMessage .= "$key: -
"; }
else { $w4fMessage .= "$key: $val
"; }
}
}
} // end while
}//end if
else {
reset($_POST);
while(list($key, $val) = each($_POST)) {
$GLOBALS[$key] = $val;
if (is_array($val)) {
$w4fMessage .= "$key: ";
foreach ($val as $vala) {
$vala =stripslashes($vala);
$w4fMessage .= "$vala, ";
}
$w4fMessage .= "
";
}
else {
$val = stripslashes($val);
if (($key == "Submit") || ($key == "submit")) { }
else { if ($val == "") { $w4fMessage .= "$key: -
"; }
else { $w4fMessage .= "$key: $val
"; }
}
}
} // end while
}//end else
$w4fMessage = "".$w4fMessage."
DragaO~
Responder Tópico
© 1999-2024 Hardware.com.br. Todos os direitos reservados.
Imagem do Modal