kevgray Posted June 14, 2007 Share Posted June 14, 2007 Hi folks, This is my first visit to this forum. I'm looking for any tutorials to do with FORMS. I have coded my own but I'm not totally happy with it and would like to 'clean it up'. What I want to achieve is - Display a FORM Receive the input Validate the input If valid, display the information and ask for confirmation If not valid re-display the form for correction If confirmed, write the output to a .csv file and display a 'thank you' ???message If not confirmed go back to the original form so it can be changed. I am currently doing this in 3 php scripts but I'm sure there must be a better way. Thanks in advance for any info you can provide. Kev Gray Quote Link to comment https://forums.phpfreaks.com/topic/55599-form-tutorials/ Share on other sites More sharing options...
kevgray Posted June 14, 2007 Author Share Posted June 14, 2007 ....surely someone can point me somewhere.................please !!!!! Quote Link to comment https://forums.phpfreaks.com/topic/55599-form-tutorials/#findComment-274923 Share on other sites More sharing options...
wildteen88 Posted June 14, 2007 Share Posted June 14, 2007 You can do it all in one script. When you submit the form you will want it to submit to itself by having $_SERVER['PHP_SELF'] as the action eg: <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Make sure your submit button has a name, for example submit. Then some place in your script you have an if statement that will check whether $_POST['submit'] variable exist like so: if(isset($_POST['submit'])) { // do all validation here // if it passes validation save post'd data to .csv } Here is a working example of what I mean: <?php // check that form has been submitted // if _POST['submit'] var exists it has been submitted if(isset($_POST['submit'])) { // lets validate posted data $err = null; // check that the name field has been filled in // and that it only contains letters and spaces if(!isset($_POST['name']) || !eregi('[a-zA-Z ]+', $_POST['name'])) { $err[] = 'Make sure you have filled in your name and that it only contains letters'; } // check that the age field has been filled in // and that it contains only numbers if(!isset($_POST['age']) || !is_numeric($_POST['age'])) { $err[] = 'Make sure you have filled in your age and that it only contains numbers'; } // check whether any errors have been flaged from validation proccess above: if(is_array($err)) { // errors have been flagged // lets display them // The form will be shown too. echo 'Please correct the following errros:'; echo "\n<br />\n<ul>\n <li>" . implode("</li>\n <li>", $err) . "</li>\n</ul>\n<br />\n"; } else { // no errors have been flagged // data is valid, lets add it to a .csv file // open the .csv file $csv_handler = fopen('data.csv', 'a'); // remove the submit var. We don't want to store this. unset($_POST['submit']); // prepare data to be added to csv file // format to be added to file: // "[name]", "[age]" $csv_entry = '"' . implode('", "', $_POST) . "\"\r\n"; // write the prepared string to the csv file fwrite($csv_handler, $csv_entry, strlen($csv_entry)); // close the file fclose($csv_handler); // print a successfully added message echo "Successfully added name and age to csv"; exit; } } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Name: <input type="text" name="name" value="<?php echo isset($_POST['name']) ? $_POST['name'] : '' ?>" /><br /> Age: <input type="text" name="age" value="<?php echo isset($_POST['age']) ? $_POST['age'] : '' ?>"><br /> <input type="submit" name="submit" value="Add" /> </form> Note it is basic and is just a simple example of how to process form information on the same page. Quote Link to comment https://forums.phpfreaks.com/topic/55599-form-tutorials/#findComment-274933 Share on other sites More sharing options...
kevgray Posted June 14, 2007 Author Share Posted June 14, 2007 Thanks Wildteen for your answer,. The other thing I want to do in the script is, after all data is valid but not necessarily correct (the user may have made a 'valid' mistake) is to display the input and ask for confirmation that the data is correct before commiting the data to the csv file. Currently I am doing this in another script and passing input data to it via a form with 'hidden' fields. Is this possible to acomplish in the single script. My current site using what I have now is at www.isra2008.com/register_over.php I hope this makes sense. Thanks very much for you time. Cheers Kev Quote Link to comment https://forums.phpfreaks.com/topic/55599-form-tutorials/#findComment-274999 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.