Can a button be processed on the same page as the button?

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
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Can a button be processed on the same page as the button?

Post by JackD »

I want to place a button on a page and have it processed on that same page. Is that possible? Here is the general idea:

<?php
function show_page_number($pg)
{ echo "Page Number is: " . $pg; }
?>

<form action='<?php show_page_number(13) ?>' method="post" >
<p><input type="submit" value="Show Page" /></p>
</form>

Unfortunately, if I just do the above, it tries to take me to "Page Number is: 13" with the 404 error:
The requested URL /Page Number is: 13 was not found on this server.
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Can a button be processed on the same page as the button?

Post by Apollo »

You can only put a normal URL in the form's action.
Remember: php is processed server-side, submitting a form is something that happens client-side.

Instead, in your PHP you should:
  • not specify an action at all (so the form will be submitted to the same PHP file)
  • check if a page number has been entered, if yes: show the appropriate page, if not, show the form.
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Re: Can a button be processed on the same page as the button?

Post by JackD »

Thanks. That almost clears it up.

So, basically, anything I do in terms of form input is going to invoke server side action to either reload the same page or another page?

I have an existing page I can work from that sends a link like:

mywebsite/mypage?page=2?fi=sam

Is there an easy way to process that to retrieve the page number and the keyword 'sam'?
imran.rajani
Forum Newbie
Posts: 16
Joined: Tue Dec 22, 2009 5:34 pm

Re: Can a button be processed on the same page as the button?

Post by imran.rajani »

JackD wrote:I want to place a button on a page and have it processed on that same page. Is that possible? Here is the general idea:

<?php
function show_page_number($pg)
{ echo "Page Number is: " . $pg; }
?>

<form action='<?php show_page_number(13) ?>' method="post" >
<p><input type="submit" value="Show Page" /></p>
</form>

Unfortunately, if I just do the above, it tries to take me to "Page Number is: 13" with the 404 error:
The requested URL /Page Number is: 13 was not found on this server.
it could be ;

Code: Select all

 
<?php
if($_POST['btnSubmit']=="Show Page") {
echo "Page Number is: " . $pg; }
?>
 
<form action="" method="post" >
<p><input type="submit" name = "btnSubmit" value="Show Page" /></p>
</form> 
 
 
i've changed ;
1) form action
2) inserted name of input submit
3) in php removed function and inserted if condition
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Can a button be processed on the same page as the button?

Post by Apollo »

JackD wrote:Thanks. That almost clears it up.

So, basically, anything I do in terms of form input is going to invoke server side action to either reload the same page or another page?
Well, almost, there is some trickery you can do client-side using javascript (and optionally Ajax), but to keep things clean and simple: yes.
I have an existing page I can work from that sends a link like:

mywebsite/mypage?page=2?fi=sam

Is there an easy way to process that to retrieve the page number and the keyword 'sam'?
For example:

Code: Select all

$pageNr = $_GET['page'];
$keyword = $_GET['fi'];
print("You have chosen page $pageNr, using keyword $keyword");
But if you use method='post' in your form, the ?page=2&fi=sam won't be added to the URL, and you can get the input using $_POST instead of $_GET.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Can a button be processed on the same page as the button?

Post by josh »

AJAX? http://www.jquery.com

Code: Select all

 
$('.button').click( function() {
 // send your data
 // show response to user
 return false; // dont submit the form
});
 
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Re: Can a button be processed on the same page as the button?

Post by JackD »

Josh,
Thanks for your reply... however, I do not understand it. I tried it as you entered it, but got an error:

Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$' in /home/content/13/5169913/html/detail.php on line 203

which was the first line. It appears that if what I could get what you posted to work, it would do what I wanted.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Can a button be processed on the same page as the button?

Post by AbraCadaver »

JackD wrote:Josh,
Thanks for your reply... however, I do not understand it. I tried it as you entered it, but got an error:

Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$' in /home/content/13/5169913/html/detail.php on line 203

which was the first line. It appears that if what I could get what you posted to work, it would do what I wanted.
That's because josh didn't post PHP 8O
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Re: Can a button be processed on the same page as the button?

Post by jason »

JackD wrote:Josh,
Thanks for your reply... however, I do not understand it. I tried it as you entered it, but got an error:

Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$' in /home/content/13/5169913/html/detail.php on line 203

which was the first line. It appears that if what I could get what you posted to work, it would do what I wanted.
Well, no. First, he posted JavaScript, not PHP. Secondly, it's very much incomplete. But it's still the answer you'd want, as it gives you an idea of what you'd want to do.

Basically, you want a button that, when clicked, submits the form to a PHP script, but you don't want the user leaving the page, or even having the page reload.

This is possible. You need to add JavaScript to your toolset though to do it. I'd recommend using a library like jQuery. It's the perfect shotgun to shoot your foot off within the wonderful world of Ajax.

http://www.jquery.com
http://docs.jquery.com/Tutorials

Also, for a tutorial covering exactly what you are looking for:

http://net.tutsplus.com/tutorials/javas ... ng-jquery/

Happy coding!
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Re: Can a button be processed on the same page as the button?

Post by JackD »

Thanks, Jason.

I haven't gotten into JavaScript yet, but it, like PHP, looks a lot like C which I have used for years. I am using DreamWeaver CS4 so I believe I can just jump in with both feet. My problem so far has been that I have been building dynamic pages that have no structure until I build them with php (echo "<table><tr>....). I have found I can spit out whatever html I need on the fly and the server sends the right stuff to the browser.

Can I do that with JavaScript as well? If I use php to echo or print JavaScript on the fly will that work dynamically as well?
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Re: Can a button be processed on the same page as the button?

Post by jason »

Yes, you can output JavaScript from PHP. JavaScript lives in the same place that HTML does. jQuery also makes it rather easy to work with JavaScript. As I said, it's the perfect shotgun. =) Really what you need to understand is where PHP lives and works (server side), and where HTML and JavaScript play (Client Side, or, in the browser). By the time HTML and JavaScript are doing their thing, PHP is finished.

Once you get your head around this, you'll be in good shape to continue.
Post Reply