• 0

Required fields in PHP form


Question

Hello,

Few days ago I was searching online for a way to create a PHP forum though which the visitors can leave their messages and to automatically appear on the page. I found the a code and I used it to create the PHP form, but unfortunately I discovered the that all the fields are not required means that the visitor can submit an empty message and it will be added empty

The form:

 <form id="form1" name="form1" method="post" action="addwish.php">
              <td><table width="760" border="0" cellpadding="3" cellspacing="1">
                  <tr>
                    <td width="112" class="style13">Name:</td>
                    <td width="633" class="style13"><input name="name" type="text" id="name" size="50" /></td>
                  </tr>
                  <tr>
                    <td valign="top" class="style13">Email:</td>
                    <td class="style13"><input name="email" type="text" id="email" size="50" /></td>
                  </tr>

                  <tr>
                    <td valign="top" class="style14 style15">Wishes</td>
                    <td class="style13"><textarea name="comment" cols="60" rows="6" id="comment"></textarea></td>
                  </tr>
                  <tr>
                    <td class="style14"> </td>
                    <td class="style14"><input type="submit" name="Submit" value="Send" />
                      <input type="reset" name="Submit2" value="Clear" /></td>
                  </tr>
              </table></td>

The Addwish.php

  <?php
$host="localhost"; // Host name
$username="name"; // Mysql username
$password="password"; // Mysql password
$db_name="dbname"; // Database name
$tbl_name="tablename"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");

$datetime=date("y-m-d h:i:s"); //date time

$name=$_POST['name']; 
$email=$_POST['email']; 
$comment=$_POST['comment']; 
$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);

//check if query successful
if($result){
echo "Your Birthday wish has been added successfully. Thank You!";
echo "<BR>";
echo "You'll be automatically redirected to the wishes page in 1 second.";
}

else {
echo "ERROR";
}

mysql_close();
?> 

Can anyone tell me how to edit the above code to make all the fields required before submitting the post into the database.

Your help is appreciated. :rose:

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Thanks a lot guys, I've used this:

if ($name == '')
{
print 'Fill in your name.';
}
elseif ($email == '')
{
print 'Fill in the e-mail.';
}
elseif ($comment == '')
{
print 'Write a comment.';
}
else 
{
$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
}  

And it worked fine.

I appreciate your help and your time.

Link to comment
Share on other sites

  • 0

Just want to point out, you aren't validating (checking for naughty code) on any of the form sections, well judging by what I can see anyway.

Its too late now, but when I get up tomorrow later today I will post up my function that I wrote to clean it all up.

Link to comment
Share on other sites

  • 0

Thanks a lot guys, I've used this:

if ($name == '')
{
print 'Fill in your name.';
}
elseif ($email == '')
{
print 'Fill in the e-mail.';
}
elseif ($comment == '')
{
print 'Write a comment.';
}
else 
{
$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
}  

And it worked fine.

I appreciate your help and your time.

Slight problem with that code, if I entered just spaces, it would validate correctly, but is an invalid input?

Change the ifs to do the following:

if(trim($name) == '')

If that throws an error, you may need to cast the trimmed string to a variable then check on the variable (it's been a couple of years since I did any PHP :)).

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.