rameshfaj Posted June 3, 2007 Share Posted June 3, 2007 I have a html form A and a php file B. The php file B calls the html form A that allows the user to enter data. The data entered in the form A is sent to file B via POST action. The file B needs to check whether some fields are empty? If B finds that some fields were empty, then it should again reload the html form A with the right entries retained in the form and (wrong entries being cleared). In this case how to pass the information to the form A. It is to be noted that B calls A and A passes data to B via POST as far as possible. All kinds of help are appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/54073-passing-parameter/ Share on other sites More sharing options...
pocobueno1388 Posted June 3, 2007 Share Posted June 3, 2007 On file B: <?php if (!empty($_POST['var1'])){ $_SESSION['var1'] = $_POST['var1']; } ?> A.html file: <form action="b.php" method="POST"> <input type="text" name="var1" value="<?php echo $_SESSION['var1']; ?>"> </form> So basically use sessions. First check if any of the fields are empty, and if there is an empty field register all the non-empty fields as sessions. Then when they go back to the form it will echo the non-empty fields and leave the empty ones blank. Quote Link to comment https://forums.phpfreaks.com/topic/54073-passing-parameter/#findComment-267312 Share on other sites More sharing options...
chronister Posted June 3, 2007 Share Posted June 3, 2007 On file B: <?php if (!empty($_POST['var1'])){ $_SESSION['var1'] = $_POST['var1']; } ?> A.html file: <form action="b.php" method="POST"> <input type="text" name="var1" value="<?php echo $_SESSION['var1']; ?>"> </form> So basically use sessions. First check if any of the fields are empty, and if there is an empty field register all the non-empty fields as sessions. Then when they go back to the form it will echo the non-empty fields and leave the empty ones blank. A.html will most likely have to be renamed to a.php Unless your server is specifically set up for it, .html files cannot be parsed as php files. Also, this will make that variable be displayed every time the page is loaded because it is a session.(after the session is set of course) I do this kind of thing by checking the fields in php once the form is submitted. If it finds an error, it sets an $error[] array with a message and loops through the error messages and displays them, then in the form itself you would do basically what pocobueno said. <?php if (!empty($_POST['var1'])){ $error[] = 'This field is is not valid'; } ?> <?php if(isset($error)){foreach($error as $error){echo $error.'<br>';}} ?> <form action="b.php" method="POST"> <input type="text" name="var1" value="<?php if(isset($error)){echo $var1;} ?>"> <input type="text" name="var2" value="<?php if(isset($error)){echo $var2;} ?>"> <input type="text" name="var3" value="<?php if(isset($error)){echo $var3;} ?>"> </form> This will pre-fill in the fields that had data so the user does not have to fill out the form all over again. I sincerely suggest making this conditional upon an $error variable because if the form submits to itself, on a successful entry it will fill in the form again with the data that was just sent.... I have made that mistake a time or two. Hope this is clear enough for ya. Nate Quote Link to comment https://forums.phpfreaks.com/topic/54073-passing-parameter/#findComment-267341 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.