Making two ajax requests after a page load but only once
I have two ajax requests that i need to make that will return two elements of data and i want to only grab them once as the page loads
I was thinking of this approach but i was wondering is there a better
$(document).ready(function(){
$.开发者_开发问答get("/shop_possystems/index.php?route=module/cart/get_subtotal",
function(data) {
});
$.get("/shop_possystems/index.php?route=module/cart/get_tax",
function(data) {
});
});
the problem is how do i get this data to my php page so another thing i was thinking was along these lines
<script type="text/javascript">
$.get("/shop_possystems/index.php?route=module/cart/get_subtotal",
function(data) {
<?php $subtotal = data; ?>
});
$.get("/shop_possystems/index.php?route=module/cart/get_tax",
function(data) {
<?php $tax = data; ?>
});
</script>
as you can see i am trying to add a script tag in the php page and trying to set the values here....both ways seem a bit hacky, not to mention the second approach is probably not even correct syntax....any suggestions
how do i get this data to my php page so another thing
Your request to index.php
returns the data. So index.php
already has the data in the page. Simply have that page deal with the data how it needs to.
The only other way to send data from js to PHP is a $.post
request.
There's nothing wrong with doing this to fire two get requests.
$(function() {
$.get(..);
$.get(..);
});
You can also just get the PHP to get that information when you do your initial page load request and just inject the data directly into the HTML.
Your first approach will make the requests on page load.
After the page is loaded, you cannot do anything with PHP on the page. PHP is a server side scripting language and as far as the browser is concerned, it's just plain text. If you need to use the result of the calls on the server, then you'll need to either POST it back to the server or fetch the content in the PHP script that is rendering the current page (instead of using ajax).
精彩评论