SitePoint Sponsor |
|
User Tag List
Results 1 to 12 of 12
-
May 27, 2003, 13:08 #1
- Join Date
- Jan 2003
- Posts
- 18
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Call a PHP function using a form/Javascript?
I have a page, index.php, with a form on it. I want to run a function, let's call it run_code() (declared in index.php)when the submit button of that form is pushed. How do I accomplish this without having to load a new page or reload the current page?
<form action="index.php" method=post name="process">
<input type="submit" value="Go">
</form>
Should I put a Javascript event handler in the form?
-
May 27, 2003, 13:57 #2
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I suppose you put the event handler in the FORM and call a Javascript function which has PHP embedded within the Javascript function;
The PHP in question would be parsed so to output what Javascript variables, etc you'd need w/in the function your going to call
-
May 27, 2003, 15:09 #3
- Join Date
- Jan 2003
- Posts
- 18
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Dr Livingston
-
May 27, 2003, 15:28 #4
- Join Date
- Nov 2002
- Location
- Malta
- Posts
- 1,111
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by mshafrir
You need to understand the difference between server side scripting (like Php) and client side scripting (like Javascript).
This is, briefly, how it works:- The client(your browser) requests a page.
- The server finds this page, does the server side scripting (if any) on it.
- The server then sends the outcome of this to your client.
- The server has finished it's job, it couldn't give a hoot about your page anymore!
- The client in the mean time receives the page, displays it and executes any client side scripting (if any) as triggered by user interaction or automatically (onload event).
Hope it helped.
RikEnglish tea - Italian coffee - Maltese wine - Belgian beer - French Cognac
-
May 27, 2003, 15:36 #5
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Ummm....
Zoef -
The way I was thinking is that the PHP embedded within the Javascript function would be parsed by the server before being sent to the client yes ?
If so, then yes, whatever your PHP puts in that Javascript function would be seen by the client okay so far ?
Here is a short function from Javascript that I use to re-direct users in the event that I can't use PHPs Header(...); function:
PHP Code:function redirectJS($url) {
?>
<script language="javascript1.2" type="text/javascript">
window.location = "<?php echo($url); ?>";
</script>
<?php
}
-
May 27, 2003, 15:57 #6
- Join Date
- Nov 2002
- Location
- Malta
- Posts
- 1,111
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Dr Livingston
, but not...
Originally Posted by mshafrir
RikEnglish tea - Italian coffee - Maltese wine - Belgian beer - French Cognac
-
May 27, 2003, 17:05 #7
- Join Date
- Jan 2003
- Location
- Calgary, Canada
- Posts
- 2,063
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You think in too small terms [img]images/smilies/tongue.gif[/img]
Code:<script type="text/javascript"> function doSomething() { alert('<?php echo str_replace("'", "\\'", $usermessage; ?>'); } </script> <form action="somefile.php" onsubmit="doSomething()" method="post"> <input type="submit" value="Click here" /> </form>
You could also have an invisible Iframe, which you could reload a page in, and get the data out from PHP with, and the user wouldn't even really know the page was reloaded.
(I use the image one a lot)Who walks the stairs without a care
It shoots so high in the sky.
Bounce up and down just like a clown.
Everyone knows its Slinky.
-
May 28, 2003, 05:58 #8
- Join Date
- Nov 2002
- Location
- Malta
- Posts
- 1,111
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by cyborg from dh
I was actually thinking in the terms as put in the question.
As I understand it:
"Can I run a Php function (I'm assuming any function) by clicking the submit button of a form without reloading the page?
So I'd say the image thingy doesn't cover that, though its a very handy little tool to have in one's box.
As for the I-frames solution, ok, might work, just read 'page or frame' were it says 'page' in the above posts.
But personally I don't like to hide things from the user. Say I click on a button that triggers an (hidden) server call, what happens in the mean time on my page? Nothing... Not untill the information comes back from the server. In the mean time I'm not getting any feedback at all. That's not a good model for interaction if you ask me.
And one could also argue that there's no principal difference in reloading a page or reloading a frame (be it hidden or not).
There's a reason that both sever side and client side scripting exist, they are there to do different things. I'd say, use the right tool for the right job.
Anyhow, do feel free to disagree or prove me wrong. Hey, I might learn something new !
RikEnglish tea - Italian coffee - Maltese wine - Belgian beer - French Cognac
-
May 28, 2003, 07:08 #9
- Join Date
- Jan 2003
- Location
- Calgary, Canada
- Posts
- 2,063
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Well, if you reload an image and access it using a PHP script you then have access to every PHP function within that script.
As for the hidden iframe, you only send like a maximum 10 lines of code, because you definitly want it to be instant...
In most cases, it really depends on what the situation is. I never write a page to depend on Javascript, however, if Javascript is there, I make some of the links happen without reloading the page, and then providing a pop-up alert() once the operation has completed.
There is no real set answer for this, i believe you have to make your own call on if/when you want to do something like this.Who walks the stairs without a care
It shoots so high in the sky.
Bounce up and down just like a clown.
Everyone knows its Slinky.
-
May 28, 2003, 07:38 #10
- Join Date
- Jan 2003
- Posts
- 5,748
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Zoef - Wasn't paying attention I think ? But your correct on this matter....
But I agree that you could use an IMG or IFRAME to solve this problem; or as some folk would say; use FLASH which IMO is way over the top for something like this
Beside, FLASH isn't a solution - it's a plugin and nothing more....
-
May 30, 2003, 06:04 #11
- Join Date
- Apr 2003
- Location
- Lausanne
- Posts
- 115
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by mshafrir
PHP Code:<form action="<?php $_SERVER['PHP_SELF']; ?>" method=post name="process">
<input type="hidden" name="doit" value=true />
<input type="submit" value="Go">
</form>
// and, at the bottom of your page:
<?php
if ($_POST['doit']) run_code();
?>
-
May 30, 2003, 06:24 #12
- Join Date
- Sep 2001
- Location
- Panama
- Posts
- 2,181
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
For me that would be the best way... simple and fast but he doesn't want the page to reload for some reason.
Community Guidelines | Community FAQ
"He that is kind is free, though he is a slave;
he that is evil is a slave, though he be a king." - St. Augustine
Bookmarks