Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jul 2012
    Location
    Boston
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Sending Data: back to my PHP function as arguments

    Hello,

    I'm very new to jQuery and only recently been adapting to JS while working with it. Crazy I know.
    Also, WordPress complicates things much further than standard AJAX... I've been able to break through (don't ask how many hours) until this:

    I'm trying to send latitude and longitude to my PHP function (gathered from JS, navigator.geolocation). I've tried several things but right now I'm building up an object(?) that's working but not creating valid arguments for my PHP query function to use.

    PHP Code:
    console.log(lat);// logs correct latitude
    console.log(lon);// logs correct longitude

    // data object for .ajax data:
        
    var data = {
            
    action:'wpa56343_search',
            
    latitudelat,
            
    longitudelon
        
    };

    // On click AJAX fires.
        
    $("#searchsubmit").click(function(e){
            
    e.preventDefault();
            
    _do_ajax(this);
            }); 
    // end click func

            
        
    function _do_ajax(obj) {
            $.
    ajax({
                
    type:"POST"// The type of request to make
                
    urlMy_Obj.ajaxurl// the request is sent to this url
                
    datadata,
                
    dataType'json',
                
    success: function( response ){ 
    My function my_ajax_search() (in functions.php) isn't getting the arguments.
    My function works with hard coded lat and long BTW:

    PHP Code:
    $latitude $_POST['latitude'];
    $longitude $_POST['longitude'];
    function 
    my_ajax_search($latitude$longitude){//make a query} 
    Last edited by Webenvelopment; 08-03-2013 at 06:21 PM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,692
    Thanks
    25
    Thanked 657 Times in 656 Posts
    My_Obj.ajaxurl this has to be a php file => My_Obj.php

    I used a button to simulate your JS lat/lon gathering function.

    The simple My_Obj.php
    PHP Code:
    <?php
    $action 
    $_POST["action"];
    $latitude $_POST["latitude"];
    $longitude $_POST["longitude"];

    echo 
    'action is '.$action.'<br />'.'latitude is '.$latitude.'<br />'.'longitude is '.$longitude.'<br />';
    ?>
    The calling function
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script type="text/javascript">
    function _do_ajax(here, there) {
        $.ajax({
            type:"POST",
            url: "My_Obj.php",
            data: {
    	        action:'wpa56343_search',
    	        latitude: here,
    	        longitude: there
    	    }}).done(function( response ){
    			document.getElementById("writ").innerHTML = response;
    	});
    }
    
    </script>
    </head>
    
    <body>
    <button id="" value="PUSH" onclick="_do_ajax('lat', 'long');">PUSH</button>
    <div id="writ"></div>
    </body>
    </html>
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New Coder
    Join Date
    Jul 2012
    Location
    Boston
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thank you Sunfighter. I should have clarified that in WP you must use wp_localize_script() to send variables to JS within an object. In my case here is that function in case your interested:

    PHP Code:
    wp_localize_script'my_theme_init''My_Obj', array( 'ajaxurl' => admin_url'admin-ajax.php' )) 
    So you can see that I now have my URL within -> My_Obj.ajaxurl its pointing to the 'must-use' WordPress AJAX core function admin-ajax.php.

    Then you also must init your actual query action function for your AJAX with:

    PHP Code:
    add_action'init''my_ajax_init' );
    function 
    my_ajax_init() {
        
    add_action('wp_ajax_nopriv_wpa56343_search''my_ajax_search');
        
    add_action('wp_ajax_wpa56343_search''my_ajax_search');

    Now I can use that action:
    PHP Code:
    my_ajax_search(){ 'query to return to AJAX here' 
    Trying to get my latitude and longitude into this action function so I can actually start working on conditional queries and such.

    Can you tell me why I cannot just do it the way I was by building up a JS object with everything in it then referencing it within my AJAX function? It triggers my query function but something regarding scope maybe why I can't get those lat lon vars?


    UPDATE:
    I created a JS object to build up my "data string?" and logged it. This is what my console shows for my data object:
    PHP Code:
    Object {action"wpa56343_search"latitude"42.041261299999995"longitude"-70.9414206"
    However I still get nothing for $_POST['latitude']/$_POST['longitude'] within functions.php
    Last edited by Webenvelopment; 08-03-2013 at 11:06 PM.

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,692
    Thanks
    25
    Thanked 657 Times in 656 Posts
    Sorry. I stay away from WordPress because of all the extra stuff they introduce. Same goes for durpal and the rest of em.
    Sorry I could not help.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    New Coder
    Join Date
    Jul 2012
    Location
    Boston
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I completely understand, and for what it's worth you're right on about regular AJAX jQuery stuff.
    UPDATE:
    The way it works is that POST vars are only available WITHIN the action function I guess... I was trying to grab them in the global scope of functions.php but you cannot. In fact doing so broke my site. Once I tried using them within the function they were available.
    Last edited by Webenvelopment; 08-04-2013 at 05:56 PM.


  •  

    Posting Permissions

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