Calling Functions from onClick
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Calling Functions from onClick

  1. #1
    Junior Member
    Join Date
    Jan 2006
    Posts
    7

    Calling Functions from onClick

    Hi All

    I am trying to call a php function from the onclick event of a button.

    I am used the following code, but keep getting the error.

    Error: '$SummitNo is not defined. Do you wish to debug.

    I have found this is very different from my other php errors as a dialog box is displayed with yes/no

    here is my code

    <?php
    include ('book_sc_fns.php');
    $SummitNo = "189";
    echo $SummitNo;
    echo '<form>';
    echo '<input type="button" value="Click me!" onclick="display_fell_details($SummitNo)" >';
    echo '</form>';

    ?>


    the function display_fell_details is working fine as I have used it on numerous occassions.

    Hope someone can help

    Jamie

  2. #2
    Senior Member
    Join Date
    May 2003
    Location
    Novi Sad, Serbia
    Posts
    576

    If I'm not mistaken...

    You're trying to call a function defined in PHP with an JS event i.e. click of a mouse...

    It can't work that way because PHP is being executed on a server side and not on a client side like JS does.
    In order to PHP function to work you have to send request to a server where PHP is being executed and then it will go through your code. OnClick event doesn't send anything to the server (well, most of JS functions don't) and you're stuck in there.
    Try creating a submit button and in form action define a PHP page to handle the request and then you'll be able to use the PHP function you created.

    You might want to read this to understand this issue more.


    And the error you're getting is a JS one because JS cannot use PHP variables in that way.
    I know that I don't know nothing...

  3. #3
    Senior Member wilku's Avatar
    Join Date
    Oct 2003
    Location
    Warsaw, Poland
    Posts
    723
    If display_fell_details is JavaScript function and it works with display_fell_details(189), you may want to improve your script. The single quotes mean $varibles are not transformed into their values. Try to change line with echo to:
    PHP Code:
    echo '<input type="button" value="Click me!" onclick="display_fell_details('.$SummitNo.')" >'
    Wilku <><

  4. #4
    Junior Member
    Join Date
    Jan 2006
    Posts
    7
    No that did not seem to work.

    Does anybody know of workarounds, without actually moving to another page.

    the display_fell_details function is actually embedded in a dreamweaver layer on the same page.

    the button that is clicked is generated as part of a list in a foreach loop and given the variable $SummitNo

    the when the button is clicked the layer will display the correct details for that record on the same screen whilst maintainning the button list.

    Thansk for yuour help
    Last edited by NotOnUrNelly; 01-13-2006 at 10:54 AM.

  5. #5
    Senior Member
    Join Date
    May 2003
    Location
    Novi Sad, Serbia
    Posts
    576

    I have to ask...

    ...is display_fell_details function a JS function or PHP? It seems to me that it is a JS function...

    If that is the case try looking at the source of the page how the function call is created i.e. something like: display_fell_details(189) and not display_fell_details($SummitNo)...

    Because if is the latter case then something with your expressing of the variable is not working properly and the variable name is shown and not the variable value...

    Well, as far as I can figure out anyway...
    I know that I don't know nothing...

  6. #6
    Junior Member
    Join Date
    Jan 2006
    Posts
    7
    Hi Bodzan

    Function_display_fells is a PHP function.

    I now gather that a php function cannot be called from a button, but cannot think of a workaround.

    Any Idea's?

    Thanks again

  7. #7
    addicted thorpe's Avatar
    Join Date
    Aug 2004
    Location
    sydney
    Posts
    3,457
    The only work around would be to use ajax, but its a bit of work.

    You must try to understand that php runs on the server, while javascript runs on the client, and never the two shall meet.

  8. #8
    Senior Member
    Join Date
    May 2003
    Location
    Novi Sad, Serbia
    Posts
    576

    Or...

    Do as I suggested first:
    Quote Originally Posted by bodzan
    Try creating a submit button and in form action define a PHP page to handle the request and then you'll be able to use the PHP function you created.
    I know that I don't know nothing...

  9. #9
    Junior Member
    Join Date
    Jan 2006
    Posts
    7
    Thanks Bodzan,

    I will submit the form to the current page, with an extra variable sent.

    I will then put an if statement that if the variable is set it calls the function.

    If not set the function will not be called.

    That way the function wont be called when the page is opened originally.

    I take it you can replace a grey submit form button with an image.

    I just need to read my book and learn how.

    Will the above work.

    Thanks for yuor help everyone.

    Im learning slowly

  10. #10
    Senior Member
    Join Date
    May 2003
    Location
    Novi Sad, Serbia
    Posts
    576

    Talking Yeah, that should work...

    That would work... You will need onClick event to submit a form where a button is an image you need. Somethig like this:
    PHP Code:
    <input type="image" src="path/to/image.jpg" name="submit" value="Submit" onClick="submit()"
    ..if I'm not mistaken...


    And - we are all still learning no matter how long we are into this...

    And you can come here whenever to get help...
    I know that I don't know nothing...

  11. #11
    Junior Member
    Join Date
    Jan 2006
    Posts
    7
    Thanks for that Bodzan,

    I'll give it a whirl and no doubts I'll be posting in the future..

    Cheers

  12. #12
    Junior Member
    Join Date
    Jun 2010
    Posts
    1

    HII

    I just read this forumpost,
    And i just rember the next hing i also am using for my page.

    Use a form with action=thesamepage.php target=_self (if you user frames)
    And the njust get the Varibles for your php funciton from the URL's Get values ( $_GET['name'])

    SEe you around. Okey. Hope this helps you

  13. #13
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    The solution is to write it in that form:

    echo '<input type="button" value="Click me!" onclick="display_fell_details(&#039;'.$SummitNo.'&#039" >';

    anoter example:

    echo '<td><a href="#" onClick="changeName(&#039;'.$row['name'].'&#039;" >'.$row['id'].'</a></td>';

    thats bc the meaning of &#039; which is "'" (single).

  14. #14
    Junior Member
    Join Date
    Jun 2012
    Posts
    4
    Sorry. Thats the currect answer:

    The solution is to write it in that form:

    PHP Code:
    echo '<input type="button" value="Click me!" onclick="display_fell_details('''.$SummitNo.''')" >'
    anoter example:

    PHP Code:
    echo '<td><a href="#" onClick="changeName(''.$row['name'].'');" >'.$row['id'].'</a></td>'
    thats bc the meaning of & # 0 3 9 which is "'" (single).

  15. #15
    Junior Member
    Join Date
    Jun 2012
    Posts
    4

    WOW, that forum makes me crazy:

    Sorry. Thats the currect answer:

    The solution is to write it in that form:

    PHP Code:
    echo '<input type="button" value="Click me!" onclick="display_fell_details(111''.$SummitNo.''111)" >'
    anoter example:

    PHP Code:
    echo '<td><a href="#" onClick="changeName(111'.$row['name'].'111);" >'.$row['id'].'</a></td>'
    while instade of 111 write & # 0 3 9 without spacechars.
    Thats because the meaning of & # 0 3 9 which is "'" (single).

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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