boom_roasted Posted January 29, 2010 Share Posted January 29, 2010 Story.html <!doctype html public "-//W3C//DTD HTML 4.0 //EN"> <html> <head> <title>Story</title> </head> <body> <h1>Story</h1> <h3> Please fill in the blanks below, and I'll tell you a story</h3> <form method = "post" action = "Story.php"> <table border = 1> <tr> <th>Color:</th> <th> <input type = "text" name = "color" value = ""> </th> </tr> <tr> <th>Musical Instrument</th> <th> <input type = "text" name = "instrument" value = ""> </th> </tr> <tr> <th>Animal</th> <th> <input type = "text" name = "anim1" value = ""> </th> </tr> <tr> <th>Another Animal</th> <th> <input type = "text" name = "anim2" value = ""> </th> </tr> <tr> <th>Yet another animal!</th> <th> <input type = "text" name = "anim3" value = ""> </th> </tr> <tr> <th>Place</th> <th> <input type = "text" name = "place" value = ""> </th> </tr> <tr> <th>Vegetable</th> <th> <input type = "text" name = "vegetable" value = ""> </th> </tr> <tr> <th>A structure</th> <th> <input type = "text" name = "structure" value = ""> </th> </tr> <tr> <th>An action</th> <th> <select name = "action"> <option value = "fast asleep">fast asleep</option> <option value = "drinking cappuccino">drinking cappuccino</option> <option value = "wandering around aimlessly">wandering around aimlessly</option> <option value = "doing nothing in particular">doing nothing in particular</option> </select? </th> </tr> <tr> <td colspan = 2> <center> <input type = "submit" value = "tell me the story"> </center> </td> </tr> </table> </form> </body> </html> Story.php <!doctype html public "-//W3C//DTD HTML 4.0 //EN"> <html> <head> <title>Little Boy Who?</title> </head> <body> <center> <h1> Little Boy Who?</h1> <? print <<<HERE <h3> Little Boy $color, come blow your $instrument!<br> The $anim1's in the $place, the $anim2's in the $vegetable.<br> Where's the boy that looks after the $anim3?<br> He's under the $structure, $action. </h3> HERE; ?> </center> </body> </html> I want this to be in one file that asks for the variables and tells the story. You know, combining the form and program into one file. How do I do this? Quote Link to comment https://forums.phpfreaks.com/topic/190191-taking-an-html-form-and-a-php-program-and-making-it-into-one-working-file/ Share on other sites More sharing options...
boom_roasted Posted January 29, 2010 Author Share Posted January 29, 2010 does anybody know? Quote Link to comment https://forums.phpfreaks.com/topic/190191-taking-an-html-form-and-a-php-program-and-making-it-into-one-working-file/#findComment-1003435 Share on other sites More sharing options...
boom_roasted Posted January 29, 2010 Author Share Posted January 29, 2010 PLEASE HELP! I'll check this when I wake up tomorrow. Quote Link to comment https://forums.phpfreaks.com/topic/190191-taking-an-html-form-and-a-php-program-and-making-it-into-one-working-file/#findComment-1003452 Share on other sites More sharing options...
premiso Posted January 29, 2010 Share Posted January 29, 2010 Pending any syntax errors I may have created here is one way to do it: <?php if (isset($_POST['submit'])) { $title = "Little Boy Who?"; $output = <<<HERE <h3> Little Boy {$_POST['color']}, come blow your {$_POST['instrument']}!<br> The {$_POST['anim1']}'s in the {$_POST['place']}, the {$_POST['anim2']}'s in the {$_POST['vegetable']}.<br> Where's the boy that looks after the {$_POST['anim3']}?<br> He's under the {$_POST['structure']}, {$_POST['action']}. </h3> HERE; }else { $title = "Story"; $output = <<<HERE <h3> Please fill in the blanks below, and I'll tell you a story</h3> <form method = "post" action = "Story.php"> <table border = 1> <tr> <th>Color:</th> <th> <input type = "text" name = "color" value = ""> </th> </tr> <tr> <th>Musical Instrument</th> <th> <input type = "text" name = "instrument" value = ""> </th> </tr> <tr> <th>Animal</th> <th> <input type = "text" name = "anim1" value = ""> </th> </tr> <tr> <th>Another Animal</th> <th> <input type = "text" name = "anim2" value = ""> </th> </tr> <tr> <th>Yet another animal!</th> <th> <input type = "text" name = "anim3" value = ""> </th> </tr> <tr> <th>Place</th> <th> <input type = "text" name = "place" value = ""> </th> </tr> <tr> <th>Vegetable</th> <th> <input type = "text" name = "vegetable" value = ""> </th> </tr> <tr> <th>A structure</th> <th> <input type = "text" name = "structure" value = ""> </th> </tr> <tr> <th>An action</th> <th> <select name = "action"> <option value = "fast asleep">fast asleep</option> <option value = "drinking cappuccino">drinking cappuccino</option> <option value = "wandering around aimlessly">wandering around aimlessly</option> <option value = "doing nothing in particular">doing nothing in particular</option> </select? </th> </tr> <tr> <td colspan = 2> <center> <input type = "submit" name="submit" value = "tell me the story"> </center> </td> </tr> </table> </form> HERE; } ?> <!doctype html public "-//W3C//DTD HTML 4.0 //EN"> <html> <head> <title><?php echo $title; ?></title> </head> <body> <h1><?php echo $title;?></h1> <?php echo $output; ?> </body> </html> EDIT: Fixed the post variables to be correctly accesed. Quote Link to comment https://forums.phpfreaks.com/topic/190191-taking-an-html-form-and-a-php-program-and-making-it-into-one-working-file/#findComment-1003454 Share on other sites More sharing options...
boom_roasted Posted January 29, 2010 Author Share Posted January 29, 2010 Pending any syntax errors I may have created here is one way to do it: <?php if (isset($_POST['submit'])) { $title = "Little Boy Who?"; $output = <<<HERE <h3> Little Boy {$_POST['color']}, come blow your {$_POST['instrument']}!<br> The {$_POST['anim1']}'s in the {$_POST['place']}, the {$_POST['anim2']}'s in the {$_POST['vegetable']}.<br> Where's the boy that looks after the {$_POST['anim3']}?<br> He's under the {$_POST['structure']}, {$_POST['action']}. </h3> HERE; }else { $title = "Story"; $output = <<<HERE <h3> Please fill in the blanks below, and I'll tell you a story</h3> <form method = "post" action = "Story.php"> <table border = 1> <tr> <th>Color:</th> <th> <input type = "text" name = "color" value = ""> </th> </tr> <tr> <th>Musical Instrument</th> <th> <input type = "text" name = "instrument" value = ""> </th> </tr> <tr> <th>Animal</th> <th> <input type = "text" name = "anim1" value = ""> </th> </tr> <tr> <th>Another Animal</th> <th> <input type = "text" name = "anim2" value = ""> </th> </tr> <tr> <th>Yet another animal!</th> <th> <input type = "text" name = "anim3" value = ""> </th> </tr> <tr> <th>Place</th> <th> <input type = "text" name = "place" value = ""> </th> </tr> <tr> <th>Vegetable</th> <th> <input type = "text" name = "vegetable" value = ""> </th> </tr> <tr> <th>A structure</th> <th> <input type = "text" name = "structure" value = ""> </th> </tr> <tr> <th>An action</th> <th> <select name = "action"> <option value = "fast asleep">fast asleep</option> <option value = "drinking cappuccino">drinking cappuccino</option> <option value = "wandering around aimlessly">wandering around aimlessly</option> <option value = "doing nothing in particular">doing nothing in particular</option> </select? </th> </tr> <tr> <td colspan = 2> <center> <input type = "submit" name="submit" value = "tell me the story"> </center> </td> </tr> </table> </form> HERE; } ?> <!doctype html public "-//W3C//DTD HTML 4.0 //EN"> <html> <head> <title><?php echo $title; ?></title> </head> <body> <h1><?php echo $title;?></h1> <?php echo $output; ?> </body> </html> EDIT: Fixed the post variables to be correctly accesed. Thanks for the help. When I run that, I don't get any errors to start with, but when I click to submit it, it leads me to a "page cannot be found" page on internet explorer. Any idea why this is happening? Quote Link to comment https://forums.phpfreaks.com/topic/190191-taking-an-html-form-and-a-php-program-and-making-it-into-one-working-file/#findComment-1003733 Share on other sites More sharing options...
premiso Posted January 29, 2010 Share Posted January 29, 2010 You need to save the above code to "Story.php" or else it will not work properly (or change the <form action to be the correct page name). Quote Link to comment https://forums.phpfreaks.com/topic/190191-taking-an-html-form-and-a-php-program-and-making-it-into-one-working-file/#findComment-1003746 Share on other sites More sharing options...
Shocker88 Posted January 29, 2010 Share Posted January 29, 2010 The issue is the action of the form - that script checks in itself if the form has been submitted and returns the story if it has. therefore change <form method = "post" action = "Story.php"> to: <form method = "post" action = ""> This will reload the same page, and now that isset($_POST['submit'])) returns true (since the form has been submitted) it will display the story back to the user. EDIT: beaten to the punch - I'll leave it up as it may help explain a little better Quote Link to comment https://forums.phpfreaks.com/topic/190191-taking-an-html-form-and-a-php-program-and-making-it-into-one-working-file/#findComment-1003748 Share on other sites More sharing options...
boom_roasted Posted January 29, 2010 Author Share Posted January 29, 2010 Thanks. I'll try that out when i get to my class in an hour. The computer I'm on now is in the school's lab and it doesn't have Apache. Quote Link to comment https://forums.phpfreaks.com/topic/190191-taking-an-html-form-and-a-php-program-and-making-it-into-one-working-file/#findComment-1003750 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.