PHP - how to use placeholders for form validation messages

-3

Hello, I'm using placeholders to send the error messages and success of submitting the form within the inputs themselves. Placeholders are gray. When the form is sent, if there are any errors, the error msg appears through the red placeholder. Message exchange is working, but the placeholder CSS style is not.

My code has PHP in the header where I style the placeholders in gray in the first place. Then, further down, in the body, another PHP code does form validation. I think it's the PHP header that is creating the error, possibly messing around with the variables. Is it because the two parts of the code are separate?

Can you give me a hand?

No header:

<?php
   $phname = 'Please, inform your name...';
   $phemail = '...and e-mail';
   $styleName = '.namecolor::-webkit-input-placeholder {color:blue}';
   $styleEmail = '.emailcolor::-webkit-input-placeholder {color:inherit}';
   $phstyle = '<style>'.$styleName.$styleEmail.'</style>';

  echo $phstyle;
?>

Comes the body and then:

<?php //PHP FOR CONTACT     FORM===============================================================
    if(!empty($_POST['bt-submit'])) {                   
        function test_input($data) {
          $data = trim($data);
          $data = stripslashes($data);
          $data = htmlspecialchars($data);
          return $data;
        }

        $destino = "[email protected]";
        $assunto = "Msg from Decoranno website";                        
        $msgErr = "";
        $nome = $de = $mensagem = "";
        $erro = FALSE;

        if ($_SERVER["REQUEST_METHOD"] == "POST")
        {

          if (empty($_POST["txtnome"]))
            {$phname = 'Please inform your name';
            $erro = TRUE;
            $styleName = '.namecolor::-webkit-input-placeholder {color:red}';
            }
          else
            {$nome = test_input($_POST["txtnome"]);
            if (!preg_match("/^[a-zA-Z ]*$/",$nome)) {
                $phname = 'Use only characters and white spaces';
                $_POST['txtnome'] = "";
                $erro = TRUE;
                $styleName = '.namecolor::-webkit-input-placeholder {color:red}';
            }
        }

      if (empty($_POST["txtdest"]))
        {$phemail = 'Please inform your email address';
        $erro = TRUE;
        $styleEmail = '.emailcolor::-webkit-input-placeholder {color:inherit}';
        }
      else
        {$de = test_input($_POST["txtdest"]);
         if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$de)){
             $phemail = 'Sorry, invalid email address';
             $_POST['txtdest'] = "";
             $erro = TRUE;
             $styleEmail = '.emailcolor::-webkit-input-placeholder {color:inherit}';
             }
        }

      if (empty($_POST["txtmsg"]))
        {$msgErr = "Please type your message";
        $erro = TRUE;
        }
      else
        {
            $mensagem = htmlspecialchars_decode(test_input($_POST["txtmsg"]));
            $msgErr = $_POST["txtmsg"]; //Repeat the message already written by the user in case of retry.
        }


        if( $erro == FALSE ){
            if (PATH_SEPARATOR ==":") {
            $quebra = "\r\n";
            } else {
                $quebra = "\n";
                }
            $msgcompleta = "From: ".$nome."<".$de.">".$quebra.$quebra.$mensagem;
            $headers = "MIME-Version: 1.1".$quebra;
            $headers .= "Content-type: text/plain; charset=iso-8859-1".$quebra;
            $headers .= "From: ".$de.$quebra; //E-mail do remetente
            $headers .= "Return-Path: ".$de.$quebra; //E-mail do remetente
            mail($destino, $assunto, $msgcompleta, $headers, "-r". "[email protected]");
            //print "Message successfully sent. Thank you." and reset placeholders on email and name inputs to blank
            $_POST['txtnome'] = $_POST['txtdest'] = $_POST['txtmsg'] = ''; $phname = $phemail = ''; // lets pretend nothing was posted
            $msgErr = 'Message sucessfully sent. Thank you.';
            }
    }
  }
//END OF PHP FOR CONTACT FORM=================================================?>

            <form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
                <span>Name</span><input class="text-input namecolor" name="txtnome" type="text" value="<?php echo $_POST['txtnome']; ?>" placeholder="<?php echo $phname; ?>" />
                <span>Email</span><input class="text-input emailcolor" name="txtdest" type="text" value="<?php echo $_POST['txtdest']; ?>" placeholder="<?php echo $phemail; ?>" />
                <textarea name="txtmsg msgcolor" placeholder="We are looking forward to hearing from you"><?php echo $msgErr;?></textarea>
                <input id="bt-send" name="bt-submit" type="submit" value="Send your message" />
            </form>
    
asked by anonymous 01.09.2015 / 16:52

2 answers

0

Php is a structured language. If at the top you define one value and then define another one, the last CASE THAT LAST will prevail. Make sure the variable $ _POST ["txtdest"] actually goes blank. Instead of using empty() function make conditions of type if ($_POST["txtdest"] == '')) for input. Because sometimes the header sends some value in the "empty" inputs.

    ...

    if ($_POST["txtdest"] == '')
            {$phemail = 'Please inform your email address';
            $erro = TRUE;
            $styleEmail = '.emailcolor::-webkit-input-placeholder {color:inherit}'; //AQUI NÃO DEVERIA SER RED?????
            }
          else
            {$de = test_input($_POST["txtdest"]);
             if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$de)){
                 $phemail = 'Sorry, invalid email address';
                 $_POST['txtdest'] = "";
                 $erro = TRUE;
                 $styleEmail = '.emailcolor::-webkit-input-placeholder {color:inherit}'; //AQUI NÃO DEVERIA SER RED?????
                 }
            }

     ...
    
01.09.2015 / 19:54
0

Maybe the problem is that the color of the attribute is not catching because of the bootstrap priority, you can use "! important" so that no css interferes with the color of the element:

::-webkit-input-placeholder { color:#ff0000!important; }
input:-moz-placeholder { color:#ff0000!important; }
    
01.09.2015 / 22:37