Results 1 to 4 of 4

Thread: how to send onclick event true to php?

  1. #1
    Join Date
    Jan 2012
    Posts
    21
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default how to send onclick event true to php?

    I wish to know how to send input button onclick event to php so then I could assign function and return result depending on this event.
    I know just two ways:
    1- with onclick ="javascript: function dosomething();"
    2- with jquery $(".selector").click(function(){do something}
    but neither give me handle this as variable inside php.

  2. #2
    Join Date
    Feb 2012
    Location
    Melbourne, Australia
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ajax

    if you are wanting to retrieve database information via javascript through PHP you will need to do some research on using some Ajax and the HttpRequestObject

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,076
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    It's a general question, so here's a general answer and in pretty much the simplest terms -

    On the sending page (some.htm):

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    <script type="text/javascript">
    jQuery(function($){
    	$('#mybut').click(function(){
    		jQuery.ajax({
    			url: 'process.php',
    			type: 'POST',
    			data: 'picnum=' + ($('#picnum').val() || 0),
    			success: function(result){
    				alert(result);
    			}
    		});
    	});
    });
    </script>
    </head>
    <body>
    Pick a Number, Any Number: <input type="text" id="picnum">
    <input type="button" id="mybut" value="Go!">
    </body>
    </html>
    On the receiving page (process.php):

    PHP Code:
    <?php
    $picnum 
    = isset($_POST['picnum'])? $_POST['picnum'] : 0;
    echo 
    "The picnum was set to $picnum.";
    ?>
    Obviously you could do a lot more on either or both pages depending upon your objectives.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    As a background note, it is important to understand how PHP works. PHP is executed on the server and its output is sent to the browser as HTML (and CSS and Javascript). Therefore, when Javascript is running in the browser, PHP is already done and cannot be used again.

    So mixing JS and PHP is impossible.

    Using Ajax is a method to get around this, but you're still not really using PHP and JS at the same time on the same page-- you're essentially loading a separate page in a hidden way (via JS, in the background).

    Understanding that premise is helpful.

    That said, you can use Ajax (though it is difficult in some cases) to pretend that JS has access to PHP.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •