dadamssg Posted January 16, 2009 Share Posted January 16, 2009 Im going through a book and using its sample code...but i get an unexpected T_ELSE erro on the fourth line...can somebody PLEASE point out the problem, i cant figure it out. thanks else //check Title { $title = (!isset($_POST['title']) || trim($_POST['title']} == " ") or die("Please Enter Title."); mysql_escape_string(trim($_POST['title'])); Quote Link to comment https://forums.phpfreaks.com/topic/141146-check-for-blanks-in-form-problem/ Share on other sites More sharing options...
kenrbnsn Posted January 16, 2009 Share Posted January 16, 2009 Please post more of your code. Ken Quote Link to comment https://forums.phpfreaks.com/topic/141146-check-for-blanks-in-form-problem/#findComment-738785 Share on other sites More sharing options...
dadamssg Posted January 16, 2009 Author Share Posted January 16, 2009 heres the whole thing <html> <head> <basefont="Arial"> </head> <?php if (!$_POST['submit']) { //form not submitted { ?> <h3><center>Event Post Form</h3></center><form action"<?=$_SERVER['PHP_SELF']?>" method='POST'> <center><Table border='10'> <tr> <td style='text-align: center font-weight: bold'> Title:</td> <td><input type='text' name='title' size='75' maxlength='75'></td> </tr><tr><td colspan='2'> </td></tr><tr> <td style='text-align: center font-weight: bold'> Description:</td> <td><textarea name='Description' cols='57' rows='5'> </textarea><br></td> </tr><tr><td colspan='2'> </td></tr><tr> <td style='text-align: center font-weight: bold'> Location:</td> <td><input type='text' name='Location' size='75' maxlength='75'></td> </tr><tr><td colspan='2'> </td></tr><tr> <td style='text-align: center font-weight: bold'> Starts:</td> <td><B> Day</B> <select name='smonth'> <option value='01'>Jan <option value='02'>Feb <option value='03'>Mar <option value='04'>April <option value='05'>May <option value='06'>June <option value='07'>July <option value='08'>Aug <option value='09'>Sep <option value='10'>Oct <option value='11'>Nov <option value='12'>Dec</select> <B>/</B> <select name='sday'> <option value='01'>1 <option value='02'>2 <option value='03'>3 <option value='04'>4 <option value='05'>5 <option value='06'>6 <option value='07'>7 <option value='08'>8 <option value='09'>9 <option value='10'>10 <option value='11'>11 <option value='12'>12 <option value='13'>13 <option value='14'>14 <option value='15'>15 <option value='16'>16 <option value='17'>17 <option value='18'>18 <option value='19'>19 <option value='20'>20 <option value='21'>21 <option value='22'>22 <option value='23'>23 <option value='24'>24 <option value='25'>25 <option value='26'>26 <option value='27'>27 <option value='28'>28 <option value='29'>29 <option value='30'>30 <option value='31'>31</select> <b>/<b> <select name='syear'> <option value='2009'>2009 <option value='2010'>2010 </select> <B> Time</B> <select name='shour'> <option value='01'>1 <option value='02'>2 <option value='03'>3 <option value='04'>4 <option value='05'>5 <option value='06'>6 <option value='07'>7 <option value='08'>8 <option value='09'>9 <option value='10'>10 <option value='11'>11 <option value='12'>12 </select> <B>:</B> <select name='sminute'> <option value='00'>00 <option value='05'>05 <option value='10'>10 <option value='15'>15 <option value='20'>20 <option value='25'>25 <option value='30'>30 <option value='35'>35 <option value='40'>40 <option value='45'>45 <option value='50'>50 <option value='55'>55 </select> <select name='sampm'> <option value='pm'>pm <option value='am'>am </select> </td> </tr><tr><td colspan='2'> </td></tr><tr> <td style='text-align: center font-weight: bold'> Ends:</td> <td><B> Day</B> <select name='smonth'> <option value='01'>Jan <option value='02'>Feb <option value='03'>Mar <option value='04'>April <option value='05'>May <option value='06'>June <option value='07'>July <option value='08'>Aug <option value='09'>Sep <option value='10'>Oct <option value='11'>Nov <option value='12'>Dec</select> <B>/</B> <select name='eday'> <option value='01'>1 <option value='02'>2 <option value='03'>3 <option value='04'>4 <option value='05'>5 <option value='06'>6 <option value='07'>7 <option value='08'>8 <option value='09'>9 <option value='10'>10 <option value='11'>11 <option value='12'>12 <option value='13'>13 <option value='14'>14 <option value='15'>15 <option value='16'>16 <option value='17'>17 <option value='18'>18 <option value='19'>19 <option value='20'>20 <option value='21'>21 <option value='22'>22 <option value='23'>23 <option value='24'>24 <option value='25'>25 <option value='26'>26 <option value='27'>27 <option value='28'>28 <option value='29'>29 <option value='30'>30 <option value='31'>31</select> <b>/<b> <select name='eyear'> <option value='2009'>2009 <option value='2010'>2010 </select> <B> Time</B> <select name='ehour'> <option value='01'>1 <option value='02'>2 <option value='03'>3 <option value='04'>4 <option value='05'>5 <option value='06'>6 <option value='07'>7 <option value='08'>8 <option value='09'>9 <option value='10'>10 <option value='11'>11 <option value='12'>12 </select> <B>:</B> <select name='eminute'> <option value='00'>00 <option value='05'>05 <option value='10'>10 <option value='15'>15 <option value='20'>20 <option value='25'>25 <option value='30'>30 <option value='35'>35 <option value='40'>40 <option value='45'>45 <option value='50'>50 <option value='55'>55 </select> <select name='eampm'> <option value='pm'>pm <option value='am'>am </select> </td> </tr><tr><td colspan='2'> </td></tr><tr> <td style='text-align: center font-weight: bold'>   Event Category: </td> <td>                              <select name='EventCat'> #50 <option value='Music'>Music <option value='Sports'>Sports <option value='Shows'>Shows <option value='LocalFestivs'>LocalFestivs <option value='other'>Other</select></td> </tr><tr><td colspan='2'> </td></tr><tr> <td colspan='2' style='text-align: center'> <input type='submit' value='Post !'></td></tr></table></center> </form> <?php } else //check Title { $title = (!isset($_POST['title']) || trim($_POST['title']} == " ") or die("Please Enter Title."); mysql_escape_string(trim($_POST['title'])); //check description $description = (!isset($_POST['description']) || trim($_POST['description']} == " ") or die("Please Enter Description."); mysql_escape_string(trim($_POST['description'])); } //check location $location = (!isset($_POST['location']) || trim($_POST['location']} == " ") or die("Please Enter Location."); mysql_escape_string(trim($_POST['location'])); //format datetimes $startDT='$syear-$smonth-$sday $shour:$sminute:00'; $endDT='$eyear-$emonth-$eday $ehour:$eminute:00'; $today= date("Y-m-d h:i:s"); //connect to database $cnx = mysqli_connect($host, $user,$password,$dbname) or die ("Couldn't connect to server."); //create query $sql = "INSERT INTO Posttable (createDate, title, description, Location, eventType, startDT, endDT) VALUES ('$today', '$title', '$description', '$location', '$EventCat', '$startDT', 'endDT')"; //execute query $result = mysqli_query($cnx,$sql) or die ("Error in query: $sql. " . mysqli_error()); //close connection mysqli_close($cnx); } ?> </body> </html> ?> Quote Link to comment https://forums.phpfreaks.com/topic/141146-check-for-blanks-in-form-problem/#findComment-738786 Share on other sites More sharing options...
kenrbnsn Posted January 16, 2009 Share Posted January 16, 2009 This is not how you check for blanks: <?php $title = (!isset($_POST['title']) || trim($_POST['title']} == " ") or die("Please Enter Title."); mysql_escape_string(trim($_POST['title'])); ?> Try this instead: <?php if (!isset($_POST['title']) || trim($_POST['title']) == " ") { die("Please Enter Title."); else $title = mysql_escape_string(trim($_POST['title'])); ?> Note: I wouldn't use the die() here, since it kills your web page completely, leaving the user confused. Store the error message and put them out at the end. Ken Quote Link to comment https://forums.phpfreaks.com/topic/141146-check-for-blanks-in-form-problem/#findComment-738793 Share on other sites More sharing options...
dadamssg Posted January 17, 2009 Author Share Posted January 17, 2009 so if i wanted to make a message i would use this? <?php if (!isset($_POST['title']) || trim($_POST['title']) == " ") { $message="Enter Title"; } else { $title = mysql_escape_string(trim($_POST['title'])); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/141146-check-for-blanks-in-form-problem/#findComment-738799 Share on other sites More sharing options...
Maq Posted January 17, 2009 Share Posted January 17, 2009 You have to display the message not just assign a variable to it. $message="Enter Title"; echo $message; Quote Link to comment https://forums.phpfreaks.com/topic/141146-check-for-blanks-in-form-problem/#findComment-738802 Share on other sites More sharing options...
corbin Posted January 17, 2009 Share Posted January 17, 2009 trim($_POST['title']) == " " The purpose of trim is to trim whitespace (" ", " " so on) from a variable, so that wouldn't make sense. trim(x) will never == " " Quote Link to comment https://forums.phpfreaks.com/topic/141146-check-for-blanks-in-form-problem/#findComment-738811 Share on other sites More sharing options...
kenrbnsn Posted January 17, 2009 Share Posted January 17, 2009 I took a closer look at the code and it's full of problems. I'm not sure what book you used, but if this is an example of the code it contains, I would find another book. Here's my take on the code to check the form: <?php else //check form { $errs = array(); $qtmp = array(); foreach($_POST as $fld => $val) { $val = trim(stripslashes($val)); switch($fld) { case 'title': case 'description': case 'location': if (strlen($val) == 0) $errs[] = 'Please enter the ' . ucwords($fld); else $qtmp[] = $fld . " = '" . mysql_real_escape_string($val) . "'"; break; case 'Eventcat': $qtmp[] = $fld . " = '" . mysql_real_escape_string($val) . "'"; break; } } if (!empty($errs)) echo implode("<br>\n",$errs) . "<br>\n"; else { //format datetimes $startDT="$syear-$smonth-$sday $shour:$sminute:00"; $endDT="$eyear-$emonth-$eday $ehour:$eminute:00"; $today= date("Y-m-d h:i:s"); $qtmp[] = "createDate = '" . $today . "'"; $qtmp[] = "startDT = '" . $startDT . "'"; $qtmp[] = "endDT = '" . $endDT . "'"; //connect to database $cnx = mysqli_connect($host, $user,$password,$dbname) or die ("Couldn't connect to server."); //create query $sql = "INSERT INTO Posttable set " . implode(', ',$qtmp); //execute query $result = mysqli_query($cnx,$sql) or die ("Error in query: $sql. " . mysqli_error()); //close connection mysqli_close($cnx); } } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/141146-check-for-blanks-in-form-problem/#findComment-738826 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.