www.webdeveloper.com
Results 1 to 8 of 8

Thread: ondblclick can't invoke a php function?

  1. #1
    Join Date
    Feb 2012
    Posts
    4

    ondblclick can't invoke a php function?

    My desired action is to load a new php page when the user double-clicks on an entry in a list.

    PHP Code:
    <?php
        
    function getdblclick($a) {
            echo 
    'arg1 is $a';
            include(
    "next_page.php");
        }
        <
    form>
        <
    select size="5" id="eid" ondblclick="getdblclick(this.value)">
        <
    option value=1>One</option>
        <
    option value=2>Two</option>
        <
    option value=3>Three</option>
        <
    option value=4>Four</option>
        </
    select>
        </
    form>
    ?>
    If I double-click on one of the entries in this list, nothing happens.
    However, if I change the declaration to this:

    PHP Code:
    <select size="5" id="eid" ondblclick="alert(this.value)"
    then it does work, and I get an alert panel.

    Why can this invoke a javascript function but not a php function?

  2. #2
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Part of the problem is that you are attempting to use a JavaScript event (ondblclick) to run a PHP function. That won't work, period. Not like that, anyway.

    Another thing that I noticed about the code you supplied is that the SELECT, itself, is contained within the opening <?php and closing ?> tags, without benefit of echo to generate the HTML. If you COULD do it that way, then the closing ?> would have to be moved to before the <select tag.

  3. #3
    Join Date
    Feb 2012
    Posts
    46
    The first example in this thread is just so wrong that you really should get back to basics here and go through tutorials on w3schools.com

    PHP is a back-end scripting engine, it only generates HTML (or other content type) based on your request. It has nothing to do with HTML and JavaScript itself once it is loaded in your browser. Functions in HTML, that can be called, are JavaScript functions.

    PHP runs only on your web server. HTML, CSS and JavaScript run only in users browser. PHP function and JavaScript function are not the same thing.

  4. #4
    Join Date
    Feb 2012
    Posts
    4
    Well, I was hoping there was an easy out... So Javascript is the only way to load a new page when the user double-clicks on an entry in a list?

  5. #5
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    You could use JavaScript to redirect to a new URL and use the select option values as the URL's to redirect to.

    Code:
    <script type="text/javascript">
    function getdblclick(a) {
        window.location.href = a;
        }
    </script>
    HTML Code:
        <form name="thisForm" method="post"> 
        <select size="5" id="eid" ondblclick="getdblclick(this.value);"> 
        <option value="http://www.domain.com/page1.html">One</option> 
        <option value="http://www.domain.com/page2.html">Two</option> 
        <option value="http://www.domain.com/page3.html">Three</option> 
        <option value="http://www.domain.com/page4.html">Four</option> 
        </select> 
        </form>
    Last edited by WolfShade; 03-01-2012 at 03:22 PM.

  6. #6
    Join Date
    Feb 2012
    Posts
    4
    Wow, this is really close to what I'm trying to do. The list that the user double-clicks on is a list of data files, and the page I'm trying to redirect to formats and displays the data.

    So, would something like this work:

    Code:
    <script type="text/javascript">
    function getdblclick(a) {
        window.location.href = "http://www.domain.com/analyze_data.html?dataset=" + a;
        }
    </script>
    HTML Code:
        <form name="thisForm" method="post"> 
        <select size="5" id="eid" ondblclick="getdblclick(this.value);"> 
        <option value="dataset1.dat">One</option> 
        <option value="dataset2.dat">Two</option> 
        <option value="dataset3.dat">Three</option> 
        </select> 
        </form>
    ...where I expect the analyze_data.html page will have $_GET['dataset'] set to the value of 'a' ("dataset1.dat", etc).

  7. #7
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    That looks like it will work, except the page you are redirecting to should be a .php (ie, analyze_data.php) page if you are going to use $_GET['dataset']. Let me know how it goes.

  8. #8
    Join Date
    Feb 2012
    Posts
    4
    Worked out perfectly! Thx to all, I learned a lot!

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
  •  
HTML5 Development Center



Recent Articles