Page 1 of 1

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

Posted: Tue Dec 22, 2009 3:07 pm
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.

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

Posted: Tue Dec 22, 2009 3:16 pm
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.

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

Posted: Tue Dec 22, 2009 5:14 pm
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'?

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

Posted: Tue Dec 22, 2009 6:55 pm
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

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

Posted: Wed Dec 23, 2009 5:58 am
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.

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

Posted: Wed Dec 23, 2009 8:08 am
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
});
 

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

Posted: Wed Jan 06, 2010 3:38 pm
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.

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

Posted: Wed Jan 06, 2010 3:43 pm
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

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

Posted: Wed Jan 06, 2010 3:44 pm
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!

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

Posted: Wed Jan 06, 2010 5:00 pm
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?

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

Posted: Wed Jan 06, 2010 8:50 pm
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.