954,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

php comment form with javascript to check null values

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:

<script language="JavaScript" type="text/javascript">
<!--
function verify ( form )
{
  if (form.email.value == "") {
    alert( "Please enter your email address." );
    return false ;
  }
else {
      return true ;
     }
}
//-->
</script>


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

<form method="post" name="Comment Form" action="PHP/comment.php" target="_self">

<table border="0" name="pageid" id="commentform">

<tr>
    <td><div align="right">Email: (Required)</div></td>
    <td><input type="text" name="email" size="30" /></td>
</tr>

<tr>
<td>&nbsp;</td>
    <td>
    <input type="submit" name="submit" value="Submit" /> 
    <input type="reset" name="reset" value="Reset" />
    </td>
</tr>

</table>
</form>
ptemedia
Light Poster
29 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Below i modified your script and now it check for null value.

<html>
<head>
<script language="JavaScript" type="text/javascript">
function verify ( form )
{
  if (form.email.value == "") {
    alert( "Please enter your email address." );
    return false ;
  }
else {
      return true ;
     }
}

</script>
</head>
<body>

<form method="post" name="Comment Form" action="PHP/comment.php" target="_self" onsubmit="return verify(this)">
 
<table border="0" name="pageid" id="commentform">
 
<tr>
    <td><div align="right">Email: (Required)</div></td>
    <td><input type="text" name="email" size="30" /></td>
</tr>
 
<tr>
<td>&nbsp;</td>
    <td>
    <input type="submit" name="submit" value="Submit" /> 
    <input type="reset" name="reset" value="Reset" />
    </td>
</tr>
 
</table>
</form>
</body>
</html>

You have to callverify function on onSubmit event of form.

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

-keval

keval_hack
Junior Poster in Training
Banned
66 posts since Jan 2010
Reputation Points: 8
Solved Threads: 4
 

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

ptemedia
Light Poster
29 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

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

keval_hack
Junior Poster in Training
Banned
66 posts since Jan 2010
Reputation Points: 8
Solved Threads: 4
 

The code given by keval_hack working perfectly.

rajarajan07
Nearly a Posting Virtuoso
1,447 posts since May 2008
Reputation Points: 167
Solved Threads: 239
 

Yeah, sorry guys, I'd made a stupid mistake (typo :( ).

It does work fine! Thanks :)

ptemedia
Light Poster
29 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: