www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Javascript code don't work inside a PHP code

  1. #1
    Join Date
    Jul 2013
    Posts
    6

    resolved [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 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?

  2. #2
    Join Date
    Jul 2013
    Posts
    66
    Quote Originally Posted by Evair_Peterson View Post
    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 ...
    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
    Nonsense!


    HTML Code:
    <input type="button" value="REGISTRAR" id="frmRegister_Button"/ onClick="JSF ()">
    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.

    HTML Code:
    <input type="text" value="" id="frmRegisterError" name="frmRegisterError"  value="<?php echo $usuario; ?>"/>
    It can be written in many different ways, the main thing here is: No Javascript needed.

  3. #3
    Join Date
    Jul 2013
    Posts
    6
    Hello Airblader. 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;
    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:
    HTML Code:
    <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";?>"/>
    And here is a part of the validation is done in PHP file:
    PHP Code:
    $frmRegisterUN $_POST['frmRegisterUN'];
    if (
    strlen($frmRegisterUN) < || strlen($frmRegisterUN) > 20)
    {
       
    $frmRegisterError "Seu nome de usuário deve conter de 5 ŕ 20 caracteres";
       return 
    false;

    As I said before, this code is working perfectly in all browsers I tested except Mozilla Firefox. For your better understanding, I'll put HERE 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.

  4. #4
    Join Date
    Jul 2013
    Posts
    6
    Please, where I wrote "but I'm not sure you're doing it right." it should be understood that I wanted to write "but I'm not sure I AM doing it right.". I wrote wrong. I'm sorry.

  5. #5
    Join Date
    Jul 2013
    Posts
    66
    This is extremely bizarre as PHP is executed on the server, therefore completely browser-independent (the browser will never even so much as receive the PHP code). How are you running your application – on a local server? If so, make sure you call the file through your server, not as a file on your system.

  6. #6
    Join Date
    Jul 2013
    Posts
    6
    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.

  7. #7
    Join Date
    Jul 2013
    Posts
    66
    Glad it works now. And yes, it's definitely a problem with the Apache setup. Good luck!

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