javascript - Receive ajax response in a website mvc -


so have 3 files (a view, consists of list.js , list.php, , controller consists of get_data.php file).

get_data.php:

<?php    $str_json = file_get_contents('php://input');    $data = json_decode($str_json, true);    $token = $data['token'];    $request = $data['request'];    echo "<p>" . $request . "</p>"; ?> 

list.js:

function getitems(list) {   var http = new xmlhttprequest();   var url = "../model/get_data.php";   var datapost = {"token" : true, "request": list};   var str_json = json.stringify(datapost);    http.open("post", url, true);    http.setrequestheader("content-type", "application/x-www-form-urlencoded");   http.setrequestheader("content-length", datapost.length);   http.setrequestheader("connection", "close");    http.onreadystatechange = function() {       if (http.readystate == 4 && http.status == 200) {           document.getelementbyid("show").innerhtml = http.responsetext;       }   };   http.send(str_json); }  window.onload = getitems("start"); 

list.php:

<some html code> <body>    <span id="show" ></span> </body> <some more code> 

i'm trying implement model-view-controller pattern, don't know how i'd transfer 'echo' inside 'get_data.php' file view.

note <span> tag in list.php file 1 showing changing text

i guess need "include" view controller via require_once() this:

get_data.php:

<?php     $str_json = file_get_contents('php://input');     $data = json_decode($str_json, true);     $token = $data['token'];     $request = $data['request'];     //echo "<p>" . $request . "</p>"; -- don't echo in controller     require_once('item.php'); ?> 

item.php:

<p>   <?php echo $request ?> </p> 

require_once() include file there scope doesn't change.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -