Why doesn't this code work?
09-11-2009, 05:10 PM
|
Why doesn't this code work?
|
Posts: 824
Name: Scott
Location: Ontario
|
Hi,
I have a button that is supposed to trigger the PHP fwrite function, but it's not working...
Here's what I have:
PHP Code:
<?php $reviewTitle = $_POST["reviewtitle"];
$reviewAuthor = $_POST["authorname"];
$reviewContent = $_POST["reviewcontent"];
$reviewFull = "<div id='contentboxtitle'><h1>". $reviewTitle . "</h1></div><div id='contentbox'><em>A review by " . $reviewAuthor . "</em><br /><br />" . $reviewContent . "</div>";
$fp = fopen("ffdatabase.txt","a");
if(!$fp) { echo 'Error: Cannot open file.'; exit; }
function addReview($fp, $reviewFull) { fwrite($fp, $reviewFull."\n"); }
fclose($fp); ?>
...PAGE CONTENT...
<?php echo " <form action='process.php' /> <input type='button' value='Add Review' onclick='addReview()' /> </form> "; ?>
Why doesn't this work? I don't get it, when the button is clicked, it triggers the funtion 'addReview' which triggers fwrite. But it doesn't work!
Why? How can I fix this? Should I make it a submit button and onsubmit?
By the way, the name of the page this is on is called 'process.php'. I want the form to go through itself, but is that bad?
All right, when I have this instead:
PHP Code:
<?php echo " <form onsubmit='addReview()' /> <input type='submit' value='Add Review' /> </form> "; ?>
It deletes all the content, and doesn't add to the database. WTH?
Am I fclose-ing it too soon?
Here's the entire code for process.php:
Code:
<?php
$reviewTitle = $_POST["reviewtitle"];
$reviewAuthor = $_POST["authorname"];
$reviewContent = $_POST["reviewcontent"];
$reviewRating = $_POST["reviewrating"];
$ratingReason = $_POST["ratingreason"];
$reviewFull = "<div id='contentboxtitle'><h1>". $reviewTitle . "</h1></div><div id='contentbox'><em>A review by " . $reviewAuthor . "</em><br /><br />" . $reviewContent . "</div><div id='contentboxtitle'><h3>Rating: " . $reviewRating . "</h3></div><div id='contentbox'>" . $ratingReason . "</div>";
$fp = fopen("ffdatabase.txt","a");
if(!$fp) {
echo 'Error: Cannot open file.';
exit;
}
function addReview($fp, $reviewFull) {
fwrite($fp, $reviewFull."\n");
}
fclose($fp);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Physicsguy's Reviews - <?php echo ("$reviewTitle"); ?> - User Review</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="Scott Kaye" />
<meta name="description" content="Physicsguy's Reviews - Find tutorials, games, reviews, and even guitar tablature on Physicsguy's Reviews, your site for everything!" />
<meta name="keywords" content="Reviews, Games, Software, Tablature, Guitar, Hero, Rock, Band, Physicsguy, Physicsguy's, Reviews, Physics, Phun, Wii, PS3, Playstation, Nintendo, Linux, Ubuntu, Xmoto, Inkscape, HTML, CSS, Super, Mario, Bros, Theme, Legend, Of, Zelda, Scott, Kaye, Scott Kaye, Cool, Site" />
<link rel="stylesheet" href="physicsguystyle2.css" type="text/css" media="screen" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="lavatheme" href="physicsguystyle.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="rocktheme" href="physicsguystyle3.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="darkmattertheme" href="physicsguystyle4.css" />
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
<link rel="icon" href="images/favicon.ico" type="image/x-icons" />
<meta name="viewport" content="initial-scale = 1.0" />
<script type="text/javascript" src="getscreen.js"></script>
<!--This script should appear below your LINK stylesheet tags -->
<script src="styleswitch.js" type="text/javascript">
/***********************************************
* Style Sheet Switcher v1.1- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for this script and 100s more
***********************************************/
</script>
</head>
<body>
<div id="container"><div id="banner"></div><div id="wrapper"><div id="main">
<script type="text/javascript">
if (pageWidth() < 400) {
document.writeln('<link rel="stylesheet" type="text/css" href="mobilestyle.css" /> <h3>You are browsing Physicsguy\'s Reviews in mobile mode. All of the features are here, nothing has changed, other than the size and functionality of this site! <br /><br /> ');
}
</script>
<script type="text/javascript">
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
if (document.cookie.indexOf("iphone_redirect=false") == -1)
document.writeln('<link rel="stylesheet" type="text/css" href="mobilestyle.css" />');
}</script>
<!--Begin main content-->
<div style="background: #800000; border: 2px solid #000000; padding: 5px; margin-bottom: 5px;">
<h1>Review Preview</h1>
<p><em>This is what your review will look like. If you like the way it looks, click submit, and if you want to make any changes, click Edit.</em></p>
</div>
<?php
$trans = array(
"<" => "<", ">" => ">",
"<" => "<", ">" => ">",
"&" => "&", "‘" => "‘ ",
"'" => "'", "\n" => "<br /> "
);
echo ("<div id='contentboxtitle'><h1>");
echo strtr("$reviewTitle", $trans);
echo ("</h1></div>");
echo ("<div id='contentbox'><em>A review by ");
echo strtr("$reviewAuthor", $trans);
echo ("</em><br /><br />");
echo strtr("$reviewContent", $trans);
echo ("</div><div id='contentboxtitle'><h3>Rating: ");
echo strtr("$reviewRating", $trans);
echo "</h3></div><div id='contentbox'>";
echo strtr("$ratingReason", $trans);
echo "</div>";
?>
<div style="background: #800000; border: 2px solid #000000; padding: 5px; margin-top: 5px;">
<?php
echo "
<form onsubmit='addReview()' />
<input type='submit' value='Add Review' />
</form>
";
?>
</div>
<!--End main content-->
</div></div><div id="extension">
<!--Begin right side content-->
<?php include("form.html"); ?>
<?php include("stylebuttons.htm"); ?>
<h2>Other Links</h12>
<hr>
<h3><a href="reviews.shtml">Go Back</a></h3>
<hr>
<a href="index.html">Back To Welcome</a>
<?php include("rightsidemenu.htm"); ?>
<hr />
<a href="setup.shtml">Settings</a>
<!-- End right side content -->
</div><div id="navigation"><div id="navmenu"><ul>
<!-- Begin navigation links...add as necessary -->
<?php include("leftsidemenu.htm"); ?>
<!-- End navigation links -->
</ul></div></div>
<div id="footer"><p class="footspace">© 2009 Physicsguy | <a href="setup.shtml">Settings</a></p></div></div>
</body>
</html>
There, that's everything.
Last edited by Physicsguy; 09-11-2009 at 05:35 PM..
|
|
|
|
09-11-2009, 06:12 PM
|
Re: Why doesn't this code work?
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
and the error is ?
Edit 1:
On a further note, can you explain why you
1) open the file cursor
2) declare the function
3) close the file cursor
BEFORE YOU CALL THE FUNCTION !?
rewrite this, and put the cursor opening, write and close into the same function !
Edit 2:
Ok, I'm really tired...
But I don't see where you call the addReview function. Are you really sure that you call it from that file, and that the function is being called ?
__________________
Only a biker knows why a dog sticks his head out the window.
Last edited by tripy; 09-11-2009 at 06:17 PM..
|
|
|
|
09-11-2009, 06:48 PM
|
Re: Why doesn't this code work?
|
Posts: 824
Name: Scott
Location: Ontario
|
addReview() is in the top PHP, before the DOCTYPE and everything.
And I do open the file cursor {[the $fp = fopen("ffdatabase.txt","a");]} before the function, and end it {[the fclose($fp);]} after the function.
So you're saying that I do the fclose($fp); after the addReview button? I'll try that.
OK, tried that, doesn't work. Here's my code:
I've also added a badwords filter, so please just ignore the words I put :P
Code:
<?php
$reviewTitle = $_POST["reviewtitle"];
$reviewAuthor = $_POST["authorname"];
$reviewContent = $_POST["reviewcontent"];
$reviewRating = $_POST["reviewrating"];
$ratingReason = $_POST["ratingreason"];
$reviewFull = "<div id='contentboxtitle'><h1>". $reviewTitle . "</h1></div><div id='contentbox'><em>A review by " . $reviewAuthor . "</em><br /><br />" . $reviewContent . "</div><div id='contentboxtitle'><h3>Rating: " . $reviewRating . "</h3></div><div id='contentbox'>" . $ratingReason . "</div>";
$fp = fopen("ffdatabase.txt","a");
if(!$fp) {
echo 'Error: Cannot open file.';
exit;
}
function addReview($fp, $reviewFull) {
fwrite($fp, $reviewFull."\n");
}
?>
<?php
function wordsExist(&$string, $words) {
foreach($words as &$word) {
if(stripos($string, $word) !== false) {
return true;
}
}
return false;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Physicsguy's Reviews - <?php echo ("$reviewTitle"); ?> - User Review</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="Scott Kaye" />
<meta name="description" content="Physicsguy's Reviews - Find tutorials, games, reviews, and even guitar tablature on Physicsguy's Reviews, your site for everything!" />
<meta name="keywords" content="Reviews, Games, Software, Tablature, Guitar, Hero, Rock, Band, Physicsguy, Physicsguy's, Reviews, Physics, Phun, Wii, PS3, Playstation, Nintendo, Linux, Ubuntu, Xmoto, Inkscape, HTML, CSS, Super, Mario, Bros, Theme, Legend, Of, Zelda, Scott, Kaye, Scott Kaye, Cool, Site" />
<link rel="stylesheet" href="physicsguystyle2.css" type="text/css" media="screen" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="lavatheme" href="physicsguystyle.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="rocktheme" href="physicsguystyle3.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="darkmattertheme" href="physicsguystyle4.css" />
<link rel="shortcut icon" href="http://www.webmaster-talk.com/images/favicon.ico" type="image/x-icon" />
<link rel="icon" href="http://www.webmaster-talk.com/images/favicon.ico" type="image/x-icons" />
<meta name="viewport" content="initial-scale = 1.0" />
<script type="text/javascript" src="getscreen.js"></script>
<!--This script should appear below your LINK stylesheet tags -->
<script src="styleswitch.js" type="text/javascript">
/***********************************************
* Style Sheet Switcher v1.1- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for this script and 100s more
***********************************************/
</script>
</head>
<body>
<div id="container"><div id="banner"></div><div id="wrapper"><div id="main">
<script type="text/javascript">
if (pageWidth() < 400) {
document.writeln('<link rel="stylesheet" type="text/css" href="mobilestyle.css" /> <h3>You are browsing Physicsguy\'s Reviews in mobile mode. All of the features are here, nothing has changed, other than the size and functionality of this site! <br /><br /> ');
}
</script>
<script type="text/javascript">
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
if (document.cookie.indexOf("iphone_redirect=false") == -1)
document.writeln('<link rel="stylesheet" type="text/css" href="mobilestyle.css" />');
}</script>
<!--Begin main content-->
<div style="background: #800000; border: 2px solid #000000; padding: 5px; margin-bottom: 5px;">
<h1>Review Preview</h1>
<p><em>This is what your review will look like. If you like the way it looks, click submit, and if you want to make any changes, click Edit.</em></p>
</div>
<?php
$trans = array(
"<" => "<", ">" => ">",
"<" => "<", ">" => ">",
"&" => "&", "‘" => "‘ ",
"'" => "'", "\n" => "<br /> "
);
echo ("<div id='contentboxtitle'><h1>");
echo strtr("$reviewTitle", $trans);
echo ("</h1></div>");
echo ("<div id='contentbox'><em>A review by ");
echo strtr("$reviewAuthor", $trans);
echo ("</em><br /><br />");
echo strtr("$reviewContent", $trans);
echo ("</div><div id='contentboxtitle'><h3>Rating: ");
echo strtr("$reviewRating", $trans);
echo "</h3></div><div id='contentbox'>";
echo strtr("$ratingReason", $trans);
echo "</div>";
?>
<div style="background: #800000; border: 2px solid #000000; padding: 5px; margin-top: 5px;">
<?php
$string = $reviewFull;
if (wordsExist($string, array(" **** "," **** "," *** "," **** "," dick "," ***** "," vagina "," piss "," sex ")))
{
echo "
Your review contains censored words. Until you remove these words, your review will not be processed.
<input type='button' value='Edit' onClick='history.back()' />
";
}
else
{
echo "
<table style='border: 1px solid #000000'><tr>
<form onsubmit='addReview()' />
<input type='submit' value='Add Review' />
</form>
<input type='button' value='Edit' onClick='history.back()' />
</tr></table>
";
}
fclose($fp);
?>
</div>
<!--End main content-->
</div></div><div id="extension">
<!--Begin right side content-->
<?php include("form.html"); ?>
<?php include("stylebuttons.htm"); ?>
<h2>Other Links</h12>
<hr>
<h3><a href="reviews.shtml">Go Back</a></h3>
<hr>
<a href="index.html">Back To Welcome</a>
<?php include("rightsidemenu.htm"); ?>
<hr />
<a href="setup.shtml">Settings</a>
<!-- End right side content -->
</div><div id="navigation"><div id="navmenu"><ul>
<!-- Begin navigation links...add as necessary -->
<?php include("leftsidemenu.htm"); ?>
<!-- End navigation links -->
</ul></div></div>
<div id="footer"><p class="footspace">© 2009 Physicsguy | <a href="setup.shtml">Settings</a></p></div></div>
</body>
</html>
Last edited by Physicsguy; 09-11-2009 at 06:50 PM..
|
|
|
|
09-11-2009, 07:35 PM
|
Re: Why doesn't this code work?
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
No, you didn't understood me...
What I see here, is that you define a function, but you never use it...
If I attempt an anlogy with an office:
1) you open the file cursor with fopen() ==> You turn on a typewriter
2) You define the function addReview() ==> You shout in your office "The typewriter is here, if anyone needs it"
3) You close the file pointer by calling fclose() ==> You power down the typewriter
The point is, that if anyone needs to use addReview(), he won't be able to, because the file cursor have already been closed.
Declaring a function is just making it available.
You need to call that function to make what is defined into the function happen.
In you case, if you never do
PHP Code:
addReview($filePointer, "this is my full review");
or something similar, nothing will ever by wrote.
And what I meant, is that the file cursor opening and closing should be done in the function too.
PHP Code:
function addReview($reviewFull) { $fp = fopen("ffdatabase.txt","a"); if(!$fp) { error_log("Could not open the database file") return FALSE; } fwrite($fp, $reviewFull."\n"); fclose($fp); return TRUE; }
I changed the exit() condition (which should ot be used in a function) to a message saved in the log file, and a returned value.
You can catch the returned value with this syntax:
PHP Code:
$return=addReview("this is my review");
if the addReview() function was able to save the review in the file, the value of $return will be TRUE.
If there was an error opening the file, the value will be FALSE.
This let you enclose this in a check:
PHP Code:
$review=$_GET['reviewText']; $ret=addReview($review); if($ret===TRUE){ echo "<b>Thank you</b>"; } else{ echo "there was an error processing your review"; mail("me@mydomain.com","Error saving a review","could not open the db file for writing"); }
Or you could even move the mail error part into the function, and make it send you an email every time the db file could not be opened.
Take some time to read this chapter: http://www.php.net/manual/en/language.functions.php
It will help you to understand how PHP works.
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
09-12-2009, 03:23 AM
|
Re: Why doesn't this code work?
|
Posts: 744
Name: Mattias Nordahl
Location: Sweden
|
I didn't have time to read all of the awnsers in this tread already, but it seems to me you have misunderstood the whole idea behind php Physicsguy. I see you're trying to call the function addReview() from an onclick attribute on your button. php is run on the server, not in the browser like JavaScript. So if you open your page and check the source code you'll notice that the addReview() function isn't there, because it's php code that have already been parsed and executed at the server, and then only the html part is sent to the browser.
For the button to work, you'll have to actually send the form, thereby loading the page again (or a new page, depending on your value in the action attribute), then handle the file opening and writing in php code, and close the file again. This can't be done when the page has already loaded and is being displayed by the browser, php code can only be run at the server.
__________________
34343639363436653237373432303635373837303635363337 34323037343638363137343263323036343639363432303739 366637353366
|
|
|
|
09-12-2009, 02:09 PM
|
Re: Why doesn't this code work?
|
Posts: 824
Name: Scott
Location: Ontario
|
Quote:
Originally Posted by tripy
PHP Code:
function addReview($reviewFull) { $fp = fopen("ffdatabase.txt","a"); if(!$fp) { error_log("Could not open the database file") return FALSE; } fwrite($fp, $reviewFull."\n"); fclose($fp); return TRUE; }
|
Thanks, but where do I put this? I have no idea... I replaced the
PHP Code:
function addReview($fp, $reviewFull) { fwrite($fp, $reviewFull."\n"); }
fclose($fp);
with what you wrote, and it just gave me an error.
Also, lizciz, I do understand PHP, I just have trouble making it work properly. Anything (well, almost) is possible with PHP, some things are just harder to code than others. I've only been coding for about 3/4 of a year now, I started in Febuary, and only started learning PHP in July. I'm still a n00b, so I need a lot of help, sorry  .
Here's the entire source of my upgraded process.php, I've changed a lot of it (further down), and I'm pretty darn happy with it, but I'd like it to be able to save, and then give it a serial number, thenadd it to the database, and then have a script that can pull that serial key out of the database. I can figure that out on my own (I hope :P), and here's rocess.php:
Code:
<?php
$reviewTitle = $_POST["reviewtitle"];
$reviewAuthor = $_POST["authorname"];
$reviewContent = $_POST["reviewcontent"];
$reviewRating = $_POST["reviewrating"];
$ratingReason = $_POST["ratingreason"];
$reviewFull = "<div id='contentboxtitle'><h1>". $reviewTitle . "</h1></div><div id='contentbox'><em>A review by " . $reviewAuthor . "</em><br /><br />" . $reviewContent . "</div><div id='contentboxtitle'><h3>Rating: " . $reviewRating . "</h3></div><div id='contentbox'>" . $ratingReason . "</div>";
$fp = fopen("ffdatabase.txt","a");
if(!$fp) {
echo 'Error: Cannot open file.';
exit;
}
function addReview($fp, $reviewFull) {
fwrite($fp, $reviewFull."\n");
}
fclose($fp);
function BadWordsExist(&$string, $words) {
foreach($words as &$word) {
if(stripos($string, $word) !== false) {
return true;
}
}
return false;
}
function AuthorisAdmin(&$Admins, $AdminNames) {
foreach($AdminNames as &$AdminName) {
if(stripos($Admins, $AdminName) !== false) {
return true;
}
}
return false;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Physicsguy's Reviews - <?php echo ("$reviewTitle"); ?> - User Review</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="Scott Kaye" />
<meta name="description" content="Physicsguy's Reviews - Find tutorials, games, reviews, and even guitar tablature on Physicsguy's Reviews, your site for everything!" />
<meta name="keywords" content="Reviews, Games, Software, Tablature, Guitar, Hero, Rock, Band, Physicsguy, Physicsguy's, Reviews, Physics, Phun, Wii, PS3, Playstation, Nintendo, Linux, Ubuntu, Xmoto, Inkscape, HTML, CSS, Super, Mario, Bros, Theme, Legend, Of, Zelda, Scott, Kaye, Scott Kaye, Cool, Site" />
<link rel="stylesheet" href="physicsguystyle2.css" type="text/css" media="screen" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="lavatheme" href="physicsguystyle.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="rocktheme" href="physicsguystyle3.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="darkmattertheme" href="physicsguystyle4.css" />
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
<link rel="icon" href="images/favicon.ico" type="image/x-icons" />
<meta name="viewport" content="initial-scale = 1.0" />
<script type="text/javascript" src="getscreen.js"></script>
<!--This script should appear below your LINK stylesheet tags -->
<script src="styleswitch.js" type="text/javascript">
/***********************************************
* Style Sheet Switcher v1.1- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for this script and 100s more
***********************************************/
</script>
</head>
<body>
<div id="container"><div id="banner"></div><div id="wrapper"><div id="main">
<script type="text/javascript">
if (pageWidth() < 400) {
document.writeln('<link rel="stylesheet" type="text/css" href="mobilestyle.css" /> <h3>You are browsing Physicsguy\'s Reviews in mobile mode. All of the features are here, nothing has changed, other than the size and functionality of this site! <br /><br /> ');
}
</script>
<script type="text/javascript">
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
if (document.cookie.indexOf("iphone_redirect=false") == -1)
document.writeln('<link rel="stylesheet" type="text/css" href="mobilestyle.css" />');
}</script>
<!--Begin main content-->
<div style="background: #800000; border: 2px solid #000000; padding: 5px; margin-bottom: 5px;">
<h1>Review Preview</h1>
<p><em>This is what your review will look like if you add it to PGReviews. If you like the way it looks, click submit, and if you want to make any changes, click Edit.</em></p>
<p>Please keep in mind that if you try to post as any admin, you will receive an alert telling you to change your name in order to submit your review. Also, if you try to post any swearwords, they will be caught and you must remove them in order to submit your review.</p>
</div>
<?php
$trans = array(
"<" => "<", ">" => ">",
"<" => "<", ">" => ">",
"&" => "&", "‘" => "‘ ",
"'" => "'", "\n" => "<br /> ",
);
echo ("<div id='contentboxtitle'><h1>");
echo strtr("$reviewTitle", $trans);
echo ("</h1></div>");
echo ("<div id='contentbox'><em>A review by ");
echo strtr("$reviewAuthor", $trans);
echo ("</em><br /><br />");
echo strtr("$reviewContent", $trans);
echo ("</div><div id='contentboxtitle'><h3>Rating: ");
echo strtr("$reviewRating", $trans);
echo "</h3></div><div id='contentbox'>";
echo strtr("$ratingReason", $trans);
echo "</div>";
?>
<div style="background: #800000; border: 2px solid #000000; padding: 5px; margin-top: 5px;">
<?php
$string = $reviewFull;
if (BadWordsExist($string, array("****","****","***","****","dick","*****","vagina","piss","sex")))
{
echo "
<p>Your review contains censored words. Until you remove these words, your review will not be processed.</p>
";$BadWordsError = 1;
}
else
{
echo "";
}
$Admins = $reviewFull;
if (AuthorisAdmin($Admins, array("Physicsguy","PGReviews","PG","PGR","Physicsguy's Reviews")))
{
echo "
<p>The name $reviewAuthor is an Admin. You cannot submit reviews as an Admin. Please change the author of your review.</p>
";$AdminError = 1;
}
else
{
echo "";
}
$ErrorCount = $AdminError + $BadWordsError;
if ($ErrorCount <= 0)
{
echo "
<p>Congragulations, your review validates! Click 'Add Review' now to submit the review to PGReviews, or click 'Edit' to change it!</p>
<table style='border: 0px solid #000000'><tr><p><b>Congragulations, your review validates! Click 'Add Review' to add your review to PGReviews, or 'Edit' if you do not like the way it turned out.</b></p>
<form onsubmit='addReview()' />
<input type='submit' value='Add Review' />
</form>
<input type='button' value='Edit' onClick='history.back()' />
</tr></table>
";
}
else if ($ErrorCount <= 1)
{
echo "<p><input type='button' value='Edit' onClick='history.back()' /> Found 1 error.</p>";
}
else if ($ErrorCount <= 2)
{
echo "<p><input type='button' value='Edit' onClick='history.back()' /> Found 2 errors.</p>";
}
?>
</div>
<!--End main content-->
</div></div><div id="extension">
<!--Begin right side content-->
<?php include("form.html"); ?>
<?php include("stylebuttons.htm"); ?>
<h2>Other Links</h12>
<hr>
<h3><a href="reviews.shtml">Go Back</a></h3>
<hr>
<a href="index.html">Back To Welcome</a>
<?php include("rightsidemenu.htm"); ?>
<hr />
<a href="setup.shtml">Settings</a>
<!-- End right side content -->
</div><div id="navigation"><div id="navmenu"><ul>
<!-- Begin navigation links...add as necessary -->
<?php include("leftsidemenu.htm"); ?>
<!-- End navigation links -->
</ul></div></div>
<div id="footer"><p class="footspace">© 2009 Physicsguy | <a href="setup.shtml">Settings</a></p></div></div>
</body>
</html>
Thanks!
|
|
|
|
09-12-2009, 02:57 PM
|
Re: Why doesn't this code work?
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
Ok, before going further, as Lizciz stated, I've found that you call addReview() on a form onsubmit handler.
Do you have a javascript function named addReview() that we don't see here ?
Because, what Mattias was saying, is that you cannot call a PHP function directly from html like this.
The form onsubmit is a call to a javascript function.
Javascript is a client( or browser) side language. What is done in javascript is done on the computer of the peoples accessing your site.
PHP is a server side language. It's executed on the server, when the web page is being built by the server, before it's sent to the browser.
The only way you can call a PHP function via javascript, is to use an AJAX request that is fired from the click, makes a request on the server, and relay back what the server did to the browser, updating a part of the page displayed in that browser.
Or, when you do the submit of the form, the page referenced by the "action=" property of the form must know that it must call the addReview() PHP function.
But this information is to be written in the PHP logic, not in the form declaration.
Like this:
PHP Code:
<?php function addReview($reviewFull) { $fp = fopen("ffdatabase.txt","a"); if(!$fp) { error_log("Could not open the database file") return FALSE; } fwrite($fp, $reviewFull."\n"); fclose($fp); return TRUE; }
$reviewSavedOk=FALSE; if(sizeof($_POST)>1){ //POST received ret=addReview($_POST['review']); if($ret===TRUE){ $msg='Your review was accepted. Thank you'; $reviewSavedOk=TRUE; } else{ $msg="There was an error recording your submission"; } } else{ $msg="Congragulations, your review validates! Click 'Add Review' now to submit the review to PGReviews, or click 'Edit' to change it!"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>Physicsguy's Reviews - <?php echo $reviewTitle; ?> - User Review</title> </head> <body> <p><?php echo $msg;?></p> <?php if($reviewSavedOk==FALSE){?> <table style='border: 0px solid #000000'> <tr> <td> <p><b>Congragulations, your review validates! Click 'Add Review' to add your review to PGReviews, or 'Edit' if you do not like the way it turned out.</b></p> <form action="<?php echo $_SERVER['PHP_SELF']?>" method='POST'> <input type='submit' value='Add Review' /> <textarea name="review">This is my super duper review!</textarea> <input type='button' value='Edit' onClick='history.back()' /> </form> </td> </tr> </table> <?php }?> </body>
</html>
__________________
Only a biker knows why a dog sticks his head out the window.
Last edited by tripy; 09-12-2009 at 02:58 PM..
|
|
|
|
09-12-2009, 05:29 PM
|
Re: Why doesn't this code work?
|
Posts: 824
Name: Scott
Location: Ontario
|
OK, thanks, but when I use that, I get this:
Parse error: syntax error, unexpected T_RETURN in /home/scott/web/process.php on line 14
And on line 14 there is the return FALSE statement.
OK... After some minor tweaking, I get it to show up. The only problem is is that as soon as the page loads, it adds it to the database.
The tweaking I did was I put a semicolon after the error message that says "Could not open the database file" after the ). The one 'ret' didn't have a $ in front of it, so I put one there.
Now instead of clicking the Add Review button to add the review, it just adds it when process.php loads up, and when I do press Add Review, it deletes all the form input, leaving me with a blank page...
Here's yet again the entire process.php...
Code:
<?php
$reviewTitle = $_POST["reviewtitle"];
$reviewAuthor = $_POST["authorname"];
$reviewContent = $_POST["reviewcontent"];
$reviewRating = $_POST["reviewrating"];
$ratingReason = $_POST["ratingreason"];
$reviewFull = "<div id='contentboxtitle'><h1>". $reviewTitle . "</h1></div><div id='contentbox'><em>A review by " . $reviewAuthor . "</em><br /><br />" . $reviewContent . "</div><div id='contentboxtitle'><h3>Rating: " . $reviewRating . "</h3></div><div id='contentbox'>" . $ratingReason . "</div>";
function addReview($reviewFull) {
$fp = fopen("ffdatabase.txt","a");
if(!$fp) {
error_log("Could not open the database file");
return FALSE;
}
fwrite($fp, $reviewFull."\n");
fclose($fp);
return TRUE;
}
$reviewSavedOk=FALSE;
if(sizeof($_POST)>1){
//POST received
$ret=addReview($reviewFull);
if($ret===TRUE){
$msg='Your review was accepted. Thank you';
$reviewSavedOk=TRUE;
}
else{
$msg="There was an error recording your submission";
}
}
else{
$msg="Congragulations, your review validates! Click 'Add Review' now to submit the review to PGReviews, or click 'Edit' to change it!";
}
function BadWordsExist(&$string, $words) {
foreach($words as &$word) {
if(stripos($string, $word) !== false) {
return true;
}
}
return false;
}
function AuthorisAdmin(&$Admins, $AdminNames) {
foreach($AdminNames as &$AdminName) {
if(stripos($Admins, $AdminName) !== false) {
return true;
}
}
return false;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Physicsguy's Reviews - <?php echo ("$reviewTitle"); ?> - User Review</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="author" content="Scott Kaye" />
<meta name="description" content="Physicsguy's Reviews - Find tutorials, games, reviews, and even guitar tablature on Physicsguy's Reviews, your site for everything!" />
<meta name="keywords" content="Reviews, Games, Software, Tablature, Guitar, Hero, Rock, Band, Physicsguy, Physicsguy's, Reviews, Physics, Phun, Wii, PS3, Playstation, Nintendo, Linux, Ubuntu, Xmoto, Inkscape, HTML, CSS, Super, Mario, Bros, Theme, Legend, Of, Zelda, Scott, Kaye, Scott Kaye, Cool, Site" />
<link rel="stylesheet" href="physicsguystyle2.css" type="text/css" media="screen" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="lavatheme" href="physicsguystyle.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="rocktheme" href="physicsguystyle3.css" />
<link rel="alternate stylesheet" type="text/css" media="screen" title="darkmattertheme" href="physicsguystyle4.css" />
<link rel="shortcut icon" href="http://www.webmaster-talk.com/images/favicon.ico" type="image/x-icon" />
<link rel="icon" href="http://www.webmaster-talk.com/images/favicon.ico" type="image/x-icons" />
<meta name="viewport" content="initial-scale = 1.0" />
<script type="text/javascript" src="getscreen.js"></script>
<!--This script should appear below your LINK stylesheet tags -->
<script src="styleswitch.js" type="text/javascript">
/***********************************************
* Style Sheet Switcher v1.1- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for this script and 100s more
***********************************************/
</script>
</head>
<body>
<div id="container"><div id="banner"></div><div id="wrapper"><div id="main">
<script type="text/javascript">
if (pageWidth() < 400) {
document.writeln('<link rel="stylesheet" type="text/css" href="mobilestyle.css" /> <h3>You are browsing Physicsguy\'s Reviews in mobile mode. All of the features are here, nothing has changed, other than the size and functionality of this site! <br /><br /> ');
}
</script>
<script type="text/javascript">
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
if (document.cookie.indexOf("iphone_redirect=false") == -1)
document.writeln('<link rel="stylesheet" type="text/css" href="mobilestyle.css" />');
}</script>
<!--Begin main content-->
<div style="background: #800000; border: 2px solid #000000; padding: 5px; margin-bottom: 5px;">
<h1>Review Preview</h1>
<p><em>This is what your review will look like if you add it to PGReviews. If you like the way it looks, click submit, and if you want to make any changes, click Edit.</em></p>
<p>Please keep in mind that if you try to post as any admin, you will receive an alert telling you to change your name in order to submit your review. Also, if you try to post any swearwords, they will be caught and you must remove them in order to submit your review.</p>
</div>
<?php echo $msg;?></p>
<?php
if($reviewSavedOk==TRUE){?>
<table style='border: 0px solid #000000'>
<tr>
<td>
<p>WHAT JUST HAPPENED</p>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method='POST'>
<input type='submit' value='Add Review' />
<input type='button' value='Edit' onClick='history.back()' />
</form>
</td>
</tr>
</table>
<?php
}?>
<?php
$trans = array(
"<" => "<", ">" => ">",
"<" => "<", ">" => ">",
"&" => "&", "‘" => "‘ ",
"'" => "'", "\n" => "<br /> ",
);
echo ("<div id='contentboxtitle'><h1>");
echo strtr("$reviewTitle", $trans);
echo ("</h1></div>");
echo ("<div id='contentbox'><em>A review by ");
echo strtr("$reviewAuthor", $trans);
echo ("</em><br /><br />");
echo strtr("$reviewContent", $trans);
echo ("</div><div id='contentboxtitle'><h3>Rating: ");
echo strtr("$reviewRating", $trans);
echo "</h3></div><div id='contentbox'>";
echo strtr("$ratingReason", $trans);
echo "</div>";
?>
<div style="background: #800000; border: 2px solid #000000; padding: 5px; margin-top: 5px;">
<?php
$string = $reviewFull;
if (BadWordsExist($string, array("****","****","***","****","dick","*****","vagina","piss","sex")))
{
echo "
<p>Your review contains censored words. Until you remove these words, your review will not be processed.</p>
";$BadWordsError = 1;
}
else
{
echo "";
}
$Admins = $reviewFull;
if (AuthorisAdmin($Admins, array("Physicsguy","PGReviews","PG","PGR","Physicsguy's Reviews")))
{
echo "
<p>The name $reviewAuthor is an Admin. You cannot submit reviews as an Admin. Please change the author of your review.</p>
";$AdminError = 1;
}
else
{
echo "";
}
$ErrorCount = $AdminError + $BadWordsError;
if ($ErrorCount <= 0)
{
echo "
<table style='border: 0px solid #000000'><tr><p><b>Congragulations, your review validates! Click 'Add Review' to add your review to PGReviews, or 'Edit' if you do not like the way it turned out.</b></p>
<form onsubmit='addReview()' />
<input type='submit' value='Add Review' />
</form>
<input type='button' value='Edit' onClick='history.back()' />
</tr></table>
";
}
else if ($ErrorCount <= 1)
{
echo "<p><input type='button' value='Edit' onClick='history.back()' /> Found 1 error.</p>";
}
else if ($ErrorCount <= 2)
{
echo "<p><input type='button' value='Edit' onClick='history.back()' /> Found 2 errors.</p>";
}
?>
</div>
<!--End main content-->
</div></div><div id="extension">
<!--Begin right side content-->
<?php include("form.html"); ?>
<?php include("stylebuttons.htm"); ?>
<h2>Other Links</h12>
<hr>
<h3><a href="reviews.shtml">Go Back</a></h3>
<hr>
<a href="index.html">Back To Welcome</a>
<?php include("rightsidemenu.htm"); ?>
<hr />
<a href="setup.shtml">Settings</a>
<!-- End right side content -->
</div><div id="navigation"><div id="navmenu"><ul>
<!-- Begin navigation links...add as necessary -->
<?php include("leftsidemenu.htm"); ?>
<!-- End navigation links -->
</ul></div></div>
<div id="footer"><p class="footspace">© 2009 Physicsguy | <a href="setup.shtml">Settings</a></p></div></div>
</body>
</html>
Try it. It doesn't work properly...
What happens is when process.php first loads up, it gives you "Your review was accepted. Thank you". It also adds the review to the database. Then underneath that it says "WHAT JUST HAPPENED", with the Edit and Add Review buttons. Then below that... Ah, here's a screenshot... Click them for bigger copy.
And here it is after I click one of the Add Review buttons.
???
Could I simply put my old
Code:
function addReview($fp, $reviewFull) {
fwrite($fp, $reviewFull."\n");
}
into a JavaScript? So, like:
Code:
<script type="text/javascript">
function addReview($fp, $reviewFull) {
fwrite($fp, $reviewFull."\n");
}
</script>
or something? Then in the HTML:
Code:
<form action="addReview()" >
<input type="submit" value="Add Review" />
</form>
?
But then the PHP doesn't work. Could I:
Code:
<?php echo"
<script type='text/javascript'>
function addReview($fp, $reviewFull) {
fwrite($fp, $reviewFull.'\n');
}
</script>
<form action='addReview()' >
<input type='submit' value='Add Review' />
</form>";
?>
No, but that doesn't work either... AUGH!
I tried moving the exit right after the fclose but that just gave me a blank page, and I tried deleting the exit, but it doesn't make a difference...
Last edited by Physicsguy; 09-12-2009 at 06:12 PM..
|
|
|
|
|
« Reply to Why doesn't this code work?
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|