Jump to content

Undefined Index if ID added to text field


Moorcam
Go to solution Solved by gizmola,

Recommended Posts

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?

Link to comment
Share on other sites

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'?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • Solution

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
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.