943,022 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 2903
  • PHP RSS
Jul 4th, 2010
0

php comment form with javascript to check null values

Expand Post »
Hi, first of all sorry if this has come up before, but I couldn't find it anywhere.

I have a working php form which submits data on submit. I need to verify the input fields to ensure they are not empty. I know i need a javascript function to run this check.

I am just having problems running the javascript before the form is submitted.

Any helps would be grand.

Javascript code:
PHP Syntax (Toggle Plain Text)
  1. <script language="JavaScript" type="text/javascript">
  2. <!--
  3. function verify ( form )
  4. {
  5. if (form.email.value == "") {
  6. alert( "Please enter your email address." );
  7. return false ;
  8. }
  9. else {
  10. return true ;
  11. }
  12. }
  13. //-->
  14. </script>

Here is some of the code, with one of the required fields, email.

PHP Syntax (Toggle Plain Text)
  1. <form method="post" name="Comment Form" action="PHP/comment.php" target="_self">
  2.  
  3. <table border="0" name="pageid" id="commentform">
  4.  
  5. <tr>
  6. <td><div align="right">Email: (Required)</div></td>
  7. <td><input type="text" name="email" size="30" /></td>
  8. </tr>
  9.  
  10. <tr>
  11. <td>&nbsp;</td>
  12. <td>
  13. <input type="submit" name="submit" value="Submit" />
  14. <input type="reset" name="reset" value="Reset" />
  15. </td>
  16. </tr>
  17.  
  18. </table>
  19. </form>
Reputation Points: 10
Solved Threads: 0
Light Poster
ptemedia is offline Offline
29 posts
since Jul 2010
Jul 4th, 2010
1
Re: php comment form with javascript to check null values
Below i modified your script and now it check for null value.

PHP Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <script language="JavaScript" type="text/javascript">
  4. function verify ( form )
  5. {
  6. if (form.email.value == "") {
  7. alert( "Please enter your email address." );
  8. return false ;
  9. }
  10. else {
  11. return true ;
  12. }
  13. }
  14.  
  15. </script>
  16. </head>
  17. <body>
  18.  
  19. <form method="post" name="Comment Form" action="PHP/comment.php" target="_self" onsubmit="return verify(this)">
  20.  
  21. <table border="0" name="pageid" id="commentform">
  22.  
  23. <tr>
  24. <td><div align="right">Email: (Required)</div></td>
  25. <td><input type="text" name="email" size="30" /></td>
  26. </tr>
  27.  
  28. <tr>
  29. <td>&nbsp;</td>
  30. <td>
  31. <input type="submit" name="submit" value="Submit" />
  32. <input type="reset" name="reset" value="Reset" />
  33. </td>
  34. </tr>
  35.  
  36. </table>
  37. </form>
  38. </body>
  39. </html>


You have to call verify function on onSubmit event of form.

You can also use php validation for null value checking and email validation.

-keval
Reputation Points: 8
Solved Threads: 4
Banned
keval_hack is offline Offline
66 posts
since Jan 2010
Jul 4th, 2010
0
Re: php comment form with javascript to check null values
I have done this, but it is still posting blank data. I'm not sure how, but the php script is still running first and the javascript is being ignored
Reputation Points: 10
Solved Threads: 0
Light Poster
ptemedia is offline Offline
29 posts
since Jul 2010
Jul 5th, 2010
0
Re: php comment form with javascript to check null values
First you check your browser support javascript or it is enable in browser settings.

You make simple javascript function that display alert for checking javascript is working and call function on button' s onClick event.

-keval
Reputation Points: 8
Solved Threads: 4
Banned
keval_hack is offline Offline
66 posts
since Jan 2010
Jul 5th, 2010
0
Re: php comment form with javascript to check null values
The code given by keval_hack working perfectly.
Reputation Points: 167
Solved Threads: 239
Nearly a Posting Virtuoso
rajarajan07 is offline Offline
1,445 posts
since May 2008
Jul 5th, 2010
0
Re: php comment form with javascript to check null values
Yeah, sorry guys, I'd made a stupid mistake (typo ).

It does work fine! Thanks
Reputation Points: 10
Solved Threads: 0
Light Poster
ptemedia is offline Offline
29 posts
since Jul 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: php script being redirected instead of output
Next Thread in PHP Forum Timeline: Upload XML file from URL to server





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC