/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Javascript code don’t work inside a PHP code

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], 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 [B]send()[/B]. 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 [B]frmRegisterError[/B] 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”; [/code]

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]

[code]function JSF ()
{
document.getElementById(“frmRegister”).submit();
} [/code]

[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”;
}
}
}
?>[/code]

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[/B] field. For this, I changed the javascript code like this:

[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”;
}
}
}
?>[/code]

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[/B], 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();
} [/code]

[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”;
}
}
}
?>[/code]

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] the message is displayed normally. However, when it’s running the function [B]send()[/B] the message is not displayed as it was expected. I believe that after running the code on the line with [B]getElementById [/B] the code execution is stopped, as if there was some error in the script.

[b]Does anyone know how to solve this problem?[/b]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@AirbladerJul 30.2013 — 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], 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 [B]send()[/B]. If everything is OK again, will proceed with the user login ...[/quote]

Good! This is how it should work.

Everything I've read so far, says that with PHP it's impossible redirect your message error to a specific field of the form[/quote]

Nonsense!


[code=html]<input type="button" value="REGISTRAR" id="frmRegister_Button"/ onClick="JSF ()">[/code][/quote]

What's up with the additional slash after the id?

Anyway, as far as your question goes, you are making it way to hard. PHP is executed before the content is even so much as delivered to the client, therefore you can manipulate it any way you want without having to write Javascript, e.g.

[code=html]<input type="text" value="" id="frmRegisterError" name="frmRegisterError" value="<?php echo $usuario; ?>"/>[/code]

It can be written in many different ways, the main thing here is: No Javascript needed.
Copy linkTweet thisAlerts:
@Evair_PetersonauthorJul 30.2013 — Hello [B]Airblader[/B]. Thank you for answer my question!

Sorry me, but my level of fluency in English is very bad. I think I have not explained well when I said I had read that it was impossible to put an error message on a specific field in a form. What I mean is that I do not know any way to do this with the same efficiency and practicality that I do it with javascript. An example is that using javascript code I can define exactly which field in the form I want to put the value of a variable without changing the original HTML code.
[CODE]
var error = "RESULT OF VALIDATION";
var place = document.getElementById('FieldID');
place.value = error;
[/CODE]


As you can see, with javascript I can access any field that has an ID set and change its value without stopping the execution of this function. And I don't know any way to do this with pure PHP.

The way you appointed me to do this, by a coincidence, I learned two or three days ago, but I'm not sure you're doing it right. Let me explain why this:

I used a similar code, but with some conditionals more to get higher accuracy. Even the result was very good because I could get the result I wanted. However, this method did not work in Mozilla Firefox. He worked in all browsers I tested, including Internet Explorer, with whom I usually have many problems. I've tried to modify code and eset many ways to make it work in Mozilla Firefox and I could not. Here is an example of the code I'm using to change the value of a field as the example you gave me:
[code=html]<input type="text" class="frmRegisterText" id="frmRegisterUN" name="frmRegisterUN"
value="<?php if($frmRegisterError){if ($frmRegisterUN == "Nome de usuário")echo "Nome de usuário";else echo $frmRegisterUN;}else echo "Nome de usuário";?>"/>[/code]


And here is a part of the validation is done in PHP file:
[code=php]$frmRegisterUN = $_POST['frmRegisterUN'];
if (strlen($frmRegisterUN) < 5 || strlen($frmRegisterUN) > 20)
{
$frmRegisterError = "Seu nome de usuário deve conter de 5 à 20 caracteres";
return false;
}[/code]


As I said before, this code is working perfectly in all browsers I tested except Mozilla Firefox. For your better understanding, I'll put [B][COLOR="#FF0000"][U]HERE[/U][/COLOR][/B] a picture of what is happening.

I thank you immensely for your cooperation. And... I'm sorry for so bad it's my English. Greetings from Brazil. Evair.
Copy linkTweet thisAlerts:
@Evair_PetersonauthorJul 30.2013 — Please, where I wrote "[U][I]but I'm not sure you're doing it right.[/I][/U]" it should be understood that I wanted to write "[B][U][COLOR="#008000"]but I'm not sure I AM doing it right.[/COLOR][/U][/B]". I wrote wrong. I'm sorry. ?
Copy linkTweet thisAlerts:
@AirbladerJul 30.2013 — This is extremely bizarre as PHP is executed on the server, therefore completely browser-independent (the browser will never even so much as [i]receive[/i] the PHP code). How are you running your application &#8211; on a local server? If so, make sure you call the file through your server, not as a file on your system.
Copy linkTweet thisAlerts:
@Evair_PetersonauthorJul 30.2013 — Now I really do not understand anything!! I was accessing the file from the internal Apache server via localhost, but still having problems... To have no doubt, I decided to upload the file to the web server. And what was my surprise? There it worked perfectly! I swear I could not understand for what reason it worked perfectly in all browsers except by Mozilla Firefox when it was accessed via localhost ... And when I access page from the web server it works perfectly on all browsers, without exception!

I could even imagine that the Apache server is misconfigured, wich I've changed something unintentionally. But I did not touch it ... If so, I will uninstall and reinstall Apache and see if it still has these crazy errors.

Thank you for your attention and for the simplest solution and unimaginable.

I spent a whole day heating up the head trying to solve this problem. ?


Thank you so much.

Greetings from Brazil.

Evair Peterson.
Copy linkTweet thisAlerts:
@AirbladerJul 30.2013 — Glad it works now. And yes, it's definitely a problem with the Apache setup. Good luck!
×

Success!

Help @Evair_Peterson spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...