www.webdeveloper.com
Results 1 to 2 of 2

Thread: [RESOLVED] How do I submit feedback PHP validation for the value of an input?

Hybrid View

  1. #1
    Join Date
    Jul 2013
    Posts
    6

    resolved [RESOLVED] How do I submit feedback PHP validation for the value of an input?

    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 Code:
    <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 Code:
    <?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?

  2. #2
    Join Date
    Jul 2013
    Posts
    6

    resolved

    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.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles