|
-
Apr 17th, 2007, 03:26 PM
#1
Thread Starter
Frenzied Member
How to show only needed html in one php page?
I am trying to make a guestbook, with different html views for the view entries, and post a new comment both on the same guest.php page, so i can take the query string and decide what the user wants to do, but if i both html for posting and viewing in the same php page but not to display both at once in the output whats the simplest way to skip one set of html ?
-
Apr 17th, 2007, 05:22 PM
#2
Re: How to show only needed html in one php page?
you could use if statements, or maybe even a switch statement.
PHP Code:
<?php
//has the user posted to this form? if so, we need to add their comment
if($_SERVER['REQUEST_METHOD'] == "POST"){
//they are posting; so we add a record to the database
//NOTE: you should do some error checking here! I left it out.
$safe_post = array();
//escape characters in all POST variables
foreach($_POST as $key => $value){
$safe_post[$key] = mysql_real_escape_string($value);
}
mysql_query("INSERT INTO tablename (field1, field2, field3) VALUES('{$safe_post['value1']}', '{$safe_post['value2']}', {$safe_post['value3']}');");
?>
successfully posted a comment!<br /><br />
please <a href="/guestbook.php">click here</a> to view your comment.
<?php
}else{
//user didn't submit anything, so we know we need to either show the comment form or view comments
if(isset($_GET['posting'])){
//we need to show the comment form, the user wants to post a comment
?>
<a href="/guestbook.php">click here</a> to go back to read comments
<br /><br />
<form action="/guestbook.php" method="post">
value1: <input type="text" name="value1" /><br />
value2: <input type="text" name="value2" /><br />
value3: <input type="text" name="value3" /><br />
<input type="submit" value="submit" />
</form>
<?php
}else{
//user is not posting, so let's just view the comments
$sql = "SELECT id, field1, field2, field3 FROM tablename";
$q = mysql_query($sql);
if(mysql_num_rows($q) > 0){
//there have been comments made, we just need to display them
while($comment = mysql_fetch_array($q)){
?>
<strong>comment #<?php echo $comment['id'] + 1; ?>:</strong><br />
<strong>field1:</strong> <?php echo $comment['field1']; ?><br />
<strong>field2:</strong> <?php echo $comment['field2']; ?><br />
<strong>field3:</strong> <?php echo $comment['field3']; ?><br />
<hr />
<?php
}
}else{
//no comments found in the database
echo "sorry, there haven't been any comments yet!";
}
//now, we always display a link (when viewing comments) to be able to post a comment
?>
<br /><br />
would you like to post a comment? <a href="/guestbook.php?posting">click here</a>
<?php
}
}
?>
completely untested (although with a database set up, I'm sure it would run), and maybe a little sloppy? but, it will at least give you an example of what you might want to do. basically, if a user requests "guestbook.php?posting", they will be posting something. if they're just requesting "guestbook.php", then we know they are just viewing the comments. however, if they're viewing "guestbook.php" and have posted to the form, we know they are making a comment, so we add to the database and then display a link to go back to viewing the comments.
hope that helps. I was in a rush.
-
Apr 18th, 2007, 02:40 AM
#3
Thread Starter
Frenzied Member
Re: How to show only needed html in one php page?
If using this,
PHP Code:
if($_SERVER['REQUEST_METHOD'] == "POST"){
and you were to refresh the page you get that "do you want to repost the variables you just sent etc...."
In that case if you did refresh the page wouldnt a duplicate entry get posted into the guestbook?
-
Apr 18th, 2007, 02:52 AM
#4
Re: How to show only needed html in one php page?
Yeah, you do. When you make any non-idempotent request (POST, PUT, DELETE) it should be made to a script that processes the request data then silently redirects to the destination page. You should use the HTTP 302 (See Other) code for this as that indicates to the client that the POST request should not be cached. This means you can then go back/forward in the browser without worrying about submitting data again or getting that annoying popup message.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|