JavaScript (with Ajax) from PHP and output buffering

Now I have a working JSON file from my PHP scripts.

The next step is to use a JavaScript script to retrieve this data for sorting, filtering and displaying.

I have a working Ajax script that checks for data disconnection well, but I need to personalize this for an individual person.

In PHP, I have a Session variable called MID (Member ID).

I am trying to use PHP to create JavaScript with a unique URL with MID as a variable.

The following seem to work, except for replacing the midValue variable in the JavaScript text with the MID variable in the external PHP script.

The code so far looks like this:

// This is a PHP file // Setup PHP Output Buffering to change the MID value session_start(); $MID = $_SESSION['MID']; function callback($buffer) { return (str_replace("midValue", $MID, $buffer)); } ob_start("callback"); /* Some bits I can't show as I haven't figured out the correct Stackoverflow tags (!) ... - Add the usual HTML tags such as `HTML, HEAD, TITLE, BODY, SCRIPT` etc - Include a DIV with an ID of **json**, this will be replaced by the JSON output it self. - Enclose the params variable with the `CDATA` tags to maintain the ampersand. */ params = "url=server.com/content.php?action=json&MID=" + midValue request = new ajaxRequest() request.open("POST", "getcontent.php", true) request.setRequestHeader("Content-type", "application/x-www-form-urlencoded") request.setRequestHeader("Content-length", params.length) request.setRequestHeader("Connection", "close") request.onreadystatechange = function() { if (this.readyState == 4) { if(this.status == 200) { if(this.responseText != null) { document.getElementById('json').innerHTML = this.responseText } else alert("Ajax Error: No data recieved") } else alert("Ajax Error: " + this.statusText) } } request.send(params) function ajaxRequest() { try { var request = new XMLHttpRequest() } catch(e1) { try { request = new ActiveXObject("Msxml2.XMLHTTP") } catch(e2) { try { request = new ActiveXObject("Microsoft.XMLHTTP") } catch(e3) { request = false } } } return request } /* Add the closing `SCRIPT, BODY and HTML` tags here. */ ob_end_flush(); 

And the getcontent.php file looks like this:

 if(isset($_POST['url'])) { echo file_get_contents("http://" . SanitizeString($_POST['url'])); } function SanitizeString($var) { $var = strip_tags($var); $var = htmlentities($var); return stripslashes($var); } 
+4
source share
1 answer

I think something simpler like this should work well for you.

 <?php session_start(); $MID = $_SESSION['MID']; ?> params = "url=server.com/content.php?action=json&MID=<?php echo $MID ?>" request = new ajaxRequest() request.open("POST", "getcontent.php", true) request.setRequestHeader("Content-type", "application/x-www-form-urlencoded") request.setRequestHeader("Content-length", params.length) request.setRequestHeader("Connection", "close") ... // rest of javascript <?php include 'footer.php'; // include footer code here ?> 

With this method, you simply output javascript and html outside of PHP, so you don't need it in tags. Then you can simply call up variables or include your header and footer where necessary.

+1
source

Source: https://habr.com/ru/post/1382748/


All Articles