Submitting text area data problem. AArrrrgh

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sugarat
Forum Newbie
Posts: 1
Joined: Thu May 27, 2004 8:40 pm

Submitting text area data problem. AArrrrgh

Post by Sugarat »

Okay.. this is driving me mad.

I have a webpage that basically is designed to allow people to enter HTML text into a textarea box, and when they click the preview button, it previews the html they entered into the box.

If they are happy with the preview they can then hit submit again and have the html data entered into the database.

The problem I have is that for some reason, when its submitted, PHP is printing all the data TWICE, although nowhere is it told to do it.. Here is the code:

Code: Select all

<!-- Render page preview -->
                               <?php
							     if($_POST&#1111;submit])
								 &#123;
								 	echo "<img src="$_POST&#1111;title_image]"><center><br><em>$_POST&#1111;author] : $_POST&#1111;date]</em></center><br><br>";
									$article_fixed = str_replace("&quote;, "", $_POST&#1111;article_text]);
									echo "$_POST&#1111;teaser]<br><br>";
									echo $article_fixed;
									echo "<form method="post" action="insert_article.php">";
									echo "<input type="hidden" name="date" value="$_POST&#1111;date]">";
									echo "<input type="hidden" name="author" value="$_POST&#1111;author]">";
									echo "<input type="hidden" name="title_image" value="$_POST&#1111;title_image]">";
									echo "<input type="hidden" name="title" value="$_POST&#1111;title]">";
									echo "<input type="hidden" name="teaser" value="$_POST&#1111;teaser]">";
									printf("<input type="hidden" name="article" value="%s"", $article_fixed);
									
									echo "<br><br><input type="submit" value="Submit">";
									echo "</form>";
								 &#125;
								 else  //Display forms
								 &#123; ?>
								 	<form method="POST" action="insert.php" enctype="text/plain">
										<bold>Date :</bold><br><br><input type="text" name="date"><br><br>
										<bold>Author :</bold><br><br><input type="text" name="author"><br><br>
										<bold>Title Image :</bold><br><br><input type="text" name="title_image"><br><br>
										<bold>Title :</bold><br><br><input type="text" name="title"><br><br>
										<bold>Teaser :</bold><br><br><textarea name="teaser" cols="60" rows="15"></textarea>
										<bold>Article :</bold><br><br><textarea name="article_text" cols="60" rows="30"></textarea>
										<input type="submit" value="Preview" name="submit">
									</form>
								 <?php
								 &#125; ?>
Any ideas why this code displays the data TWICE?
Chonk
Forum Newbie
Posts: 24
Joined: Fri May 28, 2004 3:58 am

Post by Chonk »

Code: Select all

<?php
if($_POST[submit]) 
?>
$_POST['submit'] is'nt being checked properly.

Code: Select all

<?php
isset(if($_POST['submit]') );
?>
Should fix this problem. Also i would add some other bounds checking for the other data on the same if statement(as shown below).

Code: Select all

<?php
if (isset($_POST['submit']) && isset($_POST['date]') && isset($_POST['author']) && isset($_POST['title_image']) && isset($_POST['title']) && isset($_POST['article_text']) && isset($_POST['title_image']) && isset($_POST['teaser']) );

?>
I could be wrong here, but it is my first post and im a newbie :roll:
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Unfortunately, I think ~chonk is wrong.

Code: Select all

if($_POST['submit']
should work fine.

Code: Select all

isset(if($_POST['submit']))
just plain won't work because

Code: Select all

if($_POST['submit'])
isn't a variable. Plus, if you call isset() on a particular element in an array, it'll just check if the array is set, not the element (But don't quote me on that part).

~sugarat - is that the entire page or just a snippet?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Plus, if you call isset() on a particular element in an array, it'll just check if the array is set, not the element (But don't quote me on that part).
(Sorry Pickle!!) I'm pretty sure that's wrong. I use that on $_POST and $_GET and $_SESSION vars a lot and it works on individual elements.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Use a switch

Post by RobertGonzalez »

Maybe you could use a switch statement around a "process" variable. What I have done in two step processes in the past is post the page to itself using a variable called "mode". For preview, mode is set to "preview", for posting mode is set to "post". Then in you PHP script you can direct the process based on the value of mode.

Example:

Code: Select all

<?php
if ( isset($mode) )
{
    switch(@$mode)
    {
        case "preview":
            //add in your preview data
            //also add the URL to your posting script appended 
            //with '?mode=post'
        break;

        case "post":
            //add in your posting SQL and script
        break;

        default:
            //maybe use this as your base page
        break;
    } //End Switch
}// End IF
?>
I use this technique when allowing folks to delete from a DB. This prevents immediate deleting if it was an accident, foolishness, etc. Anyway, I hope this helps.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

magicrobotmonkey wrote: (Sorry Pickle!!) I'm pretty sure that's wrong. I use that on $_POST and $_GET and $_SESSION vars a lot and it works on individual elements.
Well, you learn something every day. :D
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

also, it's

Code: Select all

if(isset($variable)){
not

Code: Select all

isset(if($variable)){
Chonk
Forum Newbie
Posts: 24
Joined: Fri May 28, 2004 3:58 am

Post by Chonk »

Lilpunkster: Your right, i didnt check the code before i posted :D
Post Reply