Output messages from PHP page

I am struggling to output a simple message ( like an Alert or MsgBox ) from a PHP page.

I have Page#1 which is html and it calls Page#2 which is pure PHP ( doing some MySql work but that is not relevant ).
When I need to send a message to the screen, I cannot get anything like echo or printf to work.

Yes, I realize that the html is client-side and the php is server-side and therein is the problem.

Is there a magical piece of code to do this??

Thanks

Please add the code you’re using (html and php)

Sorry, too much of the working code is proprietary & can’t be provided.
I can say that Page #2 is called by Page #1 via an Ajax call which is working just fine.

Thanks

Shouldn’t be a problem with this at all, it’s how most sites work today.

JS calls server side
Server side echo out response
Response is now available in JS and JS can write it to the document.

[hr]

So you need to figure out

  1. Does JS get any response from the server? (Check browser dev console)

  2. Does your ajax function properly receive the data and is it available afterwards? Server responses in JSON are perfect for this.

  3. How you can make js display the data properly

That is not really my scenario. Here is a typical one:

My server-side php code determines that one or more of the user’s input fields have improper values.
(these values were passed via the Ajax call )
I need to somehow post a message box back to the user to indicate that field X and Y are invalid.
( this can’t be known on the client-side)
That would be a typical situation.
Thanks

I fail to see how that is different

  1. JS calls a PHP script
  2. The PHP script executes some code and echoes out a response
  3. JS (beeing the “browser” of the request) receives this response
  4. JS then displays any errors it received

I sure wish it was that simple.

If I put

echo “Hello world!”;

in the middle of my php code and when it gets to that point, absolutely nothing happens or shows up on my screen.

I have tried echo and printf countless times with Zero luck.
What am missing? I cannot make this any simpler.

When PHP called from JS echoes something it doesn’t echo to your screen, it echoes to the response JS gets from the request.

Open your browsers dev console, you will see the JS request the PHP file, and you can see the response there.

Do you use jquery? I can throw together an example for you

Here’s an example without jquery.

index.html[php]

[/php]

getdata.php[php]<?php

echo ‘Some data from the server side’;[/php]

as you can see, JS executes request to server side, get’s a response, and writes it to the document. You can’t skip that step and expect the server side to write to the users browser.

JimL, Thank you for your time on this.

I finally discovered a solution.

  1. What I did was embed the error message I was getting from MySQL into the xmlhttp.responseText as piece of XML string.
  2. Then that was sent back to Page#1 with echo $xml as if it was my real data being returned which has always worked.
  3. Once back into Page#1, I parsed the responseText for and then displayed this in the Alert box.

Very ugly solution but it works.
I wish there was a simple Function to just do all of that.
Thanks

If you throw together a code example of what you have now I could have a look. I have a hard time understanding exactly how you have all this layed out.

Sponsor our Newsletter | Privacy Policy | Terms of Service