简体   繁体   中英

How to get ajax success response on other PHP page?

I have php page.

On that page, there is dropdown to select cities.These cities are appearing from databases.

Next to that drop down.i have button to add more cities.On click of that button,a pop up(colorbox) appears in which i have form to add new city.

Now what i want is that when i add city in that pop up,that city must also appear in that dropdown of cities after closing that pop up box.

i tried to use ajax and that ajax gives me response.

Whenever i add a new city.That city is added and i got that city and others in that response.

But i can't send that success data to other PHP page.

How can i do that?

This is the functions that sends request to other page and get all cities(already added city and recently added) But i can send that response to other page where i want to populate that cities dropdown with this ajax call response.

function executeQuery() {
  $.ajax({
    url: '<?php echo WEB_URL; ?>pages/reload_fields.php?chk=vessel',
    success:function(result)
                        { 
                            $("<?php echo WEB_URL; ?>pages/add_report_entry.php#refreshPG").html(result);

                        }
  });
  setTimeout(executeQuery, 5000); // you could choose not to continue on failure...
}   

Why would you need to run an AJAX hook to populate a dropdown? Why not just call the function and loop through the results:

<select>
<?php
$cities = function_that_returns_cities();

foreach ($cities as $city) {
?>
  <option><?php echo $cities['name']; ?></option>
<?php } ?>
</select>

Something like this for example.

Multiple scenarios, but if you really want to reload just the select with Ajax...

  1. Put the part with that drop down that you want to refresh in a php file, reload that with Ajax on close of pop up $("#dropdown").load("newlist.php");

On the close event of the color box fetch the cities in json format using another ajax call and populate it in that select box.

$(".callbacks").colorbox({

onClosed: function() {
    $.ajax({
        url: 'getcities.php',
        type: 'POST',
        data: 'q=' + str,
        dataType: 'json',
        success: function(json) {
            $.each(json, function(i, value) {
                $('#myselect').append($('<option>').text(value).attr('value', value));
            });
        }
    });

}

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM