How to validate value entered in text field with php?

Asked

Viewed 319 times

3

I have a project, which consists of pulling a code inserted by the user in the following field

<input type=text name=CODNOME><br>

he putting the following value 001 appears an msg

<?php $CODNOME = '001' echo " testetes"; ?>

I know I’m lost in logic someone could guide me what I should do or where to look?

  • This message will appear where? The form is redirected when the user enters the code? Give more details if possible.

  • Valter the user would remain on the page only with a refresh to validate and bring the message corresponding to the code entered example if the user type (001) will appear the message " A " if he type (002) will appear the message " B " that would be the logic I’m trying to use.

2 answers

3


Simple:

<?php 

if($_POST['CODNOME'] == "001"){
  $msg = "mensagem1";
} 

else if(($_POST['CODNOME'] == "002"){
  $msg = "mensagem2";
}

else {
  $msg = "Opção inválida";
}

echo $msg;
?>

You can also use the switch:

<?php 

switch($_POST['CODNOME']){
  case "001"
  $msg = "mensagem1";
  break;

  case "002"
  $msg = "mensagem2";
  break;

  default:
  $msg = "Opção inválida";
  break;
} 
echo $msg;
?>
  • Andrei Coelho is facing a difficulty below the CODNOME condition, this right because when I test the string error, <form action="Cod.php" method="post"> <Nav class="zz z_meio2 edge "> enter the code <br> <input type=text name=CODNAME><br> <input type=Submit value="OK"> </Nav>

  • @Luccascerqueira did not understand... It went wrong?

  • @Luccascerqueira put "quotes" in the CODNOME. Like this: <input type="text" name="CODNOME">

  • @Luccascerqueira post error here in the comments

  • @Luccascerqueira or edit your question by placing the whole code at the end of it. So I can see exactly where the error is.

  • "Parse error: syntax error, Unexpected '{' in /home/teste_home/public_html/test/imprimir3_new.php on line 204"

  • @Luccascerqueira posts all this file "imprimir3_novos.php" AT THE END OF YOUR QUESTION. So I can see where the error is.

  • @ja sei... The guilerme edited my answer wrong. I will edit it, copy and paste.

  • @Luccascerqueira is there! I changed the code. Now it will work.

Show 4 more comments

1

To validate in PHP use the function preg_match, for example:

<?php
// The "i" after the pattern delimiter indicates a case-insensitive search
if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
} else {
    echo "A match was not found.";
}
?>

She returns 1 if valid, 0 if it is invalid or false if any error occurs

The first parameter of the function is the regex and the second the string, in its case the value sent by the user

Now just need to create a regex to validate, to test regular expressions recommend this website

  • Sorry, I do not understand, I have little knowledge of php. the premise of my logic and only replace the given code by the user with an msg without using link or external search preference without accessing the database if the user type (001) will appear the message " A " if he type (002) will appear the message " B " , that would be the logic I’m trying to use. I think it’s clear but.

  • 1

    Now I understand with the title "validate value", I thought I wanted to validate something, in this case Andrei Coelho already answered how can do, recommend switch case

Browser other questions tagged

You are not signed in. Login or sign up in order to post.