Hello.
There is a php script.

if (!empty($_SESSION['name'])) { $total_cost = 0; $total_count = 0; foreach ($_SESSION['price'] as $key => $out) { $total_cost = $total_cost + (int)$_SESSION['price'][$key] * (int)$_SESSION['count_prdct'][$key]; } foreach ($_SESSION['count_prdct'] as $key => $out) { $total_count = $total_count + (int)$_SESSION['count_prdct'][$key]; } $cart_alert = 'количество товара ' . (STRING)$total_count . ' на сумму ' . (STRING)$total_cost; echo $cart_alert; } echo 'asjdaoijsdaoisjdoaijsdaoidjs'; 

There is a div on the page.

 <div id="cart"> <a href="http://dev.1-11.ru/forma/cart.php"><font color="#FF0000">Корзина {$cart_alert}</font></a> </div> 

How to use the jquery / ajax to update the contents of this div, without reloading the page, outputting the php script.
Tried so

 $("#cart").load("/cart_update.php #cart"); 

But this command only makes the div empty.

  • what kind of address do you have? #cart why add? - Vasily Barbashev
  • From here I took maxua.com.ua/blog/php-script/jquery-job/reload-div.html - /cart_update.php- relative path. Tried and absolute insert. - votanko
  • duck you and leave only $("#cart").load('/cart_update.php') and see what the server returns to you in the Network tab. You just need to write data to the block - Vasily Barbashev
  • And then you are trying to find the #cart block in the returned result and insert it into your block - Vasily Barbashev
  • Oh, thanks a lot - it worked. - votanko

1 answer 1

To do this, use AJAX - requests.

The code I use for this purpose is:

 function send_request(r_method, r_path, r_args, r_handler) { var Request = false; if(window.XMLHttpRequest) { Request=new XMLHttpRequest(); } else if (window.ActiveXObject) { try { Request = new ActiveXObject("Microsoft.XMLHTTP") }; catch(CatchException) { Request=new ActiveXObject("Msxml2.XMLHTTP"); } } if(!Request) { return; } Request.onreadystatechange=function() { if(Request.readyState==4) if(Request.status==200) r_handler(Request.responseText); if(r_method.toLowerCase() =="get"&&r_args.length>0) r_path+="?"+r_args; Request.open(r_method,r_path,true); if(r_method.toLowerCase()=="post") { Request.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8"); Request.send(r_args) } else { Request.send(null) } } } 

Briefly about the arguments:

 send_request("Тип запроса. get либо post","Путь/к/файлу.php","Аргументы. Например: topic=46764&page=4",function(response){ //Код, который будет исполняться при успешном окончании запроса //response - переменная, содержащая ответ }) 

A small example:

test.html

 <html> <head> <script> function send_request(r_method, r_path, r_args, r_handler) { var Request = false; if(window.XMLHttpRequest) { Request=new XMLHttpRequest(); } else if (window.ActiveXObject) { try { Request = new ActiveXObject("Microsoft.XMLHTTP") }catch(CatchException) { Request=new ActiveXObject("Msxml2.XMLHTTP"); } } if(!Request) { return; } Request.onreadystatechange=function() { if(Request.readyState==4) if(Request.status==200) r_handler(Request.responseText); if(r_method.toLowerCase() =="get"&&r_args.length>0) r_path+="?"+r_args; Request.open(r_method,r_path,true); if(r_method.toLowerCase()=="post") { Request.setRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8"); Request.send(r_args) } else { Request.send(null) } } } function request() { send_request("GET", "test.php", "testval=test", function(response) { document.body.innerHTML+=response }) } </script> </head> <body> <button onclick="request()">Загрузить!</button> </body> </html> 

test.php

 <?PHP echo "Тестовая переменная: ".$_GET['testvar']; ?> 
  • why use a homemade bike. if the author is already using jquery? - Grundy
  • I would not use jquery. Very much this bloated framework. - Pretzel
  • 2
    This is a library, and now you can assemble the parts you need. In any case, if jquery is already being used in the project, adding a samopis request to it, instead of using the finished one, is not always correct and in most cases inconvenient. - Grundy
  • @grundy, Whom you like best. I usually do not use - Pretzel