Moorcam Posted February 14, 2017 Share Posted February 14, 2017 Hi all, Strange one. I have Google Maps Places API added to a text field for Autocomplete purposes. However, if I add the id="address" to the text field and save the data I get Undefined Index. Here is the text field: <div class="form-group"> <label><?php echo $lang_company_address; ?></label> <input type="text" class="form-control" id="address" name="company_address" value="<?php echo $row['company_address']; ?>"/> </div> Here is where I am getting the Undefined error: $company_address = mysqli_real_escape_string($mysqli, $_POST['company_address']); And here is the Google JS code: <script> function initMap(){ var autocomplete = new google.maps.places.Autocomplete($("#address")[0], {}); google.maps.event.addListener(autocomplete, 'place_changed', function() { var place = autocomplete.getPlace(); console.log(place.address_components); }); } </script> The script above works fine. Although I do get the dreaded Ooops Something went wrong error, which I presume is tied to the above somehow. The API key is called as below: <script src="https://maps.googleapis.com/maps/api/js?key=<?php echo $row['google_api']; ?>&libraries=places&callback=initMap" async defer></script> The key is stored in the database. Any ideas? Quote Link to comment Share on other sites More sharing options...
ginerjm Posted February 14, 2017 Share Posted February 14, 2017 You reference the index 'company_address' in two places. One as an element of $_POST and once as an element of what I assume is a query result. So - where does this ACTUALLY get defined? And which line is the problem? Do you have a query that looks for 'company_address'? Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted February 14, 2017 Share Posted February 14, 2017 and save the data I get Undefined Index. the problem is in the 'save the data' part of the process. all the posted information, except the one line of php code with the escape string call in it, is about the google map auto-fill api, which occurs before you submit the form. you haven't shown the form (is there a form? is the form field inside the form? does the form use method='post'?), how the form gets submitted (is the browser submitting the form or are you using ajax?), or enough of the form processing code (could the form processing code be running, but you are redirecting/re-requesting the page and the error you are seeing is from the second execution of the page?) in short, post the code that's relevant to the problematic part of the process, that would be needed to reproduce the problem, in order to get the quickest solution. Quote Link to comment Share on other sites More sharing options...
Moorcam Posted February 14, 2017 Author Share Posted February 14, 2017 Fixed. For some reason the Google API was affecting it. Basically, the URL was not authorized to use the API so once I did that, all errors disappeared. No idea how it caused an Undefined Index though. But all is good. Quote Link to comment Share on other sites More sharing options...
Solution gizmola Posted February 15, 2017 Solution Share Posted February 15, 2017 An undefined index occurs when you try and reference a key in an array that doesn't exist. Based on the info you provide that was the attempted referencing of "$_POST['company_address']". Clearly, prior to the api actually returning something, that variable didn't exist in the POST data. You can improve your error checking by using functions like array_key_exists or trying: if (isset($POST['some_key'])) { } else { // Deal with error, it's not going to work } Quote Link to comment Share on other sites More sharing options...
Moorcam Posted February 17, 2017 Author Share Posted February 17, 2017 Thanks gizmola, Much appreciated. Still learning. Quote Link to comment 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.