Daney11 Posted April 26, 2007 Share Posted April 26, 2007 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 More sharing options...
Psycho Posted April 26, 2007 Share Posted April 26, 2007 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'] ?>"> ?> Link to comment https://forums.phpfreaks.com/topic/48793-solved-if-and-else-form/#findComment-239134 Share on other sites More sharing options...
Daney11 Posted April 26, 2007 Author Share Posted April 26, 2007 Lovely, Thankyou very much mate Dane Link to comment https://forums.phpfreaks.com/topic/48793-solved-if-and-else-form/#findComment-239145 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.