madsovenielsen Posted May 23, 2009 Share Posted May 23, 2009 Hey all. I was wondering if its okay to have form input and output on the same page. <div> <form action="index.php" method="post"> Name<br /> <input type="text" name="name" /><br /> Age<br /> <input type="text" name="age" /> <br /><br /> <input type="submit" /> </form> <?php $name = $_POST["name"]; $age = $_POST["age"]; echo "Your name is ", $name ," you are ", $age, " years old"; ?></div> I get an error message as long as theres no form input. i know how to fix this. but is this approach okay ?/mads Link to comment Share on other sites More sharing options...
chibineku Posted May 23, 2009 Share Posted May 23, 2009 From my limited understanding of php, you can post a page to itself:<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">Then you can grab the POSTed variables and plug them in. You'll need to have some sort of error handling at the start of the page, deciding whether the variables you are POSTing are set, and if not, assign some default values for the page to plug in to the form. Link to comment Share on other sites More sharing options...
jlhaslip Posted May 23, 2009 Share Posted May 23, 2009 <?php $page_title = 'Widget Cost Calculator';// include ('./includes/header.html');/* This function calculates a totaland then prints the results. */function calculate_total ($tax = 5) { global $total; $taxrate = $tax / 100; // Turn 5% into .05. $total = ($_POST['quantity'] * $_POST['price']) * ($taxrate + 1); $total = number_format ($total, 2); } // End of function.// Check if the form has been submitted.if (isset($_POST['submitted'])) { if (is_numeric($_POST['quantity']) && is_numeric($_POST['price'])) { // Print the heading. echo '<h1 id="mainhead">Total Cost</h1>'; $total = NULL; // Initialize $total. if (is_numeric($_POST['tax'])) { calculate_total ($_POST['tax']); } else { calculate_total (); } echo '<p>The total cost of purchasing ' . $_POST['quantity'] . ' widget(s) at $' . number_format ($_POST['price'], 2) . ' each is $' . $total . '.</p>'; // Print some spacing. echo '<p><br /></p>'; } else { // Invalid submitted values. echo '<h1 id="mainhead">Error!</h1> <p class="error">Please enter a valid quantity and price.</p><p><br /></p>'; } } // End of main isset() IF.// Leave the PHP section and create the HTML form.?><h2>Widget Cost Calculator</h2><form action="calculator.php" method="post"> <p>Quantity: <input type="text" name="quantity" size="5" maxlength="10" value="<?php if (isset($_POST['quantity'])) echo $_POST['quantity']; ?>" /></p> <p>Price: <input type="text" name="price" size="5" maxlength="10" value="<?php if (isset($_POST['price'])) echo $_POST['price']; ?>" /></p> <p>Tax (%): <input type="text" name="tax" size="5" maxlength="10" value="<?php if (isset($_POST['tax'])) echo $_POST['tax']; ?>" /> (optional)</p> <p><input type="submit" name="submit" value="Calculate!" /></p> <input type="hidden" name="submitted" value="TRUE" /></form><?php// include ('./includes/footer.html');?> See also: http://www.peachpit.com/articles/article.aspx?p=412368 Link to comment Share on other sites More sharing options...
madsovenielsen Posted May 23, 2009 Author Share Posted May 23, 2009 Thats great stuff. thanks alot. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.