Question:
PHP: Echo Results on the Same Page?
n!ch0
2012-11-20 20:29:44 UTC
(BACKGROUND)
I have an intermediate knowledge of PHP, working with mySQL, creating methods and classes, etc. Like most people, I'm self taught, so certain things I can't grasp without an explanation, or without seeing an example written by someone else.

A few years ago I was working on a Content Management System. It took me 3 months. Everything worked very well, I had even created my own WYSIWYG Editor, but the actual coding was VERY "rookie". I'm currently re-writing my CMS uses Classes so everything is re-usable.

(QUESTION)
One thing I'm confused on is echo'ing a result to the same page. Can this be done in PHP? Or do I need to incorporate an additional language, such as AJAX?

(EXAMPLE)
Say I have a functions.php page that contains the following:

function displayResults() {
echo "hello world!";
}

and I have a link (for example) on index.php, that when clicked, echo's "displayResults()" to a div element named "results".

(ADDITIONAL EXAMPLE - IF THE FIRST ONE WASN'T CLEAR ENOUGH)
insert.php contains a submit button that when clicked, calls the page "actions.php". "actions.php" perfoms the action to insert the data to the database. On this same page is a method that echo's whether or not the action was successful. I want that "echo" to target a specific div element on "insert.php" and NOT take the user to a blank page with the echo'd results.

(MORE BLAH BLAH)
This is just a basic explanation of course. I've done similar things when I was first learning JavaScript 10 years ago, but since I working with a mySQL database, I don't want to have to switch back and forth between languages.

(HOW YOU CAN HELP ME)
If the code is too complex to explain, for whatever reason, please provide a pseudo code solution.
Thank you!
Four answers:
Samrat
2012-11-20 20:55:50 UTC
you can set a session variable inside your display results function and the redirect user to the insert.php page where you echo the variable in whichever div you want. For eg.



//actions.php

---database operations---

if(check if successful)

{

do something

}

else

{

displayResults();

$next_url = 'http://example.com/insert.php';

echo '';

}



-------------------------------------------

//functions.php

displayResults()

{

$_SESSION['error'] = 'Database not updated.';

}



/------------------------------------------

//insert.php

<----- form elements or etc -------->













A better way would be to have the form and the database operations in same page. So that

page will submit the form to itself and you will not need to redirect to other script. If the database

operation is successful, redirect to next form or next page and if not successful, then set the

session variable and remain on same page.



Hope this helps.
anonymous
2012-11-20 20:58:05 UTC
You need AJAX, which is a function in JavaScript, not a language. I would look into jQuery. It will make your coding life easier.
Rad7
2012-11-20 20:51:25 UTC
Try something like this:



index.php:















Some content...



Click to get results















====================================================================================

functions.php:






function displayResults() {

echo "hello world!";

}



displayResults();



?>
?
2016-02-24 03:10:18 UTC
Do you have a server up? if not, I recomment XAMPP.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...