driz Posted November 18, 2008 Share Posted November 18, 2008 Hi, I am building a PHP guestbook for a university assignment that will collect some basic user info, save it to a text file, and then spit it back out. However my code isn't saving the data to the text file, where is the error? Thank you. x <?php// PHP Guestbook// CAMERON DRYSDALE (u0558234)// variables$filename = "entries.txt";$date = date('d / F / Y');$time = date('h:i a');$name = $_POST['name'];$email = $_POST['email'];$website = $_POST['website'];$message = $_POST['message'];$submit = $_POST['submit'];// when user submits the formif($submit=="submit") { //save to contents to the txt file $fp=fopen($filename, 'a+'); // in this format $fstring="||" . $date . "||" . $time . "||" . $name . "||" . $email . "||" . $website . "||" . $message . "\n\r"; fwrite($fp, $fstring); fclose($fp); }; ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>PHP Guestbook</title> <link rel="stylesheet" type="text/css" href="stylesheet.css" /> </head> <body> <div id="wrapper"> <div id="sidebar"> <form name="guestbook" method="post" action="index.php"> <fieldset> <legend>Add Comment</legend> <div class="pad"> <ul> <li> <label for="name"><strong>*</strong>Name</label> <input type="text" name="name" size="30" /> </li> <li> <label for="email"><strong>*</strong>Email</label> <input type="text" name="email" size="30" /> </li> <li> <label for="website">Website</label> <input type="text" name="website" size="30" /> </li> <li> <label for="message"><strong>*</strong>Message</label> <textarea name="message"></textarea> </li> <li> <label for="submit"> </label> <button type="submit" class="submitBtn"><span>Submit</span></button> <button type="reset" class="resetBtn"><span>Cancel</span></button> </li> <ul> <p><strong>*</strong> = required</p> </div> </fieldset> </form> <div id="footer"> <p>© 2008 CAMERON DRYSDALE (u0558234)</p> </div> </div><!-- /sidebar--> <div id="content"> <div class="padding"> <h1>PHP Guestbook</h1> <h2>by CAMERON DRYSDALE (u0558234)</h2> <?php // begin printing the entries container echo("<div class=\"listcomments\">"); if($submit!="submit") { // grabs the posts from the txt file $filecontents=file($filename); // sets what happens for each line in the txt file foreach($filecontents as $line) { // gets rid of the un-wanted seperators in the txt file $data=explode("||" , $line); //checks for blank entries before displaying entries if (trim($line) != "") { echo("<div class\"comment\""); echo("<div class=\"message\"><p>" . $data[5] . "</p></div>"); echo("<div class=\"author\""); echo("<p><img src=\"img/avatar.png\" class=\"user\" /><span>" . $data[2] . "</span>"); echo("" . $data[0] . "" . $data[1] . "<br />"); echo("" . $data[4] . "</p>"); // echo("<p class=\"email\"><span>Email address:</span> " . $data[3] . "</p>"); echo("</div>"); echo("</div>"); } }; }; // if user has clicked the submit button, display thankyou message if($submit=="submit") { echo("<div class=\"popup\"><p>Thanks! Your comment has been added.</p></div>"); }; // ends the entries echo("</div>"); ?> </div><!-- /padding--> </div><!-- /content--> </div><!-- /wrapper --> </body></html> Link to comment Share on other sites More sharing options...
Lulzim Posted November 18, 2008 Share Posted November 18, 2008 replace this <button type="submit" class="submitBtn"><span>Submit</span></button> with this <input type="submit" class="submitBtn" value='submit' name='submit' /> Link to comment Share on other sites More sharing options...
driz Posted November 18, 2008 Author Share Posted November 18, 2008 Why can't I use button? Link to comment Share on other sites More sharing options...
Lulzim Posted November 18, 2008 Share Posted November 18, 2008 it is because to submit a form you have to have input of type submit, you can use button but you'll have to add some javascript code to submit the form Link to comment Share on other sites More sharing options...
driz Posted November 18, 2008 Author Share Posted November 18, 2008 I thought the button element was the W3C standard? Why do you need JavaScript to use it here? I use it on other things and it works fine :S Link to comment Share on other sites More sharing options...
driz Posted November 18, 2008 Author Share Posted November 18, 2008 Are you saying that the button element can't be used to post with PHP? Or is it just my code? Link to comment Share on other sites More sharing options...
Synook Posted November 18, 2008 Share Posted November 18, 2008 The <button> tag is a standard element... you just don't use it to submit forms. That is what <input type="submit" /> is for! Link to comment Share on other sites More sharing options...
driz Posted November 18, 2008 Author Share Posted November 18, 2008 The <button> tag is a standard element... you just don't use it to submit forms. That is what <input type="submit" /> is for!That makes styling difficult if you are using the sliding doors effect, how would I get around this then? Add an onclick event? Or not? <script language="JavaScript">function submitform(){ document.guestbook.submit();}</SCRIPT> <button type="submit" class="submitBtn" onclick="submitform();"><span>Post Comment</span></button> Link to comment Share on other sites More sharing options...
Synook Posted November 18, 2008 Share Posted November 18, 2008 * The language attribute is deprecated and not well-implemented, use type* Give your form an id not name <script type="text/javascript">function submitform() { document.getElementById("guestbook").submit();}</script> <button type="submit" class="submitBtn" onclick="submitform();"><span>Post Comment</span></button> For sliding doors you could just use a wrapper that goes outside the input tag... <span><input type="submit" /></span> Link to comment Share on other sites More sharing options...
driz Posted November 18, 2008 Author Share Posted November 18, 2008 Hmm tried that, doesn't seem to be having any effect, the button is just acting as it did before, and not submitting the info. xEDIT: It doesn't need JS at all, all I needed was to add value='submit' to the button and it works, so button does work with form! Link to comment Share on other sites More sharing options...
driz Posted November 19, 2008 Author Share Posted November 19, 2008 I have began working on a swear word filter: <?php function filterWords($str) { // words to filter $words=array("bollocks", "######", "######", "bastard", "crap"); // beep the word $beep=array("*BEEP*"); } ?> This function needs to replace swear words found in the text file and display *BEEP* instead of the word. I'm a little stuck as to how to do this? Link to comment Share on other sites More sharing options...
Raize Posted November 19, 2008 Share Posted November 19, 2008 I have began working on a swear word filter:...This function needs to replace swear words found in the text file and display *BEEP* instead of the word. I'm a little stuck as to how to do this?Create an array of the words you want to get rid of, and an array for their replacements. In this case, since they're all being replaced with the same thing, the replacement can be a regular variable. Then use a "string case-insensitive replace" with those values:<?php$string = "That's bullocks! What the bloody heck is going on in this bloomin place?";$badwords = array('bullocks', 'bloody', 'bloomin');$goodwords = '*BEEP*';$string = str_ireplace($badwords, $goodwords, $string);print($string);?> The above will produce:That's *BEEP*! What the *BEEP* heck is going on in this *BEEP* place?This will, of course, only replace those words that match exactly your "badwords" array. If you want to get more complex, you'll need the use of regular expressions.Hope that helps. Link to comment Share on other sites More sharing options...
driz Posted November 19, 2008 Author Share Posted November 19, 2008 Hi, that's what I already kind of have, although I never thought to use a variable for the beep seen as its just one word, but my problem is how to get this to work with my guestbook, this needs to check . data[5] . on my code. See top of thread. x Link to comment Share on other sites More sharing options...
Raize Posted November 19, 2008 Share Posted November 19, 2008 Wouldn't that be $clean = str_ireplace($find, $replace, $data[5]);? Link to comment Share on other sites More sharing options...
driz Posted November 20, 2008 Author Share Posted November 20, 2008 This is my code:BUT it throws up an error with that replace, Fatal error: Call to undefined function: str_ireplace() in /data/members/paid/d/r/driz.co.uk/htdocs/www/guestbook/index.php on line 152 <?php // words to filter $words=array("bugger", "dammit", "bloody", "crap"); // beep the word $beep=array("*BEEP*"); $filtered = str_ireplace($words, $beep, $data[6]); // start with container for comments echo("<div class=\"listcomments\">"); // if user submits a comment if($submit!="submit") { // get comments from the text file $filecontents=file($filename); // sets what happens for each line in the text file foreach($filecontents as $line) { // gets rid of the un-wanted seperators in the text file $data=explode("||" , $line); // check for blank entries before displaying entries if (trim($line) != "") { $i++; if($i&1) { echo("<div class\"comment\">"); echo("<div class=\"message\"><p>" . $data[6] . "</p></div>"); echo("<div class=\"author\">"); echo("<p><img src=\"img/avatar.png\" class=\"user\" /><span><a href=\"mailto:$data[4]\">" . $data[3] . "</a></span>"); echo("" . $data[1] . " @ " . $data[2] . "<br />"); echo("" . $data[5] . "</p>"); echo("</div>"); echo("</div>"); } else { echo("<div class\"commentalt\">"); echo("<div class=\"messagealt\"><p>" . $data[6] . "</p></div>"); echo("<div class=\"authoralt\">"); echo("<p><img src=\"img/avataralt.png\" class=\"useralt\" /><span><a href=\"mailto:$data[4]\">" . $data[3] . "</a></span>"); echo("" . $data[1] . " @ " . $data[2] . "<br />"); echo("" . $data[5] . "</p>"); echo("</div>"); echo("</div>"); } } }; }; // if user has clicked the submit button, display thankyou message if($submit=="submit") { echo("<div class=\"alert\"><p>Thanks! Your comment has been added.</p></div>"); }; // closes the comments container echo("</div>"); ?> Link to comment Share on other sites More sharing options...
Raize Posted November 20, 2008 Share Posted November 20, 2008 Yikes. "Call to undefined function" means that you're trying to call a function that you haven't defined yet (in other words, you're trying to use a function that doesn't exist)I have PHP running on both a Linux-based web server as well as my Windows-based workstation, and str_ireplace works on both without a problem. So I don't know if it's something in your config or what.. Sorry. Link to comment Share on other sites More sharing options...
driz Posted November 20, 2008 Author Share Posted November 20, 2008 I'm running the code on a different server this time, and its not complaining about the function, but its not filtering the words either? Is my filter code in the wrong area? Thanks. x Link to comment Share on other sites More sharing options...
Raize Posted November 20, 2008 Share Posted November 20, 2008 I believe the problem is this line here:echo("<div class=\"message\"><p>" . $data[6] . "</p></div>");str_ireplace doesn't actually change the original value, it just returns the results. So when you use:$filtered = str_ireplace($words, $beep, $data[6]);The value of $data[6] doesn't change. The results of the filter are now stored in the $filtered variable. So the line above should be:echo("<div class=\"message\"><p>$filtered</p></div>");Make sense? Link to comment Share on other sites More sharing options...
driz Posted November 20, 2008 Author Share Posted November 20, 2008 Right I've managed to get that working, seems I should of put the code inside of my if submit statement. Link to comment Share on other sites More sharing options...
driz Posted November 20, 2008 Author Share Posted November 20, 2008 DOUBLE POST Link to comment Share on other sites More sharing options...
Raize Posted November 21, 2008 Share Posted November 21, 2008 I'm not sure what that "DOUBLE POST" thing was about...Is everything working then? Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.