0

I have a question, there is a way to update $error when his value change on external listPagPrinc.php?

<div id="statoPag">
    <h3> Stato : <?php echo $error; ?> </h3>
</div>

<div class="headerCont">
    <?php
        include('procedure/listPagPrinc.php');
    ?>
</div>
3
  • 2
    No, that is not possible, but still there might be solutions: you can inlude() that file earlier and save its content in a buffer, then fill your view later with that variable and the buffered content. We would need more code to help more. Commented Sep 9, 2015 at 17:36
  • You can't do this using PHP. try to use Some Javascript, Ajax, maybe the server send can help you. w3schools.com/html/html5_serversentevents.asp Commented Sep 9, 2015 at 17:37
  • Agree with @arkascha; however if you consider changing file contents see php.net/manual/en/function.file-put-contents.php Commented Sep 9, 2015 at 18:43

2 Answers 2

1

Not as-is, no. Think of these HTML/PHP files like an office printer, once it prints out each line, you can't "go back" and print over it

In this example, all of the first 5 lines are run and effectively "set in stone" before anything is called in procedure/listPagPrinc.php.

If, and this is just speculation, you can't simply include procedure/listPagPrinc.php before you render $error because it also is printing additional HTML, you just need to encapsulate its code in functions as best as possible: One to set the value of $error, and a separate one to output the HTML you need.

Sign up to request clarification or add additional context in comments.

2 Comments

You may want to take a look at "output buffering".
I was oversimplifying to avoid confusing the situation - I am well aware of output buffering, I just don't think it's particularly helpful here. It seems more likely that any form of control flow (functions or classes) would be a better solution in this specific instance.
1

You need to update the text content of the tag; you can do this with e.g. jQuery at runtime. This is the preferred way if some tags, and only those, change during the lifetime of the application page, and you do not wish to reload the whole page from scratch.

In this case, from listPagPrinc.php, you can output some Javascript code:

echo <<<JAVA1
<script>
    alert("Ciao, mondo");
</script>
JAVA2;

or in your case using jQuery

echo <<<JAVA2
<script>
    $('#statoPag h3').text("Errore!");
</script>
JAVA2;

Very likely the call will need to be inside a jQuery onDocumentReady function to be sure that it executes.

A better and faster way (and as @arkascha observed, nicer and more robust): you can generate the header from listpagPrinc.php or from a wrapper.

// file listPagPrincWrapper.php, replace your current file

// Ideally listPagPrinc could return a text value. In case it is
// printing it, as seems likely, we capture the output. This way
// we don't neet to modify the existing code.

ob_start();
include('procedure/listPagPrinc.php');
$lpp = ob_get_clean();

// At the end, we do the output part.
print <<<HTML
<div id="statoPag">
    <h3> Stato : {$error}</h3>
</div>
<div class="headerCont">{$lpp}</div>
HTML;

2 Comments

What an ugly and risky approach. What if javascript is disabled on the client side?
@arkascha , you raise a good point. I had hinted that there was a better way; I have now amended my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.