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 [B]JSF()[B]send()
During the validation, if you find any mistake, the functions will generate a message that is printed in the [B]frmRegisterError
[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:
[code=html]<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();
}
[code=php]<?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 [B]frmRegisterError
[code=php]<?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 [B]getElementById
[code]function JSF ()
{
var error = document.getElementById(“frmRegisterUN”).value;
alert (error);
document.getElementById(“frmRegister”).submit();
}
[code=php]<?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 [B]JSF()[B]send()[B]getElementById
[b]Does anyone know how to solve this problem?