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">&nbsp;</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:

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.

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.