SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question 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?

  2. #2
    Non-Member
    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


  3. #3
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    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

    [img]images/smilies/biggrin.gif[/img]
    Do you think you could show me a code example of this? Thanks.

  4. #4
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mshafrir
    How do I accomplish this without having to load a new page or reload the current page
    Can't be done, as far as I know.

    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 .

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  5. #5
    Non-Member
    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
     
    }
    Works yes ? That'll give you some inspiration I hope...

  6. #6
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    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
     
    }
    Works yes ? That'll give you some inspiration I hope...
    Yes indeed, that will work, Php will spit out anything , but not...
    Quote Originally Posted by mshafrir
    ...when the submit button of that form is pushed.
    It's not possible to use Php (or any other server side language) for 'user interaction' without (re)loading a page.

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  7. #7
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    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>
    There are also other ways to submit data to PHP without reloading the page, using PHP to serve an image, and the like... but, umm... yeah [img]images/smilies/wink.gif[/img]

    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.

  8. #8
    Ceci n'est pas Zoef Zoef's Avatar
    Join Date
    Nov 2002
    Location
    Malta
    Posts
    1,111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cyborg from dh
    You think in too small terms [img]images/smilies/tongue.gif[/img]

    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 !

    Rik
    English tea - Italian coffee - Maltese wine - Belgian beer - French Cognac

  9. #9
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    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.

  10. #10
    Non-Member
    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....


  11. #11
    SitePoint Zealot dbworm's Avatar
    Join Date
    Apr 2003
    Location
    Lausanne
    Posts
    115
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mshafrir
    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
    >
    [font=verdana, arial, helvetica][size=2]
    Wouldn't this work (untested)?
    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();
    ?>
    Yes the page will be reloaded but this is super fast. I don't see any other way.

  12. #12
    Free your mind Toly's Avatar
    Join Date
    Sep 2001
    Location
    Panama
    Posts
    2,182
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •