Jump to content

keeping people from leaving blank section


serverman

Recommended Posts

i want to make this have the first/last name and comment be required to post the message to the sql database

ok heres my script for the form

<form action="../Beta/leaveacomment/insert.php" method="post">
<h3 align="center">Give us some feedback</h3><table width="250" border="1" align="center">
  <tr>
    <td>Firstname:</td>
    <td><input name="firstname" type="text" maxlength="15" /></td>
  </tr>
  <tr>
    <td>Lastname:</td>
    <td><input name="lastname" type="text" maxlength="15" /></td>
  </tr>
  <tr>
    <td>Email
: </td>
    <td><input name="email" type="text" id="email" /></td>
  </tr>
  <tr>
    <td>Comment</td>
    <td><label>
      <textarea name="comment" id="comment"></textarea>
    </label></td>
  </tr>
  <tr>
    <td colspan="2"><input type="reset" name="Reset" value="Reset" />
      <input name="submit" type="submit" />
      <label></label></td>
    </tr>
</table>

</form>

here is code for the processer

<?php
$login = mysql_connect("---","---","---");
if (!$login)
  {
  die('Could not connect: ' . mysql_error());
  }mysql_select_db("website_stuff", $login);$sql="INSERT INTO comment (FirstName, LastName, Email, Comment)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[email]','$_POST[comment]')";if (!mysql_query($sql,$login))
  {
  die('Error: ' . mysql_error());
  }
echo "thank you for leaving a comment."."<a href='../../home.PHP'>Back to Home</a>";mysql_close($login)
?>

EDITED

 

Put before the query.

 

You need to set the variables from the post data first by:

 

$username = $_POST['username'];

 

etc, then you can do this:

 

if(!$firstname or !$lastname or !$comment) {
echo ("Please complete all fields");
}
else {
//do sql stuff
}

 

And yes as the other person said, put paragraphs in your code, and indent

Put before the query.

 

if(!firstname or !lastname or !comment) {
echo ("Please complete all fields");
}
else {
//do sql stuff
}

 

And yes as the other person said, put paragraphs in your code, and indent

sweet thank... that was an easy one... i'm really new to this stuff :P thanks for the help

No.  That's not good enough.  Use

if (!empty($_POST['firstname']) && !empty($_POST['lastname']) && !empty($_POST['comments'])) {

echo "Please fill in first name, last name, and comments!';

}

else {

//do query

}

 

And use '' on associative array indexes.  I.E: $_POST['element'] instead of $_POST[element].

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.