Javascript form


Hey! I havn't been on for quite some time been learning a few stuff. Plus im in the middle of my exams. Anyway heres a tutorial on how to make the form and use javascript to submit it.

First lets look at the complete code...
PHP Code
  1. <html>
  2.  
  3. <script language='javascript'>
  4. function submitform(value, valuee){
  5. if (value == "" || valuee == ""){
  6. alert("Error, please fill in all the form!");
  7. }else{
  8. alert("Form succesfully filled in. Thank you " + value + " " + valuee);
  9. document.form1.submit();
  10. }
  11.  
  12. }
  13. </script>
  14.  
  15. <form name='form1' method='post'>
  16. First Name: <input type='text' name='firstname'><br>
  17. Surname: <input type='text' name='surname'><br>
  18. <a href='#' onclick='submitform(document.form1.firstname, document.form1.surname);' onmouseover="this.style.cursor:pointer;">Submit</a><br>
  19.  
  20. </form>
  21.  
  22. </html>


Okay lets go over it. First we have the javascript.
Javascript is very similar to php and very easy to understand if you know php.

In the js script, i first created a function called 'submitform'. In the submitform brackets i put in two variables, both undefined and both seperated by commas.

I then checked to see if the values had been set (if there was any information in them) and if there wasnt, i prompted the users browser and alerted them as to the problem.

I then done an else statement and alerted the user again about there information being submitted. Then I done a form submit thing whihc would submit the form. The form would only be submitted after the user had clicked OK on the alert prompt.

And the rest of it is just the html which you should look at to see what i done and how i ran the function.

Thanks,
if theres any problems please mention them. I havn't tested the script.
ilyas-shezad's Avatar
Views:
1,996
Rating:
Posted on Wednesday 11th June 2008 at 01:03 PM
ilyas-shezad
ilyas-shezad's Avatar
You dont even need to put in a action=""
It can be processed on the same page.
Posted on Monday 9th June 2008 at 03:32 PM
DanielXP
DanielXP's Avatar
where ever you put a action="" too.
Posted on Monday 9th June 2008 at 05:13 AM
jambomb
jambomb's Avatar
were you go when u posted it lol