Paola Posted April 29, 2019 Share Posted April 29, 2019 I am trying to create a code where the user enters the zip code and if found in the database, a location will be retrieved. I am getting two undefined variables as errors. I am still learning the POST and GET method and I fear that is where I am getting something mixed up. I have also attached an image of my DB. Thank you so much any input is greatly appreciated. Notice: Undefined variable: address in C:\xampp\htdocs\index.php on line 53 Notice: Undefined variable: zip in C:\xampp\htdocs\index.php on line 54 index.php file code: <?php include ('header.php'); include ('function.php'); ?> <div class="wrapper"> <div> <?php echo'<form method="POST" action"'.getLocations($conn).'"> <input type="text" name="zip" class="search" placeholder="Zip code"><br> <button type="submit" value="submit" id="submit">Submit</button> </form>'; getLocations($conn); ?> </div> </div> <div> <!--foreach($zip as $zip) : --> <?php echo $address['address']; echo $zip['zip']; ?> </div> function.php file code: <?php $dBServername = "localhost"; $dBUsername = "root"; $dBPassword = ""; $dBName = "addresses"; // Create connection $conn = mysqli_connect($dBServername, $dBUsername, $dBPassword, $dBName); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } function getLocations($conn) { if (isset($_POST['submit'])){ // Validate Zip code field if (!empty ($_POST['zip']) && is_numeric ($_POST['zip'])) { $zip = (int)$_POST['zip']; $query = "SELECT * FROM locations WHERE zip = '$zip'"; //get the results $result = mysqli_query($conn, $query); //fetch the data $zip = mysqli_fetch_all($result, MYSQLI_ASSOC); var_dump($zip); mysqli_free_result($result); //close connection mysqli_close($conn); } } } Quote Link to comment https://forums.phpfreaks.com/topic/308651-php-undefined-variable-unable-to-retrieve-data/ Share on other sites More sharing options...
requinix Posted April 29, 2019 Share Posted April 29, 2019 There are a few issues here: echo'<form method="POST" action"'.getLocations($conn).'"> 1. getLocations() is a function that does not return anything. You can't use it with a string like that. 2. The form action is supposed to be a URL. If you want to submit the form to the same page it's on you can leave the action empty or missing. 3. There needs to be an equals sign with that action. <!--foreach($zip as $zip) : --> 4. That's PHP code. If you need to comment it out, keep it inside the <?php tags and use a PHP comment, not an HTML comment. 5. $zip that was set inside the getLocations() function is not going to be available to use outside the function. You should make the function return the value, then when you call it assign the returned value to a variable. 6. The function is only set to do anything when the form is submitted. Make sure that everything still works even when the form hasn't been submitted yet. 7. Don't iterate over a variable and then use that same variable name for the individual values. You'll confuse yourself. Perhaps use $zips and $zip? echo $address['address']; 8. $address is never defined anywhere. Did you mean $zip? But... echo $zip['zip']; 9. Since you commented out the foreach loop, and because of #5, there is no $zip to use. if (isset($_POST['submit'])){ 10. Your form's submit button is not named "submit". function getLocations($conn) { 11. This function requires that you pass it an argument for the database connection (which you have also named $conn). You aren't doing that. 1 Quote Link to comment https://forums.phpfreaks.com/topic/308651-php-undefined-variable-unable-to-retrieve-data/#findComment-1566356 Share on other sites More sharing options...
Paola Posted April 29, 2019 Author Share Posted April 29, 2019 Thank you so much for your input. I was able to get this working ? Quote Link to comment https://forums.phpfreaks.com/topic/308651-php-undefined-variable-unable-to-retrieve-data/#findComment-1566367 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.