Page 1 of 1

Submitting text area data problem. AArrrrgh

Posted: Thu May 27, 2004 8:40 pm
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?

Posted: Fri May 28, 2004 3:58 am
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:

Posted: Fri May 28, 2004 9:51 am
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?

Posted: Fri May 28, 2004 9:57 am
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.

Use a switch

Posted: Fri May 28, 2004 10:02 am
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.

Posted: Fri May 28, 2004 10:30 am
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

Posted: Fri May 28, 2004 11:08 am
by d3ad1ysp0rk
also, it's

Code: Select all

if(isset($variable)){
not

Code: Select all

isset(if($variable)){

Posted: Sat May 29, 2004 6:43 am
by Chonk
Lilpunkster: Your right, i didnt check the code before i posted :D