How to Clear Previously Echoed Items in PHP

How to clear previously echoed items in PHP

<?php

ob_start();
echo 'a';
print 'b';

// some statement that removes all printed/echoed items
ob_end_clean();

echo 'c';

// the final output is equal to 'c', not 'abc'

?>

Output buffering functions

The output buffering functions are also useful in hackery to coerce functions that only print to return strings, ie.

<?php
ob_start();
var_dump($myVar);
$data = ob_get_clean();
// do whatever with $data
?>

How to clear previous output and echo in php

use substr()

var xhttp = new XMLHttpRequest();
var lastResponse = '';
xhttp.onreadystatechange = function() {
//console.log(xhttp.readyState+" "+xhttp.status);
if (xhttp.readyState === 3 && xhttp.status ===200) {

var currentAnswer = xhttp.responseText.substr(lastResponse.length);
lastResponse = xhttp.responseText;
console.log(currentAnswer+"\n");

}
};
xhttp.open("GET", "ag.php", true);
xhttp.send();

Clear echoed items PHP

No, whatever you write out is send on to the webserver, which sends it along to the browser. There is however a module in PHP called output buffering which decouples the output stream (temporarily).

Look into ob_start() and ob_end_clean()

<?PHP

ob_start(); // output buffering enabled

// these echos will be buffered in memory, instead of written out as they usually would:
echo 'A';
echo 'B';
echo 'C';

// now we've moved the buffer into `$html` (so that now contains 'ABC'), and we've stopped output buffering.
$html = ob_get_clean();

echo 'D'; // this is being send to the client/webbrowser as usual

echo $html; // now we print the ABC we intercepted earlier

So the client will receive : D A B C

how would i clear previously echoed result without affecting any other functions in php?

See this code:

echo 'world';
echo 'hello !';

You can intercept the echo using ob_start(), ob_get_contents() and ob_clean().

ob_start();
echo 'world';

var $echoed = ob_get_contents();
ob_clean();

// real echo
echo 'hello ' . $echoed . '!';

// now you see
// hello world!

Because the ob 'output buffering' is native to PHP, you can use it with anything like functions, includes and so on. I'm using this approach, to intercept (1.) outputs in my controller flow, and to intercept (2.) the view's output, so I can compose them later (for example to render PHP errors into a debug div.

How to clear/remove/edit echo in PHP

You will need both server side (PHP) and client side (javascript) to achieve this goal.

Server side: only echo once, discard the while loop;

Client side: call server side (ajax or refresh the whole page) every n second to get the latest data.

Example:

(1) Create a new php file "nowStat.php", and move your db query into this file, like:

$result = $conn->query("SELECT id FROM gamecode WHERE gamecode = '$gameCode'"); 
$players = $result->num_rows;
echo "$players Players";

(2) In your createcode.php file, define an empty <h4> or <div> with an id:

<h4 id="playerStatus"></h4>

Then in the same file use jQuery/Ajax to retrieve the status every 5 sec:

setInterval(function() { $("#playerStatus").load("nowStat.php"); }, 5000); 

Here is a very simple tutorial about jQuery/Ajax: https://www.w3schools.com/jquery/jquery_ajax_intro.asp

how do you delete a previous echo / form PHP

Your code has a form so the page refreshes and clears everytime you click a button and this question has no sense. Anyway, if thats what you need, one way around can be to use jquery to clear. You can give the div an id and use simple jquery to clear everything inside the element. You can echo the jquery code or you can create a function to call when you need with an echo.
There is an example of the code.

<script>
function cleardiv(){
$("#div").html("");
}
</script>
<div class="content">
<div id="myForm" class="container-fluid">
<form id="myForm" method="POST">
<p class="Titel">Controles opvragen:</p>
<div class="form-group">
<div class="form-group">
<button name="Controle1" type="submit" class="btn btn-primary" value="1">Controle 1</button>
<button name="Controle2" type="submit" class="btn btn-primary" value="2">Controle 2</button>
<button name="Controle3" type="submit" class="btn btn-primary" value="3">Controle 3</button>
<button name="Controle4" type="submit" class="btn btn-primary" value="4">Controle 4</button>
<button name="Controle5" type="submit" class="btn btn-primary" value="5">Controle 5</button>
</div>
<div id="div">
<?php
echo "<script> cleardiv() </script>";
$uri = $_SERVER['REQUEST_URI'];
if (isset($_POST['Controle1']))
{

$checkpointID = $_POST['Controle1'];
FormBuilder($checkpointID) ;
}
if (isset($_POST['Controle2']))
{

$checkpointID = $_POST['Controle2'];
FormBuilder($checkpointID);
}
if (isset($_POST['Controle3']))
{
$checkpointID = $_POST['Controle3'];
FormBuilder($checkpointID);
}
if (isset($_POST['Controle4']))
{
$checkpointID = $_POST['Controle4'];
FormBuilder($checkpointID);
}
if (isset($_POST['Controle5']))
{
$checkpointID = $_POST['Controle5'];
FormBuilder($checkpointID);
}
if (isset($_POST['Controle6']))
{
$checkpointID = $_POST['Controle6'];
FormBuilder($checkpointID);
}
?>
</div>
</div>
</form>
</div>

I know that probably this is not the right way to do it but it can be an option.

Delete everything that has been echoed

You could use output buffering:

<?php
ob_start();
$var = true;
echo "testing";
if($var) {
ob_clean();
echo "Hey";
}

PHP Echo information and wait to remove it

If data has been sent to browser, php can't impact it. My solution is when you show your input, you attached a javascript function. okay, i thinks this code will help you

<?php 
function flush_buffers(){
//This function make buffer send with out some problem
ob_end_flush();
ob_flush();
flush();
ob_start();
}
ob_start();
$title = "<span class='mytitle'>My Title</span>";
echo $title; //You send your information
flush_buffers();

$input = "<input type='text'>"
$js_code = "<script type='text/javascript'>$('.mytitle').remove()</script>";//with jQuery :)
echo $input.$js_code;
flush_buffers();
?>


Related Topics



Leave a reply



Submit