0

I'm just starting to learn javascript today and I was wondering how to call on a javascript function in PHP after it is done submitting a form to the database. My javascript code is basically an alert box saying that your account has been created. Any response wold help. Here is the section of the PHP code that I want to insert the function into (everything else really doesn't matter) and javascript code insert, if you require anything else please tell me. Thanks a lot!

PHP:

<?php

class Registration
{
    **FORM VALIDATION AND SUBMISSION NOT INCLUDED**

                    if ($query_check_user_name->num_rows == 1) 
                    {
                        $this->errors[] = "Sorry, that user name is already taken. Please choose another one.";
                    } 
                    else 
                    {
                        // write new users data into database
                        $query_new_user_insert = $this->db_connection->query("INSERT INTO users (user_name, user_password_hash, user_email) VALUES('" . $this->user_name . "', '" . $this->user_password_hash . "', '" . $this->user_email . "');");

                        if ($query_new_user_insert) 
                        {
                            $this->registration_successful = true;
                            $this->messages[] = "Registration successful! Please go back to the login page and login in with your new username and password.";
                            echo "<script type='text/javascript'>myFunction()</script";
                        } 
                        else 
                        {
                            $this->errors[] = "Sorry, your registration failed. Please go back and try again.";
                        }
                    }
                } 
                else 
                {
                    $this->errors[] = "Sorry, no database connection.";
                }
            }
        } 
        else 
        {
            $this->errors[] = "An unknown error occurred.";
        }
    }
}
?>

Javascript:

<script>
    function myFunction()
    {
        var r = confirm("Registration Successful!\nYou will now be redirected to the login page");
        if(r == true)
        {
           location.assign("http://yhsaa.org/members/index.php");
        }
    }   
</script>
9
  • has myFunction() been defined in your page somewhere BEFORE where you output your <script>myFunction()</script> text? If not, then you're just going to get a "call to undefined function" error. Commented Feb 3, 2014 at 19:01
  • Also, you're missing > in this: echo "<script type='text/javascript'>myFunction()</script"; Commented Feb 3, 2014 at 19:02
  • @MarcB well, the php code appears before the html code on the same page and the script is included in the html code, does that count? Commented Feb 3, 2014 at 19:03
  • @Gezim ignore that, that was a problem with copying and pasting, thanks. Commented Feb 3, 2014 at 19:03
  • then, yeah. you're trying to call a function that hasn't been defined yet. remember that PHP and JS are parsed and executed top-down. calling a function before the parser's reached the definition will just kill the script. Commented Feb 3, 2014 at 19:04

2 Answers 2

1

Try this:

echo "<script type='text/javascript'>window.onload = function()
{
    myFunction();
}
</script>";

What is happening is that this code creates and event on window object called onload. This event will be fired when your html has finished loading. Then it will call your function myFunction(). This behaviour will make sure that your function was declared before its called, so its avoids not declared exception for your function.

Its like changing this:

myFunction(); // At this time JS doesn't know that you have an function called myFunction()
function myFunction() {};

to this:

function myFunction() {};
myFunction(); // At this time JS already know your function myFunction() and it will call it.
Sign up to request clarification or add additional context in comments.

4 Comments

But will that code then be run after I submit the data to the form?
@picardisbetterthankirk Yes, the same way you did before. What will change is the time JS will call it, but will be always after the form was posted.
thanks, I used your suggestions and the sugestions in the comments and solved it. Thanks a ton!
@picardisbetterthankirk glad to help. The most important thing here is to understand why it solved your problem.
0

Your script contains an error, you don't close the script tag correctly !

3 Comments

You answer contains an error: It doesn't answer the question properly.
To make this a proper answer, show the incorrect line, show how to correct it (What to type) and explain the concept so the asker can use that information in the future. A person who doesn't know how to close a script tag won't learn how from this answer.
I'm new to stackoverflow and had an argument with the code example markup =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.