MockY Posted July 16, 2010 Share Posted July 16, 2010 I am using file_get_contents() and preg_match() in order to extract specific information from a public database. This information is stored in variables for being used in populating a form automatically. The process is as follows: The user is presented with an empty form. Instead of having to fill out all the fields by hand, the user has an option of entering a number in one of the fields and by clicking a button, the form will fetch information based on that number from the public database and populate some of the fields with this information. I have in mostly figured out, but there is one thing I don't really understand why it wont work. If I store the entire form in a variable and later print it out once the form is processed, the variables can't be used as values for the fields. But if I echo out the form as a string, then I can. That part simply don't make sense. Let me demonstrate. The following will not display the fetched address in the field once the form is run $form = ' <form method="post" action="testing.php"> <table > <tr> <td>License #</td> <td>Street 1</td> </tr> <tr> <td><input type="text" name="licenumber" value="" style="width:150px;" /></td> <td><input type="text" name="street1" value="'. $extractedaddress .'" style="width:150px;" /></td> </tr> </table> <input type="submit" name="test" value="Process" /> </form>'; if (isset ($_POST['test'])) { // Grab the address based on the number from the website using file_get_contents // Store the address in a variable $extractedaddress = '1234 Some Street'; } echo $form; } else { echo $form; } The following does work just fine and is practically the same thing. But instead of echoing out the $form variable, I echo out the form itself. In my book, this is the same as above, but for some reason it works $form = ' <form method="post" action="testing.php"> <table > <tr> <td>License #</td> <td>Street 1</td> </tr> <tr> <td><input type="text" name="licenumber" value="" style="width:150px;" /></td> <td><input type="text" name="street1" value="'. $extractedaddress .'" style="width:150px;" /></td> </tr> </table> <input type="submit" name="test" value="Process" /> </form> '; if (isset ($_POST['test'])) { // Grab the address based on the number from the website using file_get_contents // Store the address in a variable $extractedaddress = '1234 Some Street'; } echo '<form method="post" action="testing.php"> <table > <tr> <td>License #</td> <td>Street 1</td> </tr> <tr> <td><input type="text" name="licenumber" value="" style="width:150px;" /></td> <td><input type="text" name="street1" value="'. $extractedaddress .'" style="width:150px;" /></td> </tr> </table> <input type="submit" name="test" value="Process" /> </form>'; } else { echo $form; } Why does the second work but not the first example, and what can I do to make it work? Link to comment https://forums.phpfreaks.com/topic/207963-pre-populating-of-form-fields-not-working-like-it-should/ Share on other sites More sharing options...
wildteen88 Posted July 16, 2010 Share Posted July 16, 2010 You must define variables before you can use them. PHP does not backtrack. To fix the issue place your form proccessing code before you define the form, eg if (isset ($_POST['test'])) { // Grab the address based on the number from the website using file_get_contents // Store the address in a variable $extractedaddress = '1234 Some Street'; } $form = ' <form method="post" action="testing.php"> <table > <tr> <td>License #</td> <td>Street 1</td> </tr> <tr> <td><input type="text" name="licenumber" value="" style="width:150px;" /></td> <td><input type="text" name="street1" value="'. $extractedaddress .'" style="width:150px;" /></td> </tr> </table> <input type="submit" name="test" value="Process" /> </form>'; echo $form; Link to comment https://forums.phpfreaks.com/topic/207963-pre-populating-of-form-fields-not-working-like-it-should/#findComment-1087153 Share on other sites More sharing options...
MockY Posted July 16, 2010 Author Share Posted July 16, 2010 But since $form contains a variable call and is printed out at the same place as the form itself, in my mind, that would be the same thing. Either way, thanks for you help. I will put the processing code on top of my pages from now on. Link to comment https://forums.phpfreaks.com/topic/207963-pre-populating-of-form-fields-not-working-like-it-should/#findComment-1087162 Share on other sites More sharing options...
wildteen88 Posted July 16, 2010 Share Posted July 16, 2010 PHP will call for the variable $extractedaddress while your defining your $form variable. Not when you're going to use the $form variable. This is why it is important to make sure you're defining variables before you use them. Link to comment https://forums.phpfreaks.com/topic/207963-pre-populating-of-form-fields-not-working-like-it-should/#findComment-1087167 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.