Jump to content

[SOLVED] If and Else FORM


Daney11

Recommended Posts

Hi guys, i create a form and i want to add some if and else statments within the form when it gets submitted.

 

This is the sort of thing i want to use, to display errors to the user when they submit the form but it displays the error as soon as the script is loaded.

if (isset($_POST['mail_subject']) == "" ) {

echo '<script type="text/javascript">alert("You Have Not Entered A Subject")</script>\n';

}

if (isset($_POST['mail_body']) == "" ) {

echo '<script type="text/javascript">alert("You Have Not Entered Any Text")</script>\n';

}

else {

if (isset($_POST['mail_subject'])) {

$strQuery = "INSERT INTO mail_inbox 
(
mail_inbox_id,
mail_inbox_manager_id,
mail_inbox_from_id,
mail_inbox_date,
mail_inbox_time,
mail_inbox_subject,
mail_inbox_body,
mail_inbox_type
)";

$strQuery .= " VALUES 
(
'NULL',
'$_POST[mail_to]',
'$_POST[mail_from_id]',
'$date',
'$time',
'$_POST[mail_subject]',
'$_POST[mail_body]',
'Manager Mail'
)";

mysql_query($strQuery,$db) or die(mysql_error());

}

}

 

This is what i am using and it works perfect, it submits the data within the form.

if (isset($_POST['mail_subject'])) {

$strQuery = "INSERT INTO mail_inbox 
(
mail_inbox_id,
mail_inbox_manager_id,
mail_inbox_from_id,
mail_inbox_date,
mail_inbox_time,
mail_inbox_subject,
mail_inbox_body,
mail_inbox_type
)";

$strQuery .= " VALUES 
(
'NULL',
'$_POST[mail_to]',
'$_POST[mail_from_id]',
'$date',
'$time',
'$_POST[mail_subject]',
'$_POST[mail_body]',
'Manager Mail'
)";

mysql_query($strQuery,$db) or die(mysql_error());

}

 

Any ideas please guys?

 

Thanks a lot

 

Dane

Link to comment
https://forums.phpfreaks.com/topic/48793-solved-if-and-else-form/
Share on other sites

Well, your use of isset is incorrect. isset will return either true or false if the value is set. It does not return the value of the variable. You want to use isset to determine if you should check the values (i.e. determine if the form was submitted or the users first visit):

 

Try this:

<?php
if (isset($_POST['mail_subject'])) {

  $error = false;

  if (!$_POST['mail_subject']) {
    $error = 'You Have Not Entered A Subject';
  }

  if (isset($_POST['mail_body']) == "" ) {
    $error = '"You Have Not Entered Any Text';
  }


  if (!$error) {

    $strQuery = "INSERT INTO mail_inbox
    (
      mail_inbox_id,
      mail_inbox_manager_id,
      mail_inbox_from_id,
      mail_inbox_date,
      mail_inbox_time,
      mail_inbox_subject,
      mail_inbox_body,
      mail_inbox_type
    )";

    $strQuery .= " VALUES 
    (
      'NULL',
      '$_POST[mail_to]',
      '$_POST[mail_from_id]',
      '$date',
      '$time',
      '$_POST[mail_subject]',
      '$_POST[mail_body]',
       'Manager Mail'
    )";

    mysql_query($strQuery,$db) or die(mysql_error());

    //Confirmation message or redirect here
  }
}

//Show your form here with $error and populate the fields with the posted values if they exist
//Example

<?php echo $error ?>

Subject: <input type="text" name="mail_subject" value="<?php echo $_POST['mail_subject'] ?>">

?>

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.