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">
                    <td width="112" class="style13">Name:</td>
                    <td width="633" class="style13"><input name="name" type="text" id="name" size="50" /></td>
                    <td valign="top" class="style13">Email:</td>
                    <td class="style13"><input name="email" type="text" id="email" size="50" /></td>
                    <td valign="top" class="style14 style15">Wishes</td>
                    <td class="style13"><textarea name="comment" cols="60" rows="6" id="comment"></textarea></td>
                    <td class="style14">&nbsp;</td>
                    <td class="style14"><input type="submit" name="Submit" value="Send" />
                      <input type="reset" name="Submit2" value="Clear" /></td>

The Addwish.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

$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";

//check if query successful
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";


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:

7 Years
Discussion Span
Last Post by ReMoN SaMiR

After line 16, if the result of strlen() on any of those three variables is empty, display the page again with an error saying what was missing - or whatever you want.


you can do something like this:

if(!isset($_POST || strlen(trim($_POST)) == 0)
// display the form again with error message


You don't need the isset(), as textfields are always submitted whether they're populated or not.

Edited by quasipickle: n/a


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.';
$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";

And it worked fine.

However I still need your help, I want to split the results into PHP pages, would you help me with that?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.