Jump to content

Recommended Posts

Hi All,


I’m having trouble with some conditions on a form:

What I want the below to say is:

1) When Submit is hit insert the form details entered into the database if the required fields are filled in ($opening & $body_text)
2) If they are not then don’t submit any of the form data and print out the relevant error message

Trouble is with the form I created it posts everything to the database even if the required fields are not filled in!

Can anyone please advise?

Many Thanks

[code]
<?

session_start();

//Connect to DB
include("*************");

//import form information
$section=$_POST['section'];
$added_by=$_POST['added_by'];
$headline=$_POST['headline'];
$opening=$_POST['opening'];
$body_text=$_POST['body_text'];

//When the submit button is hit
if (isset($_POST['Submit'])) {

//if no opening paragraph and no body entered print an error  
if (empty($opening) && empty($body_text)){
  print "No opening paragraph and body text was entered. <br>Please include this information to continue";
  }
   //if no opening paragraph entered send print an error  
  elseif (empty($opening)){
  print "No opening paragraph was entered.<br>Please include this information to continue.<br>";
  }
  //if no body text send print an error  
elseif (empty($body_text)){
  print "No body text was entered.<br>Please include this information to continue.<br>";
  }

  //if the form has the required fields filled in then run the query to add data to the database
  else {

  $query = "INSERT INTO cms_stories(section,added_by,headline,opening,body_text) VALUES ('$section','$added_by','$headline','$opening','$body_text')";
  $msg = "A new Story has been added to the database - Please Click <a href=\"http://index\">Here</a> to return to the main menu";
  
  $link = mysql_connect;
  mysql_select_db($db);
  $result = mysql_query($query) or die('Query failed: ' . mysql_error());
  mysql_close();

  
      }
                            }
?>
<html>
<head>
<title>Add to DB</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF">
<blockquote> </blockquote>
<form action="add_blank.php" method="post" name="frmAdd">
  <p><?php echo $msg?></p>
  <table width="737" border="0" cellspacing="1" cellpadding="1">
  <tr>
    <td>Section</td>
    <td>
      <select name="section">
        <option value="news">News</option>
        <option value="sport">Sport</option>
        <option value="business">Business</option>
        <option value="viator">Viator</option>
      </select>
    </td>
  </tr>
  <tr>
    <td>Added By</td>
    <td>
      <input name="added_by" type="text" size="35" maxlength="128" value="">
    </td>
  </tr>
  <tr>
    <td height="35">Headline</td>
    <td height="35">
      <input type="text" size="48" maxlength="255" name="headline">
    </td>
  </tr>
  <tr>
    <td>Opening Paragraph</td>
    <td>
      <textarea name="opening" wrap="virtual" rows="4" cols="80"></textarea>
    </td>
  </tr>
  <tr>
    <td>Body Text:</td>
    <td>
      <textarea name="body_text" wrap="virtual" rows="25" cols="80"></textarea>
    </td>
  </tr>
</table>
<input type="Submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Clear Story">
</form>
</body>
</html>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/12141-form-conditions/
Share on other sites

just use this

[code]
if (empty ($_POST['opening']) || empty ($_POST['body_text']))
{
  die ("Error: You're not cool enough."); //print error
}
[/code]

and if you want it to load the form any way

[code]
if (empty ($_POST['opening']) || empty ($_POST['body_text']))
{
  $error = "You're not cool enough";
}
if (!isset ($error))
{
  //query
}
?>
[/code]
<form> etc...
Link to comment
https://forums.phpfreaks.com/topic/12141-form-conditions/#findComment-46225
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.