dadamssg Posted January 23, 2009 Share Posted January 23, 2009 i have a form and it has three required fields. if one of the fields is left blank it redisplays the form with an error message....but CLEARS the form...booo. How do i display the errors but keep the input? session variables will work for text input but what about selection lists and radio buttons? i think ajax would be the answer but can't find a good tutorial to explain that and don't really feel like learning another programming language at the moment. thanks Link to comment https://forums.phpfreaks.com/topic/142078-way-to-save-input-but-display-errors/ Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 Hey there, the more languages you know the better, anyway: Basically when you redisplay the form, you put the $_POST['username'] etc values into the "value" field of the input boxes. eg: <input type="text" name="username" value="<?=$_POST['username'];?>"> i hope this helps Link to comment https://forums.phpfreaks.com/topic/142078-way-to-save-input-but-display-errors/#findComment-744051 Share on other sites More sharing options...
dadamssg Posted January 23, 2009 Author Share Posted January 23, 2009 how about those selection lists and radio buttons? Link to comment https://forums.phpfreaks.com/topic/142078-way-to-save-input-but-display-errors/#findComment-744053 Share on other sites More sharing options...
N1CK3RS0N Posted January 23, 2009 Share Posted January 23, 2009 Use sticky forms. Basically you submit the form to the same page. You use an if/else condition to display either the form or the results. IF the form is submitted then you will see results. ELSE you will see the form. Then for the value of the input you use the $_GET or $_REQUEST to return whatever was entered into that input. So if you have a text input for someones name. You would do something like this <?php $page_title = 'Widget Cost Calculator'; include ('includes/header.html'); // Check for form submission: if (isset($_POST['submitted'])) { // Minimal form validation: if ( is_numeric($_POST['quantity']) && is_numeric($_POST['price']) && is_numeric($_POST['tax']) ) { // Calculate the results: $total = ($_POST['quantity'] * $_POST['price']); $taxrate = ($_POST['tax'] / 100); // Turn 5% into .05. $total += ($total * $taxrate); // Add the tax. // Print the results: echo '<h1>Total Cost</h1> <p>The total cost of purchasing ' . $_POST['quantity'] . ' widget(s) at $' . number_format ($_POST['price'], 2) . ' each, including a tax rate of ' . $_POST['tax'] . '%, is $' . number_format ($total, 2) . '.</p>'; } else { // Invalid submitted values. echo '<h1>Error!</h1> <p class="error">Please enter a valid quantity, price, and tax.'; } } // End of main isset() IF. // Leave the PHP section and create the HTML form: ?> <h1>Widget Cost Calculator</h1> <form action="calculator.php" method="post"> <p>Quantity: <input type="text" name="quantity" size="5" maxlength="5" 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="5" value="<?php if (isset($_POST['tax'])) echo $_POST['tax']; ?>" /></p> <p><input type="submit" name="submit" value="Calculate!" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php // Include the footer: include ('includes/footer.html'); ?> Link to comment https://forums.phpfreaks.com/topic/142078-way-to-save-input-but-display-errors/#findComment-744058 Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 You would use a if statement to check which one is selected, eg: form: <?php // $_POST['selection'] will hold the value for the selection options. This uses an array to make it simple. $selected[$_POST['selection']] = "selected"; ?> <input type="select" name="selection"> <option value="number1" <?=@$selected['number1'];?>>Number 1</option> <option value="number2" <?=@$selected['number2'];?>>Number 2</option> <option value="number3" <?=@$selected['number3'];?>>Number 3</option> </input> ---- Radios are even easier: <?php $radioselected[$_POST['rusty_with_radios']] = "selected"; ?> <input type="radio" name="rusty_with_radios" value="number1" <?=@$radioselected['number1'];?>> <input type="radio" name="rusty_with_radios" value="number2" <?=@$radioselected['number2'];?>> Hope this helps, Link to comment https://forums.phpfreaks.com/topic/142078-way-to-save-input-but-display-errors/#findComment-744059 Share on other sites More sharing options...
haku Posted January 23, 2009 Share Posted January 23, 2009 <option value="number1" <?=@$selected['number1'];?>>Number 1</option> As a side point, using this php method (<?=____?>) is considered a bad practice, as some servers have short tags off by default, meaning that this code will not be portable to such servers. And if the hosting company ever changes their default settings suddenly, this will stop working. Link to comment https://forums.phpfreaks.com/topic/142078-way-to-save-input-but-display-errors/#findComment-744069 Share on other sites More sharing options...
uniflare Posted January 23, 2009 Share Posted January 23, 2009 yeh lol got lazy on that 1 Link to comment https://forums.phpfreaks.com/topic/142078-way-to-save-input-but-display-errors/#findComment-744071 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.