Jump to content

How do I submit feedback PHP validation for the value of an input?


Evair_Peterson
 Share

Recommended Posts

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?
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.