March 10, 2013

php form and onsubmit javascript function call

Question by user1060187

I’ve created a form with html being echoed in PHP and am trying to call a Javascript function in the same form. I’ve copied and pasted the js part which I have put in my head tags at the top of the php file.

<script>
    function validate(){
        alert("You need to enter the date in format dd-mm-yyyy");
    }
</script>

Further on I have this at the start of the form

echo "<form method="post" action="index.php" onsubmit="validate()">
        <br>
        <span>Name: <input type="text" name="name" value="$name"></span>"

Is it possible to call javascript from php in the same document?
Cheers

Answer by Starx

You need to make your function available when it is executed.

window.onload = function() {
    function validate(){
        alert("You need to enter the date in format dd-mm-yyyy");
    }
};

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!