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

    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.

      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:

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

        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

          ...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... 😃

            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

              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.

                Do as I suggested first:

                bodzan wrote:

                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.

                  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

                    That would work... You will need onClick event to submit a form where a button is an image you need. Somethig like this:

                    <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... 😃

                      Thanks for that Bodzan,

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

                      Cheers

                        4 years later

                        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:p

                          2 years later

                          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).

                            Sorry. Thats the currect answer:

                            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 & # 0 3 9 which is "'" (single).

                              Sorry. Thats the currect answer:

                              The solution is to write it in that form:

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

                              anoter example:

                              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).

                                5 days later

                                If you want a submit button, use a submit button rather than an image which uses javascript.

                                If you want it to look like an image, set the buttons value to an empty string, give it background-image: url('path/to/image') and optionally border: none;

                                  Write a Reply...