showing results in the same page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cali_dotcom
Forum Commoner
Posts: 49
Joined: Fri Aug 22, 2008 7:28 pm
Location: Rancho Cucamonga, CA

showing results in the same page

Post by cali_dotcom »

hi,
i'm a newbie programmer and i just built a website with a poll. the page is completely in html and linked to the php script processing the poll result in the action attribute of the form tag. my problem is the results of the poll comes up in a new page when the user hits the submit button, but i need the results of the poll to appear just under the poll questions on the same page. can anybody help me with that?
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: showing results in the same page

Post by it2051229 »

yes you can on the form tag....

Code: Select all

 
<form action='<?php echo $_SERVER["PHP_SELF"]?>'>
 
this will call the same page.. and whatever value is going to be passed will also be on the same page.. but remember computer starts processing codes from TOP to BOTTOM.. so i suggest you process the passed variables before displaying any HTML pages.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: showing results in the same page

Post by califdon »

cali_dotcom wrote:hi,
i'm a newbie programmer and i just built a website with a poll. the page is completely in html and linked to the php script processing the poll result in the action attribute of the form tag. my problem is the results of the poll comes up in a new page when the user hits the submit button, but i need the results of the poll to appear just under the poll questions on the same page. can anybody help me with that?
You can do that using AJAX, but not by just calling a PHP script in the usual manner. AJAX is the use of Javascript (therefore, it can respond to a user's actions) to call a PHP script in asynchronous mode, which will then return only data, not a whole new page to the browser. The Javascript must include a function that "listens" for the returned data and (usually) replaces the innerHtml property of some HTML element. It's not that hard to do, but the code is a little confusing at first. Check out:
http://www.w3schools.com/Ajax/Default.Asp
http://www.xul.fr/en-xml-ajax.html
http://www.tizag.com/ajaxTutorial/
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: showing results in the same page

Post by it2051229 »

oh wtf? we're talking about ajax here? cause you said you're newbie lol.. ajax isn't for newbs but yeah you can start learning it if you have strong foundation with javascripting and phps
User avatar
The_Anomaly
Forum Contributor
Posts: 196
Joined: Fri Aug 08, 2008 4:56 pm
Location: Tirana, Albania

Re: showing results in the same page

Post by The_Anomaly »

califdon wrote:
cali_dotcom wrote:hi,
i'm a newbie programmer and i just built a website with a poll. the page is completely in html and linked to the php script processing the poll result in the action attribute of the form tag. my problem is the results of the poll comes up in a new page when the user hits the submit button, but i need the results of the poll to appear just under the poll questions on the same page. can anybody help me with that?
You can do that using AJAX, but not by just calling a PHP script in the usual manner. AJAX is the use of Javascript (therefore, it can respond to a user's actions) to call a PHP script in asynchronous mode, which will then return only data, not a whole new page to the browser. The Javascript must include a function that "listens" for the returned data and (usually) replaces the innerHtml property of some HTML element. It's not that hard to do, but the code is a little confusing at first. Check out:
http://www.w3schools.com/Ajax/Default.Asp
http://www.xul.fr/en-xml-ajax.html
http://www.tizag.com/ajaxTutorial/
This javascript stuff is probably the best way to do it, but not necessary if you don't need/want asynchronous pages. It's probably worth looking into, just because of how professional and seamless it is, but not necessary.

You could also do this by using a conditional statement based on whether the user submitted a form. i.e:

Code: Select all

 
<?php
if(isset($_POST['Submit']){
   displayResults();
}
?>
 
Of course, the page would have to reload, but it would be simpler.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: showing results in the same page

Post by califdon »

The OP said that he wants to avoid a second page load. That's why it involves AJAX.
Post Reply