Can a button be processed on the same page as the button?
Moderator: General Moderators
Can a button be processed on the same page as the button?
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.
<?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?
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:
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?
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'?
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?
it could be ;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.
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>
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?
Well, almost, there is some trickery you can do client-side using javascript (and optionally Ajax), but to keep things clean and simple: yes.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?
For example: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'?
Code: Select all
$pageNr = $_GET['page'];
$keyword = $_GET['fi'];
print("You have chosen page $pageNr, using keyword $keyword");Re: Can a button be processed on the same page as the button?
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?
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.
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.
- 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?
That's because josh didn't post PHPJackD 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.
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.
Re: Can a button be processed on the same page as the button?
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.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.
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?
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?
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?
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.
Once you get your head around this, you'll be in good shape to continue.