
Gemini 🤖
Members-
Posts
37 -
Joined
-
Last visited
Never
Everything posted by Gemini 🤖
-
Hi, this is a simple question but I've been struggling for days to find this parse error and I haven't been able to find it! I get a parse error on line 127. This is the line that has all of the !isset statements. <div id="mainnew"> <a href="index.php?content=neweditorials"><img border="0" src="images/neweditorials.png" /> </a> </div> <div id="maintoprated"> <a href="index.php?content=topratededitorials"><img border="0" src="images/topratededitorials.png" /> </a> </div> <div id="mainmostpopular"> <a href="index.php?content=mostpopulareditorials"><img border="0" src="images/mostpopulareditorials.png" /></a> </div> <?php function newForm() { $username=$_POST['username']; $password=$_POST['password']; $password2=$_POST['password2']; $email=$_POST['email']; $gender=$_POST['gender']; ?> <div align="left" class="maincontent"> <div class='boldunderl'>Create Your Account</div><br /> <form method="post" id="createaccountform" action="index.php?content=addaccount"> <div> <label for="username">Username<br /></label> <input id="username" name="username" type="text" value="<?php echo $username; ?>" /> </div> <br /> <div> <label for="password">Password<br /></label> <input id="password" name="password" type="text" value="<?php echo $password; ?>" /> </div> <br /> <div> <label for="password2">Confirm Password<br /></label> <input id="password2" name="password2" type="text" value="<?php echo $password2; ?>" /> </div> <br /> <div> <label for="email">Email Address<br /></label> <input id="email" name="email" type="text" value="<?php echo $email; ?>" /> </div> <br /> <div> <label for="gender">Gender<br /></label> <?php if($gender=='Male') { ?> <input id="gender" name="gender" type="radio" value="Male" checked />Male <input id="gender" name="gender" type="radio" value="Female" />Female <?php }else{ ?> <input id="gender" name="gender" type="radio" value="Male" />Male <input id="gender" name="gender" type="radio" value="Female" checked />Female <?php } ?> </div> <br /> <div align="center"> <input id="send" name="send" type="image" value="Submit" src="images/submit.png" /> <input type="hidden" name="addaccount" value="addaccount" /> </div> <br /> </form> </div> <?php } ?> <?php $username=$_POST['username']; $password1=$_POST['password']; $password2=$_POST['password2']; $email=$_POST['email']; $gender=$_POST['gender']; if((strlen($username)) < 3) { $error1=array('username1'=>'<b>Error:</b> Your username must be at least 3 characters long!<br>'); } if(preg_match("/([^0-9a-zA-Z ])/" , $username)) { $error2=array('username2'=>'<b>Error:</b> Your username must contain only letters or numbers!<br>'); } if((empty($password1))) { $error3=array('password1'=>'<b>Error:</b> You haven\'t chosen a password!<br>'); } if((empty($password2))) { $error4=array('password2'=>'<b>Error:</b> You haven\'t confirmed your password!<br>'); } if($password1!=$password2) { $error5=array('password3'=>'<b>Error:</b> Your passwords do not match!<br>'); } if((empty($email))) { $error6=array('email1'=>'<b>Error:</b> You haven\'t filled in your email address!<br>'); } if(preg_match("/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/" , $email)) { $error7=array('email2'=>'<b>Error:</b> Your email format is incorrect!<br>'); } if((empty($gender))) { $error8=array('gender1'=>'<b>Error:</b> You must select a gender!<br>'); } if((strlen($username)) > 15) { $error9=array('username3'=>'<b>Error:</b> Your username must not be more than 15 characters long!<br>'); } if (!isset($error1) && !isset($error2) && !isset($error3) && !isset($error4) && !isset($error5) && !isset($error6) && !isset($error7) !isset($error8) !isset($error9)) { include("config.php"); include("dbconnect.php"); $query="INSERT INTO users (username, password, email, gender, date) VALUES ('$username', '$password1', '$email', '$gender', NOW() )"; $result=mysql_query($query) or die('Sorry, could not query the database'." Error: ".mysql_error()); if ($result) { $categoryout = str_replace (" ", "_", $categoryselect); $titleout = str_replace (" ", "_", $editorialtitle); header("Location:index.php?content=main&id=$categoryout&title=$titleout"); }else { echo "<div class=boldcenter>Sorry, there was a problem posting your editorial</div>"; } } else { echo'<div class=formerror align=left>'; if(isset($error1['username1'])){echo $error1['username1'];} if(isset($error2['username2'])){echo $error2['username2'];} if(isset($error9['username3'])){echo $error9['username3'];} if(isset($error3['password1'])){echo $error3['password1'];} if(isset($error4['password2'])){echo $error4['password2'];} if(isset($error5['password3'])){echo $error5['password3'];} if(isset($error6['email1'])){echo $error6['email1'];} if(isset($error7['email2'])){echo $error7['email2'];} if(isset($error8['gender1'])){echo $error8['gender1'];} echo'</div>' , '<br>'; newForm(); } ?> </div> The code is not finished yet, I have not finished changing some of the words, the header location, or the security yet. Right now I'm just focused on finding the parse error. Can anybody help? Thanks!
-
Thanks a lot! I actually am writing to note that I just got the script to work correctly, but I believe I have cheated around the system a little. I have assigned each error to its own variable so that the affected section of code now looks like this... if((empty($categoryselect))) { $error1=array('categoryselect'=>'<div class="formerror"><div class=boldcenter>Error: You must choose a category!</div></div><br>'); } if((empty($editorialtitle))) { $error2=array('editorialtitle1'=>'<div class="formerror"><div class=boldcenter>Error: Your title field is empty!</div></div><br>'); } if((empty($editorialcontent))) { $error3=array('editorialcontent1'=>'<div class="formerror"><div class=boldcenter>Error: Your editorial field is empty!</div></div><br>'); } if((strlen($editorialtitle)) < 4) { $error4=array('editorialtitle2'=>'<div class="formerror"><div class=boldcenter>Error: Your title must be at least 4 characters long!</div></div><br>'); } if((str_word_count($editorialcontent)) < 50) { $error5=array('editorialcontent2'=>'<div class="formerror"><div class=boldcenter>Error: Your editorial must be at least 50 words long!</div></div><br>'); } if (!isset($error1) && !isset($error2) && !isset($error3) && !isset($error4) && !isset($error5)) { include("config.php"); include("dbconnect.php"); $query="INSERT INTO editorials (category, date, time, title, article) VALUES ('$categoryselect', NOW(), CURTIME(), '$editorialtitle', '$editorialcontent')"; $result=mysql_query($query) or die('Sorry, could not query the database'."Error: ".mysql_error); if ($result) { echo "<div class=boldcenter>Your editorial has been successfully posted!</div>\n"; }else { echo "<div class=boldcenter>Sorry, there was a problem posting your editorial</div>"; } } else { if(isset($error1['categoryselect'])){echo $error1['categoryselect'];} if(isset($error2['editorialtitle1'])){echo $error2['editorialtitle1'];} if(isset($error3['editorialcontent1'])){echo $error3['editorialcontent1'];} if(isset($error4['editorialtitle2'])){echo $error4['editorialtitle2'];} if(isset($error5['editorialcontent2'])){echo $error5['editorialcontent2'];} newForm(); } Is this necessary to do, or should I be able to use the $error variable for all of the assigned arrays?
-
Anybody?
-
Sorry, but the reason I included the code of the entire page is because I don't know where the error is. The javascript is absolutely not a waste as it sets up a WYSIWYG editor in the textarea for guests to format their submissions, just like this forum. I attempted to do this and had no luck. I then even removed this code altogether and still get the same result. I'm out of ideas!
-
Does anybody have any ideas as to why all of the arrays are not getting their values set? I don't think the problem is with the echo because, as I stated before, I tried removing all the if statements from all the checks so that it would definitely add an item to each array, and I removed the checks on the echos for the contents of the arrays. The results of this were I got many undefined indexes (which I shouldn't have because they were all declared) and I only got one error message, the last echo in the list....
-
phpdragon, No problem, i'm glad that more than one person learned something from this So I have figured out how to get the previously selected item from the drop down list to show up, and have successfully implemented TinyMCE as a WYSIWYG textarea replacement. So now....my only problem. Here is the current code: <script type="text/javascript" src="tiny_mce/tiny_mce.js"></script> <script type="text/javascript"> tinyMCE.init({ theme : "advanced", convert_urls : false, mode : "textareas" }); </script> <div align="center" class="maincontent"> <?php if (array_key_exists('submit',$_POST)){ //Form has been submitted } $categoryselect=$_POST['categoryselect']; function newForm() { $categoryselect=$_POST['categoryselect']; $editorialtitle=$_POST['editorialtitle']; $editorialcontent=$_POST['editorialcontent']; ?> <div class="maincontent"> <div class='boldunderl'>Post an Editorial</div><br /> <form method="post" id="addeditorialform" action="index.php"> <div> <label for="categoryselect">Category<br /></label> <select name='categoryselect'><option value="<?php echo $categoryselect; ?>" name="<?php echo $categoryselect; ?>" /><?php echo $categoryselect; ?></option> <?php include("config.php"); include("dbconnect.php"); $query="SELECT category FROM categories ORDER BY category"; $result=mysql_query($query) or die('Sorry, could not query the database'."Error: ".mysql_error); while($row=mysql_fetch_assoc($result)){ $categoryselect=$row['category']; echo"<option value='$categoryselect' name='$categoryselect'>$categoryselect</option>"; } ?> </select> </div> <br /> <div> <label for="editorialtitle">Title of Editorial<br /></label> <input id="editorialtitle" name="editorialtitle" type="text" value="<?php echo $editorialtitle; ?>" /> </div> <br /> <div> <label for="editorialcontent">Editorial Content</label> <br /> <textarea id="editorialcontent" name="editorialcontent" cols="40" rows="15"><?php echo $editorialcontent; ?></textarea> </div> <br /> <div> <input id="send" name="send" type="image" value="Submit" src="images/submit.png" /> <input type="hidden" name="content" value="addeditorial" /> </div> <br /> </form> </div> <?php } ?> <?php function cleanUp($data) { $data = trim(strip_tags(htmlspecialchars($data))); return $data; } $categoryselect=$_POST['categoryselect']; $editorialtitle=$_POST['editorialtitle']; $editorialcontent=$_POST['editorialcontent']; cleanUp($editorialtitle); cleanUp($editorialcontent); if (!get_magic_quotes_gpc()) { $categoryselect=addslashes($categoryselect); $editorialtitle=addslashes($editorialtitle); $editorialcontent=addslashes($editorialcontent); } if((empty($categoryselect))) { $error=array('categoryselect'=>'<div class="formerror"><div class=boldcenter>Error: You must choose a category!</div></div><br>'); } if((empty($editorialtitle))) { $error=array('editorialtitle1'=>'<div class="formerror"><div class=boldcenter>Error: Your title field is empty!</div></div><br>'); } if((empty($editorialcontent))) { $error=array('editorialcontent1'=>'<div class="formerror"><div class=boldcenter>Error: Your editorial field is empty!</div></div><br>'); } if((strlen($editorialtitle)) < 4) { $error=array('editorialtitle2'=>'<div class="formerror"><div class=boldcenter>Error: Your title must be at least 4 characters long!</div></div><br>'); } if((str_word_count($editorialcontent)) < 50) { $error=array('editorialcontent2'=>'<div class="formerror"><div class=boldcenter>Error: Your editorial must be at least 50 words long!</div></div><br>'); } if (!isset($error)) { include("config.php"); include("dbconnect.php"); $query="INSERT INTO editorials (category, date, time, title, article) VALUES ('$categoryselect', NOW(), CURTIME(), '$editorialtitle', '$editorialcontent')"; $result=mysql_query($query) or die('Sorry, could not query the database'."Error: ".mysql_error); if ($result) { echo "<div class=boldcenter>Your editorial has been successfully posted!</div>\n"; }else { echo "<div class=boldcenter>Sorry, there was a problem posting your editorial</div>"; } } else { if(isset($error['categoryselect'])){echo $error['categoryselect'];} if(isset($error['editorialtitle1'])){echo $error['editorialtitle1'];} if(isset($error['editorialcontent1'])){echo $error['editorialcontent1'];} if(isset($error['editorialtitle2'])){echo $error['editorialtitle2'];} if(isset($error['editorialcontent2'])){echo $error['editorialcontent2'];} newForm(); } ?> </div> Basically, I first added a 1 and a 2 to the end of fields that have the same name but can have different error messages. My current problem is this. Only the last array if(isset($error['editorialcontent2'])){echo $error['editorialcontent2'];} is working! So if you submit a form, you get only one error which is this last error. I tried removing all the if statements from all the checks so that it would definitely add an item to each array, and I removed the checks on the echos for the contents of the arrays. The results of this were I got many undefined indexes (which I shouldn't have because they were all declared) and I only got one error message, the last echo in the list.... Can anyone see the problem? I have been unable to figure it out.
-
Hmm i'm pretty sure that selected="selected" is the XHTML compliant version of that? In either case, how would I go about adding that to the last selected option in the menu? I know I have to use the $categoryselect command, but where and how do I implement this?
-
Thanks, that did the trick! I understand what you mean about the variables having to exist. The current code however cannot display different combinations of errors. For example when a blank form is submitted, only "Error: Your editorial must be at least 50 words long!" shows up! Also, does anybody know how to get the <select> drop-down menu to display the last chosen variable? Or about adding a WYSIWYG editor to the <textarea>? Thanks!
-
Hi dt192, Thanks for the suggestion, but that did not work. I don't understand why it can't find it...
-
First of all, I would like to thank you for the excellent help...I really couldn't appreciate it more. Unfortunately, I still have a few problems that for the life of me, I cannot figure out! First of all, the value field in the select menu is not a valid option. I know that selected="selected" is the way to select a value by default and this is put in the <option> tag. For example, <option value="Business" selected="selected"> will select the Business field by default. Second of all, I changed the double t's in edittorialtitle back to editorialtitle as it should be. Then, I rant the form without any input. The echo $error['editorialcontent']; works beautifully, yet the echo $error['editorialtitle']; throws an error Notice: Undefined index: editorialtitle in C:\wamp\www\udeclare\addeditorial.inc.php on line 100 Here is the current code for reference: <div align="center" class="maincontent"> <?php if (array_key_exists('submit',$_POST)){ //Form has been submitted } function newForm() { $categoryselect=$_POST['categoryselect']; $editorialtitle=$_POST['editorialtitle']; $editorialcontent=$_POST['editorialcontent']; ?> <div class="maincontent"> <h1>Post an Editorial</h1> <form method="post" id="addeditorialform" action="index.php"> <div> <label for="categoryselect">Category<br /></label> <select name="categoryselect"><option value="Business">Business</option><option value="Current Events">Current Events</option><option value="Economy">Economy</option><option value="Government Policy">Government Policy</option><option value="Philosophy">Philosophy</option><option value="Products">Products</option><option value="Science">Science</option><option value="Technology">Technology</option></select> </div> <br /> <div> <label for="editorialtitle">Title of Editorial<br /></label> <input id="editorialtitle" name="editorialtitle" type="text" value="<?php echo $editorialtitle; ?>" /> </div> <br /> <div> <label for="editorialcontent">Editorial Content</label> <br /> <textarea id="editorialcontent" name="editorialcontent" cols="40" rows="15"><?php echo $editorialcontent; ?></textarea> </div> <br /> <div> <input id="send" name="send" type="submit" value="Submit" /> <input type="hidden" name="content" value="addeditorial" /> </div> <br /> </form> </div> <?php } ?> <?php function cleanUp($data) { $data = trim(strip_tags(htmlspecialchars($data))); return $data; } $categoryselect=$_POST['categoryselect']; $editorialtitle=$_POST['editorialtitle']; $editorialcontent=$_POST['editorialcontent']; cleanUp($editorialtitle); cleanUp($editorialcontent); if (!get_magic_quotes_gpc()) { $categoryselect=addslashes($categoryselect); $editorialtitle=addslashes($editorialtitle); $editorialcontent=addslashes($editorialcontent); } if((empty($editorialtitle))) { $error=array('editorialtitle'=>'<div class="formerror"><h3>Error: Your title field is empty!</h3></div><br>'); } if((empty($editorialcontent))) { $error=array('editorialcontent'=>'<div class="formerror"><h3>Error: Your editorial field is empty!</h3></div><br>'); } if((strlen($editorialtitle)) < 4) { $error=array('editorialtitle'=>'<div class="formerror"><h3>Error: Your title must be at least 4 characters long!</h3></div><br>'); } if((str_word_count($editorialcontent)) < 50) { $error=array('editorialcontent'=>'<div class="formerror"><h3>Error: Your editorial must be at least 50 words long!</h3></div><br>'); } if (!isset($error)) { include("config.php"); include("dbconnect.php"); $query="INSERT INTO editorials (category, date, time, title, article) VALUES ('$categoryselect', NOW(), CURTIME(), '$editorialtitle', '$editorialcontent')"; $result=mysql_query($query) or die('Sorry, could not query the database'."Error: ".mysql_error); if ($result) { echo "<h3>Your editorial has been successfully posted!</h3>\n"; }else { echo "<h3>Sorry, there was a problem posting your editorial</h3>"; } } else { echo $error['editorialtitle']; echo $error['editorialcontent']; //Best part about this technique is it is super small, and if there is an error it will be printed if not nothing is printed. newForm(); } ?> </div> Next, it seems as if this array will output fine if there is a single error on the form, but if there is more than one error it seems as if it will not work?? Or do I just need an explanation for this? Lastly, and this is a little off topic, but how would I add a WYSIWYG text editor to the <textarea> field on the form that holds 'editorialcontent'? Like on this forum? Once again, thanks for all the help. I've learned more already than I could in a long time by just reading tutorials of other people's scripts.
-
Hi, thanks for the reply. I understand now what you mean about double quotes and have replaced them with the singe quotes. The page is not supposed to be accessed directly because of security issues with spam from search engines, or at least thats my thinking. The reason there is so many echoes is because it is the only way I know of showing the user the form with the information that was already filled in. (I do not know how to show the complete drop down menu on the second form though). I understand what you mean about creating the arrays and then checking if they are present, but how do I deal with more than one error present (i.e. more than one array?) I am not sure how I would be able to output only one error or another, or multiple errors using arrays. Lastly, I know security is important, but what exactly is it that is insecure about this script? I tried to remove the PHP and HTML tags and convert the remaining characters into text using htmlspecialchars. What else can I do? Thanks!
-
Hello. First of all let me state that I am new to PHP and have only begun to learn it. Today I set off trying to create my own server-side PHP form validation that would alert the user if certain parts of the form were not filled out correctly without deleting the user's data. The solution that I have come up with works, but it is very inefficient and cumbersome. Here is the code of the form: <div align="center" class="maincontent"> <h1>Post an Editorial</h1> <form method="post" id="addeditorialform" action="index.php"> <div> <label for="categoryselect">Category<br /></label> <select name="categoryselect"><option value="Business">Business</option><option value="Current Events">Current Events</option><option value="Economy">Economy</option><option value="Government Policy">Government Policy</option><option value="Philosophy">Philosophy</option><option value="Products">Products</option><option value="Science">Science</option><option value="Technology">Technology</option></select> </div> <br /> <div> <label for="editorialtitle">Title of Editorial<br /></label> <input id="editorialtitle" name="editorialtitle" type="text" /> </div> <br /> <div> <label for="editorialcontent">Editorial Content</label> <br /> <textarea id="editorialcontent" name="editorialcontent" cols="40" rows="15"></textarea> </div> <br /> <div> <input id="send" name="send" type="submit" value="Submit" /> <input type="hidden" name="content" value="addeditorial" /> </div> <br /> </form> </div> And here is the code of the validation/submition file: <div align="center" class="maincontent"> <?php if (!isset($_POST['send'])) { echo "<h1>Error: </h1> <p>Accessing this page directly is not allowed.</p>"; exit; } global $error; $error=0; function newForm() { $categoryselect=$_POST['categoryselect']; $editorialtitle=$_POST['editorialtitle']; $editorialcontent=$_POST['editorialcontent']; echo"<div class=\"maincontent\">"; echo"<h1>Post an Editorial</h1>"; echo"<form method=\"post\" id=\"addeditorialform\" action=\"index.php\">"; echo"<div>"; echo"<label for=\"categoryselect\">Category<br /></label>"; echo"<select name=\"categoryselect\"><option value=\"$categoryselect\">$categoryselect</option></select>"; echo"</div>"; echo"<br />"; echo"<div>"; echo"<label for=\"editorialtitle\">Title of Editorial<br /></label>"; echo"<input id=\"editorialtitle\" name=\"editorialtitle\" type=\"text\" value=\"$editorialtitle\" />"; echo"</div>"; echo"<br />"; echo"<div>"; echo"<label for=\"editorialcontent\">Editorial Content</label>"; echo"<br />"; echo"<textarea id=\"editorialcontent\" name=\"editorialcontent\" cols=\"40\" rows=\"15\">$editorialcontent</textarea>"; echo"</div>"; echo"<br />"; echo"<div>"; echo"<input id=\"send\" name=\"send\" type=\"submit\" value=\"Submit\" />"; echo"<input type=\"hidden\" name=\"content\" value=\"addeditorial\" />"; echo"</div>"; echo"<br />"; echo"</form>"; echo"</div>"; } function cleanUp($data) { $data = trim(strip_tags(htmlspecialchars($data))); return $data; } $categoryselect=$_POST['categoryselect']; $editorialtitle=$_POST['editorialtitle']; $editorialcontent=$_POST['editorialcontent']; cleanUp($editorialtitle); cleanUp($editorialcontent); if (!get_magic_quotes_gpc()) { $categoryselect=addslashes($categoryselect); $editorialtitle=addslashes($editorialtitle); $editorialcontent=addslashes($editorialcontent); } if((empty($editorialtitle))) { $error=$error+1; } if((empty($editorialcontent))) { $error=$error+4; } if((strlen($editorialtitle)) < 4) { $error=$error+8; } if((str_word_count($editorialcontent)) < 50) { $error=$error+16; } switch ($error) { case 0: include("config.php"); include("dbconnect.php"); $query="INSERT INTO editorials (category, date, time, title, article) VALUES ('$categoryselect', NOW(), CURTIME(), '$editorialtitle', '$editorialcontent')"; $result=mysql_query($query) or die('Sorry, could not query the database'."Error: ".mysql_error); if ($result) { echo "<h3>Your editorial has been successfully posted!</h3>\n"; }else { echo "<h3>Sorry, there was a problem posting your editorial</h3>"; } break; case 8: echo"<div class=\"formerror\"><h3>Error: Your title must be at least 4 characters long!</h3></div><br>"; newForm(); break; case 9: echo"<div class=\"formerror\"><h3>Error: Your title must be at least 4 characters long!</h3></div><br>"; newForm(); break; case 16: echo"<div class=\"formerror\"><h3>Error: Your editorial must be at least 50 words long!</h3></div><br>"; newForm(); break; case 20: echo"<div class=\"formerror\"><h3>Error: Your editorial must be at least 50 words long!</h3></div><br>"; newForm(); break; default: echo"<div class=\"formerror\">"; echo"<h3>Error: Your title must be at least 4 characters long!</h3>"; echo"<h3>Error: Your editorial must be at least 50 words long!</h3>"; echo"</div>"; newForm(); break; } ?> </div> As you can see, the form is a very simple one with three fields. I have created cases in a switch structure for every possible outcome of form errors, which like I stated above is extremely cumbersome and difficult. It also takes a lot of code. I want to find a way to shrink this code down. As a final result, I want the MAINCONTAINER div to hold the results of the exact error, not a general error. I tried setting up an array for the if((empty($editorialtitle))) condition and using push_array for the rest, but that only worked if there was an error in the if((empty($editorialtitle))) Furthermore, when there was an error in that condition, and an array could be successfully output, I could not figure out what to do after that. I attempted to use in_array and array_key_exists but could not find a solution to my problem. Thanks for taking the time to read this. Can you help me?