fareedreg Posted December 21, 2009 Share Posted December 21, 2009 Dear all thanks for your support. I have a form1.php in which I have 5 no's of text boxes. Name,Date,amount1,amount2,amount3.... All fields are compulsory. I would like to make a form validate.php which will validate that amount1 cannot be greater than amount2 and amount2 cannot be less than amount3.. How can i validate this in validate.php and also i would like to make a loop until these amounts are valid control get back to form1.php along with previously entered amount.. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/185904-form-validation/ Share on other sites More sharing options...
Adam Posted December 21, 2009 Share Posted December 21, 2009 Is this what you're after..? if ($amount1 > $amount2 && $amount2 < $amount3) { // invalid } Edit: To make all compulsory: if (!isset($_POST['amount1'], $_POST['amount2'], $_POST['amount3'])) { // invalid } If you want help more relevant to your code you'll need to.. provide some code. Quote Link to comment https://forums.phpfreaks.com/topic/185904-form-validation/#findComment-981699 Share on other sites More sharing options...
ChemicalBliss Posted December 21, 2009 Share Posted December 21, 2009 Ok, you are obviously new to php so my advice is take it one step at a time. First, get the structure ready:Create form1.php with the ability to return the values that were previously entered. Then you can add validation. So the easiest method is to use sessions (if your going from page to page); eg: form1.php <?php // First start the session // Set some default values for the fields. can be blank, but needed if you dont want E_NOTICE errors. eg: $form['name'] = 'Please enter your name'; // Check if certain session variables are set, if they are then save them to some variables eg: if(isset($_SESSION['name'])){ $form['name'] = $_SESSION['name']; // So instead of the default value, you have the value that is saved in the session. } // Display the form with the default values (or modified session values); eg: ?> <input type="text" value="<?php echo($form['name']); ?>" name="name" id="name" /> <?php // That's it ?> Then with validate.php: <?php // Start the session like in form1.php // Check all the $_POST values (values submitted by the form) // If there is an error, save all the $_POST values into $_SESSION values, eg; $_SESSION['name'] = $_POST['name']; // NOTE: Saving any $_POST/$_GET or $_REQUEST variable directly can result in security vulnerabilities. Make sure you don't use code like this for a production (public) website. // Then you can use a header() redirect back to form1.php, and the form will automatically preserve the values. // Once you get this far, the specific validation you need will be very simple. Obviously if there is no problem validating, then you can continue with whatever comes after form1.php (whatever your doing with the data). ?> This should get you started, dont forget to read tutorials, and check out the php manual (http://www.php.net/manual/en/getting-started.php). Good Luck! -CB- Quote Link to comment https://forums.phpfreaks.com/topic/185904-form-validation/#findComment-981702 Share on other sites More sharing options...
fareedreg Posted December 21, 2009 Author Share Posted December 21, 2009 Thanks for your advice adam but I would like to know after printing error from validate.php how can i get my form1.php form back with previously filled values. Is this what you're after..? if ($amount1 > $amount2 && $amount2 < $amount3) { // invalid } Edit: To make all compulsory: if (!isset($_POST['amount1'], $_POST['amount2'], $_POST['amount3'])) { // invalid } If you want help more relevant to your code you'll need to.. provide some code. Quote Link to comment https://forums.phpfreaks.com/topic/185904-form-validation/#findComment-981703 Share on other sites More sharing options...
ChemicalBliss Posted December 21, 2009 Share Posted December 21, 2009 The answer to that is in my post - use sessions to store the data so it doesnt get lost between pages. You will need to use these functions: session_start(); // starts the session. isset(); // checks wether a variable has been set/exists. header(); // Sends specific headers to the browser telling it what to expect or do. You will also need to use these super globals: $_SESSION; // Contains every variable stored in the current session. $_POST; // Contains the post values from the form. Ofcourse you will need to use a few if statements, else if's and else. Good Luck -CB- Quote Link to comment https://forums.phpfreaks.com/topic/185904-form-validation/#findComment-981705 Share on other sites More sharing options...
Adam Posted December 21, 2009 Share Posted December 21, 2009 I'd disagree there ChemicalBliss, $_SESSION shouldn't be used to store form data in that way. Instead you should create a template, or a file that you can include (either via a template system or regular file includes) that stores the error output and the actual form. Then the input value attributes are populated by the $_POST / $_GET array. That way either at the form or the validation level you can include it and the fields will be populated. Also if the user purposefully navigates from the form the values won't still be stored. Quote Link to comment https://forums.phpfreaks.com/topic/185904-form-validation/#findComment-981713 Share on other sites More sharing options...
ChemicalBliss Posted December 22, 2009 Share Posted December 22, 2009 I'd agree, but he wanted a seperate validation file in which case GET/POST data would be lost upon returning to the form. SESSIONS also help him to understand what they can be used for, it's not perfect, but it will work. Two different methods achieve the same result, just that my method is more specific to his original question. -CB- Though if you can do it, merge validation into form1.php - it is more efficient to have the form and validation in one place, executed at once. Quote Link to comment https://forums.phpfreaks.com/topic/185904-form-validation/#findComment-982206 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.