Evair_Peterson Posted July 28, 2013 Share Posted July 28, 2013 Hi! I read that for security reasons, the ideal is that the form data is validated two times ever. The first on the client side with javascript code and a second time on the server side with PHP code. The validation with javascript I have (almost) ready. However, I'm having problems with PHP validation ... <html> <head> <title>Formulario</title> <script type="text/javascript"> function frmRegisterValidate (){ var frmRegisterError = document.getElementById("frmRegisterError"); var frmRegisterUN = document.getElementById("frmRegisterUN").value; var frmRegisterPW = document.getElementById("frmRegisterPW").value; if (frmRegisterUN == "") { frmRegisterError.value = "Type your username"; return false; } else if (frmRegisterPW == "") { frmRegisterError.value = "Type your password"; return false; } document.getElementById("frmRegister").submit(); } </script> </head> <body> <form method="post" action="frmRegister.php" id="frmRegister" name="frmRegister" accept-charset="utf-8"> <label>Nombre de usuario: </label> <input type="text" id="frmRegisterUN" value="" name="frmRegisterUN" /> <br /> <label>Informe una contrasena: </label> <input type="text" value="" id="frmRegisterPW" name="frmRegisterPW"> <br /> <label>Se encontro un error: </label> <input type="text" value="" id="frmRegisterError" name="frmRegisterError"/> <br /> <input type="button" value="REGISTER" id="frmRegister_Button" onClick="frmRegisterValidate ()"/> </form> </body> </html> <?php if (!empty ($_POST['frmRegisterUN'])) { $frmRegisterUN = $_POST['frmRegisterUN']; } else { $frmRegisterUN = NULL; echo "Type your username <br />"; } if (!empty ($_POST['frmRegisterPW'])) { $frmRegisterPW = $_POST['frmRegisterPW']; } else { $frmRegisterPW = NULL; echo "Type your password <br />"; } ?> This is a very simplified version of the form that I am developing. As you can see, I can do data validation in both javascript and PHP ... My question is: with the PHP code, how should I do so that the error message is displayed in the value of the input called frmRegisterError equal to that done with javascript? Quote Link to comment https://forums.phpfreaks.com/topic/280597-how-do-i-submit-feedback-php-validation-for-the-value-of-an-input/ Share on other sites More sharing options...
Evair_Peterson Posted July 30, 2013 Author Share Posted July 30, 2013 With the help of some friends from Spain and Venezuela I could solve the problem. As my goal was to change the value of the input called frmRegister with the resulting message of the validation in PHP if it found any errors, so I changed the code of this input to this: HTML Code: <input type="text" id="frmRegisterError" value="<?php if($frmRegisterError){echo $frmRegisterError;}?>" /> The reason to put the PHP validation code in another file, is that I think that is better organized and is easier to make changes if needed in the future. To solve this problem, I have included it in the head of the main page with this single line: PHP Code: <?php include '/PHP/frmRegister.php'; ?> By doing this so I have the external file as if it were part of the main page. Inside this file I put a very simple code just to see if it would work in the right way: PHP Code: <?php $frmRegisterError = ""; if (!empty($_POST)) {$frmRegisterError = "ERRO PHP";} ?> And it worked! At least for the moment the problem is solved!Hope this may help someone else with the same problem in the future!Greetings from Brazil! Evair. Quote Link to comment https://forums.phpfreaks.com/topic/280597-how-do-i-submit-feedback-php-validation-for-the-value-of-an-input/#findComment-1442645 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.