I need to execute a javascript code inside a PHP code. The goal is that it works like this: when the user clicks on the REGISTER button on the form, will be executed the javascript function called JSF(), which should make a first validation of the data entered into the form. If everything is OK, will submit and execute a second validation by the PHP function called send(). If everything is OK again, will proceed with the user login ...

During the validation, if you find any mistake, the functions will generate a message that is printed in the frmRegisterError field. For the javascript code to direct the error message in the desired field I've made ​​as follows:

Code:
var frmRegisterError = document.getElementById("frmRegisterError"); 
frmRegisterError.value = "TYPE YOUR USERNAME";
Everything I've read so far, says that with PHP it's impossible redirect your message error to a specific field of the form, then the solution was to mix PHP with javascript. Just to test if it would work I did as follows:

HTML Code:
<form method="post" action="teste.php?function=send" id="frmRegister" name="frmRegister" accept-charset="utf-8"> 
<label>Nombre de usuario: </label> 
<input type="text" id="frmRegisterUN" value="" name="frmRegisterUN" /> 
<br /> 
<label>Se encontro un error: </label> 
<input type="text" value="" id="frmRegisterError" name="frmRegisterError"/> 
<br /> 
<input type="button" value="REGISTRAR" id="frmRegister_Button"/ onClick="JSF ()"> 
</form>
Code:
function JSF () 
{ 
document.getElementById("frmRegister").submit(); 
}
PHP Code:
<?php  
if($_GET['function'] == "send")  
{  
    
send();  
}  

function 
send ()  
{  
    if (isset(
$_POST))  
    {  
        
$usuario $_POST['frmRegisterUN'];  
        if (
$usuario != "")  
        {  
            
?>  
            <script>  
            document.write("TYPE YOUR USERNAME");  
            </script>  
      <?php  
        
}  
        else  
        {  
            echo 
"TYPE YOUR USERNAME";  
        }  
    }  
}  
?>
Thus, this combination of PHP with javascript worked perfectly. Both printed message as I want. Since the code worked out that way, I think all I need to do is replace the javascript code inside PHP to print the error message inside the frmRegisterError field. For this, I changed the javascript code like this:

PHP Code:
<?php  
function send ()  
{  
    if (isset(
$_POST))  
    {  
        
$usuario $_POST['frmRegisterUN'];  
        if (
$usuario != "")  
        {  
            
?>  
      <script>  
            var error = document.getElementById("frmRegisterError");  
            error.value = "TYPE YOUR USERNAME";  
            </script>  
      <?php  
        
}  
        else  
        {  
            echo 
"TYPE YOUR USERNAME";  
        }  
    }  
}  
?>
Theoretically supposed to have worked, but I don't know for what reason it did not work that way! After testing a few ways I came to the following conclusion: when you have a javascript code inside the PHP code I can't use getElementById, because it will not work. To make sure that this was what was happening, I did the following test:

Code:
function JSF () 
{ 
var error = document.getElementById("frmRegisterUN").value; 
alert (error); 
document.getElementById("frmRegister").submit(); 
}
PHP Code:
<?php  
if($_GET['function'] == "send")  
{  
    
send();  
}  

function 
send ()  
{  
    if (isset(
$_POST))  
    {  
        
$usuario $_POST['frmRegisterUN'];  
        if (
$usuario != "")  
        {  
            
?>  
      <script>  
            var error = document.getElementById("frmRegisterUN").value;  
            alert (error);  
            </script>  
      <?php  
        
}  
        else  
        {  
            echo 
"TYPE YOUR USERNAME";  
        }  
    }  
}  
?>
Thus, if the user enters the correct user name, the two functions should display an error message with a alert window, but this message appears only once! I noticed that when the page executes the JSF() the message is displayed normally. However, when it's running the function send() the message is not displayed as it was expected. I believe that after running the code on the line with getElementById the code execution is stopped, as if there was some error in the script.


Does anyone know how to solve this problem?