1
I have a form system where the fields are generated by the base date through select. The same is done with foreach, how can I search the base date for the required fields and see if they have been filled in? And if they are not going to validate the form and show a message. NOTE: I don’t want to use required input.
<?php
if (isset($_POST['acao'])) {
while (list ($key, $val) = each ($_POST)){
mysql_query("insert into cms_formularios_respostas(usuario,resposta,campo,formulario) values('$name','".strip_tags($val)."','$key','$row[id]')") or die(mysql_error());
}
}
?>
<form method="post" enctype="multipart/form-data" style="margin-bottom:5px">
<input type="hidden" name="acao" id="acao" value="<?php echo $row['id']; ?>" />
<?php
$get_form = mysql_query("SELECT * FROM cms_formularios_campos WHERE formulario = '$row[id]' ORDER BY ID") or die(mysql_error());
while($form = mysql_fetch_assoc($get_form)){
?><p><label class="label-form" for="<?php echo $form['id']; ?>"><?php echo $form['nome']; ?></label><br /><?php if($form['tipo'] == 'Resposta curta'){ ?><inputtype="text" name="<?php echo $form['id']; ?>" id="<?php echo $form['id']; ?>" class="input-form" /><br><?php if($form['obrigatorio'] == 'sim'){ ?><span style="color:red">* Requerido</span><?php } ?><?php } ?></p><input type="submit" class="input-form" value="Enviar" />
Can be done only with javascript can be?
– Felipe Duarte
Hi Felipe! So I didn’t want to javascript. I saw on a site the same system and it was made by php even, at the time of entering the base date checked the mandatory fields of the form if they were filled, otherwise it showed the message You left the field [FIELD NAME] blank that was required.
– user89739
Well, then it can be done on the server but it will not be in real time, every time the user submits the form, it will go to another validation page, when returning the completed data will be lost.
– Felipe Duarte
Even so, I prefer the server!
– user89739
Okay, I’ll get back to you.
– Felipe Duarte
And how should it be done to know which field is mandatory? By column
obrigatorio
? And even if you do such a check on the server, which should be done, I strongly recommend using therequired
in HTML. This will prevent the user from making multiple unnecessary requests to the server.– Woss
Yes required column (yes or no). I used required, but I still prefer to use it by php!
– user89739
@Carlosg this PHP code right at the beginning is what the request is about and should do such validation?
– Woss
@Andersoncarloswoss Yes, I thought about selecting the required fields of the form with select before foreach and using something like if(Empty($result)){ does it give?
– user89739
@Carlosg Many errors occur in your logic. You can’t just do what you want, because there are good practices in programming and usability. Validating forms through the server, after refreshing, is far from being a coherent solution, because the javascript usage does not present any kind of risk to your application and does not hinder what Voce is trying to do. If there is a reason for you to take it away, speak soon, but for now I can answer that you give up what you want to do, for usability reasons.
– Lollipop
@Carlosg does not need to edit the answer, saying it solved the problem. Leave the question as it is, with the answer already given, to help other people.
– Wallace Maxters