Jump to content

php post/get from same page form


Recommended Posts

Trying to put a form and its results in the same php file. I know it can be done, just trying to figure out how to do it simple.For some reason it is giving error: Notice: Undefined index: infoBox in C:\Apache2\wwwroot\modem_rewrite\infobox.php on line 3. Which makes sense since it has no data yet... but I cannot seem to figure out how to get rid of the error. The script works fine otherwise, but no matter what I get the error about infoBox undefined.

<?php$infoBox = $_POST["infoBox"];if (!isset($_POST['submit'])) {	echo "<html>";	echo "<head>";	echo "<title>InfoBox Generator</title>";	echo "</head>";	echo "<body>";	echo "<form method=\"post\" action=\"" .$_SERVER['PHP_SELF']. "\">";	echo "Info Box:<input type=\"text\" name=\"infoBox\" /><br />";	echo "<div style=\"width:110px;margin-left:15%;\">";	echo "<input type=\"submit\" value=\"submit\" name=\"submit\" />";	echo "</div>";	echo "</form>";	echo "</body>";	echo "</html>";}else {	echo "<html>";	echo "<head>";	echo "<title>InfoBox Generator</title>";	echo "</head>";	echo "<body>";	echo "<textarea>";	echo "$infoBox";	echo "</textarea>";	echo "</body>";	echo "</html>";}?>

editted at 3:22

Link to post
Share on other sites

Nevermind, I figured it out... Sorry for the trouble. Here is the code if anyone has the same issue....

<?phpif (!isset($_POST['submit'])) {	echo "<html>";	echo "<head>";	echo "<title>InfoBox Generator</title>";	echo "</head>";	echo "<body>";	echo "<form method=\"post\" action=\"" .$_SERVER['PHP_SELF']. "\">";	echo "Info Box:<input type=\"text\" name=\"infoBox\" /><br />";	echo "<div style=\"width:110px;margin-left:15%;\">";	echo "<input type=\"submit\" value=\"submit\" name=\"submit\" />";	echo "</div>";	echo "</form>";	echo "</body>";	echo "</html>";}else {$infoBox = $_POST["infoBox"];	echo "<html>";	echo "<head>";	echo "<title>InfoBox Generator</title>";	echo "</head>";	echo "<body>";	echo "<textarea>";	echo "$infoBox";	echo "</textarea>";	echo "</body>";	echo "</html>";}?>

Link to post
Share on other sites

hmmm. I need to work on saving as html files now :)Any links you all have on the top of your head before I go scouring the internet for them.Take form data that is currently being output to a <textarea> and save it as an html file. Seems simple enough. Thanks for the link justsomeguy, it is bookmarked for reading right now. Will check this thread again after I've read more of it and see if anyone posted any links.Thanks in again

Link to post
Share on other sites

Dude, you're going to make yourself crazy with all those echo statements. If you need to interpolate a lot of variables in your markup, consider using heredoc syntax. OR close your PHP tags inside the curly braces of your if statements, which will make the HTML print only if the condition is true. You can then open little php sections to echo just the variables where you need them.

Link to post
Share on other sites

I was having trouble with doing that Deidre's Dad. But I also had a few other syntax errors, and I was just narrowing it down by using the echo's. (and yes, it was driving me crazy).I'll see if I can get it to work without them now. you should see my other "generator" file :) It's just a huge mess... works, but it sure is ugly.I'm so out of practice on php... never was a guru by any means, but I've sure lost my touch with error correction. Starting to come back to me, but sure is slow.Probably doesn't help that I'm learning c#/asp.net at work instead of being able to focus on php... oh well.Anyway... I shall let you know my success on removing the echo's... echo's... echo's...EDIT: I've removed the echo's and it works! Thank god its Friday!

<?phpif (!isset($_POST['submit'])) {?><html><head><title>InfoBox Generator</title></head><body><h1>Info Box Generator</h1><form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"><input type="text" name="infoBox" style="width:75%;height:25%" /><br /><input type="submit" value="submit" name="submit" /></form><a href="index.php">Back to main</a></body></html><?php}else {$infoBox = $_POST["infoBox"];?><html><head><title>InfoBox Generator</title></head><body><h1>Info Box Generated Code</h1><textarea style="width:75%;height:25%;text-align:left;"><p class="info" style="width:50%;height:25%"><?php echo $infoBox;?>></p></textarea><br /><a href="index.php">Back to main</a></body></html><?php}?>

Link to post
Share on other sites

You're a good student. I was afraid you were going to ask for a big old example, but that's just the way it's done, right down to the silly little brace at the end. I think it's one of the most innovative little tricks in all of scripting. :)

Link to post
Share on other sites
  • 8 months later...
I was having trouble with doing that Deidre's Dad. But I also had a few other syntax errors, and I was just narrowing it down by using the echo's. (and yes, it was driving me crazy).I'll see if I can get it to work without them now. you should see my other "generator" file :) It's just a huge mess... works, but it sure is ugly.I'm so out of practice on php... never was a guru by any means, but I've sure lost my touch with error correction. Starting to come back to me, but sure is slow.Probably doesn't help that I'm learning c#/asp.net at work instead of being able to focus on php... oh well.Anyway... I shall let you know my success on removing the echo's... echo's... echo's...EDIT: I've removed the echo's and it works! Thank god its Friday!
<?phpif (!isset($_POST['submit'])) {[b]?>[/b]<html><head><title>InfoBox Generator</title></head><body><h1>Info Box Generator</h1><form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"><input type="text" name="infoBox" style="width:75%;height:25%" /><br /><input type="submit" value="submit" name="submit" /></form><a href="index.php">Back to main</a></body></html>[b]<?php[/b]}else {$infoBox = $_POST["infoBox"];[b]?>[/b]<html><head><title>InfoBox Generator</title></head><body><h1>Info Box Generated Code</h1><textarea style="width:75%;height:25%;text-align:left;"><p class="info" style="width:50%;height:25%"><?php echo $infoBox;?>></p></textarea><br /><a href="index.php">Back to main</a></body></html>[b]<?php[/b]}?>

Sorry to bump up this old thread, I stumbled across it while searching for some reference.Anyway why are those needed? Can somebody explain to me? It's in bold ^^^
Link to post
Share on other sites

Because you need to exit the PHP block to write the HTML code. If you were still in PHP you would need to use echo to write everything out, which is harder.

<?php if ($test) { ?><some />HTML<?php } ?>

<?php if ($test) {	echo "<some />HTML";}?>

Link to post
Share on other sites

PHP was designed with this functionality. Other languages would certainly break, but PHP is very flexible about what goes between the curly braces. The only rule in this situation is that the curly braces themselves must be enclosed in <?php ?> tags. The "content" of the curly braces does not have to be, and it doesn't have to be PHP code. If the content inside the curly braces exists outside the script tags, it will be not be processed as PHP -- it will simply be printed.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...