Mr Chris Posted June 19, 2006 Share Posted June 19, 2006 Hi,I havemy form which I want to check for required fields before posting to the DB. It says:1) IF Submit is hit but 'opening' and 'body' are empty then output an error2) IF there is no error then submit the dataI'm pretty sure i'm nearly there, but it does not like this line:[b] empty ($_POST['opening']) || empty ($_POST['body_text']) [/b]Parse error: parse error, unexpected T_VARIABLE in *********/*****/new_news/*****.php on line 19Can anyone please help as this is driving me potty![code]<?phpsession_start();//Connect to DB include("*****");// POST variables $section=$_POST['section'];$added_by=$_POST['added_by'];$headline=$_POST['headline'];$opening=$_POST['opening'];$body_text=$_POST['body_text'];if (isset($_POST['Submit'])){ empty ($_POST['opening']) || empty ($_POST['body_text']) $error = "There is an error";}if (!isset ($error)){ $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"><form action="add_blank.php" method="post" name="frmAdd"> <p><?php echo $msg?> <?php echo $error?></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] Quote Link to comment https://forums.phpfreaks.com/topic/12362-required-fields-please-please-help/ Share on other sites More sharing options...
Orio Posted June 19, 2006 Share Posted June 19, 2006 Change this:[code]if (isset($_POST['Submit'])){ empty ($_POST['opening']) || empty ($_POST['body_text']) $error = "There is an error";}[/code]To this:[code]if (isset($_POST['Submit'])){ if(empty($_POST['opening']) || empty($_POST['body_text'])){ $error = "There is an error";};}[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/12362-required-fields-please-please-help/#findComment-47262 Share on other sites More sharing options...
zq29 Posted June 19, 2006 Share Posted June 19, 2006 [code]if (isset($_POST['Submit']) && (empty($_POST['opening']) || empty ($_POST['body_text']))){ $error = "There is an error";}[/code][b]EDIT:[/b] Damnit Orio! You beat me to it again! haha Quote Link to comment https://forums.phpfreaks.com/topic/12362-required-fields-please-please-help/#findComment-47263 Share on other sites More sharing options...
Mr Chris Posted June 19, 2006 Author Share Posted June 19, 2006 Cheers Guys,I really do appreciate it.Couple of things though since I added your suggestions:On this page[a href=\"http://www.slougheaz.org/greenock/new_news/new.php\" target=\"_blank\"]http://www.slougheaz.org/greenock/new_news/new.php[/a]1) When the page loads it automatically brings up [b]A new Story has been added to the database - Please Click Here to return to the main menu [/b] without even hitting submit2) I'd like the page to hold the values in the textboxes when submit is hit so the user can see what has been enteredWould it be possible to kindly advise again please?ThanksChris Quote Link to comment https://forums.phpfreaks.com/topic/12362-required-fields-please-please-help/#findComment-47265 Share on other sites More sharing options...
zq29 Posted June 19, 2006 Share Posted June 19, 2006 I'd rearrange the following:[code]if (isset($_POST['Submit'])) { // POST variables $section = $_POST['section']; $added_by = $_POST['added_by']; $headline = $_POST['headline']; $opening = $_POST['opening']; $body_text = $_POST['body_text']; if(empty($_POST['opening']) || empty ($_POST['body_text'])) { $error = "There is an error"; } 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"; //You'd probably want to unset all of the variables you created above here on success - unset(); }}?>[/code]Then to have the defaults on your inputs:[code]<input type="text" size="48" maxlength="255" name="headline" <?php if(isset($headline)) echo "value='$headline'"; ?>/>[/code]Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/12362-required-fields-please-please-help/#findComment-47268 Share on other sites More sharing options...
Mr Chris Posted June 19, 2006 Author Share Posted June 19, 2006 That is fantastic- you have no idea on how that's been bugging me. Many ThanksChris Quote Link to comment https://forums.phpfreaks.com/topic/12362-required-fields-please-please-help/#findComment-47270 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.