• Resolved muazzamazaz

    (@muazzamazaz)


    My Js file containts:

    jQuery(document).ready(function($) {
    $('#placeorder').click(function(){   alert('6');
    
        	$.ajax({url:MyAjax.ajaxurl,
    action: 'my_action'});
    									});
    
    });

    and php file:

    // embed the javascript file that makes the AJAX request
    wp_enqueue_script( 'my-ajax-request',get_template_directory_uri() . '/js/countclicks.js', array( 'jquery' ) );
    // declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
    wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    
    add_action('wp_ajax_my_action', 'my_action_callback');
    add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
    
    function my_action_callback() {
    
    echo "<script Langauage=\"Javasacript\">";
    echo ("alert(\"Invalid user, please try again\.\");");
    echo "</script>";die();
    
    }

    function file include above PHP file as:
    include_once(‘order_submit_inc.php’);

    But I can’t execute my PHP function on button click. I want to call this from page template.

Viewing 7 replies - 1 through 7 (of 7 total)
  • You can’t echo the alert from the php file. Instead, echo the alert from the response of the ajax request.

    You need to process your response from your jQuery.

    jQuery(document).ready(function($) {
    
        $('#placeorder').click(function(){   
    
            alert('6');
    
            data = {action: 'my_action'};
    
        	$.post(MyAjax.ajaxurl, data, function(response) {
    
                    // This is where we alert the response
    		alert('Got this from the server: ' + response);
    	});
        });
    });

    And the PHP:

    add_action('wp_ajax_my_action', 'my_action_callback');
    add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
    
    function my_action_callback() {
    
        echo 'TESTING AJAX';
    
        die();
    }

    Thread Starter muazzamazaz

    (@muazzamazaz)

    It works fine for getting function into action.

    I need to write insertion query in function like:

    $wpdb->insert( 'shopping_list', array('user_id'=>$current_user->ID,
    'title'=>$_POST['title'],
    'shopping_list'=>$_POST['shopping_list'],
    'delivery_date'=>$_POST['delivery_date'],
    'creation_time'=>date,
    'delivery_slot'=>$_POST['delivery_slot'],
    'instruction'=>$_POST['instruction'],
    'address'=>$_POST['address']
    )
    );

    What changes should be in file.

    This goes inside the php ajax function:

    global $wpdb;
    
    if($wpdb->insert( 'shopping_list', array(
    	'user_id'=>$current_user->ID,
    	'title'=>$_POST['title'],
    	'shopping_list'=>$_POST['shopping_list'],
    	'delivery_date'=>$_POST['delivery_date'],
    	'creation_time'=>date,
    	'delivery_slot'=>$_POST['delivery_slot'],
    	'instruction'=>$_POST['instruction'],
    	'address'=>$_POST['address']
    	)
    ) != false) {
    	echo 'INSERT SUCCESSFUL';
    } else {
    	echo 'INSERT FAILED';
    }
    
    die();

    You’ll also need to get the values from your jquery ajax to your php file. You can pass them through using the data js variable.

    Thread Starter muazzamazaz

    (@muazzamazaz)

    The following JS file code has solved the problem and PHP function getting values.

    jQuery(document).ready(function($) {
    
        $('#placeorder').click(function(){   
    
           // data = {action: 'my_action'};
         var dataString = {action: 'my_action',
    			'title' 				: $('#title').val(),
    			'shopping_list' 			: $('#shopping_list').val(),
    			'delivery_date' 	: $('#delivery_date').val(),
    		'delivery_slot' 	: $('#delivery_slot').val(),
    			'special_instructions' 			: $('#special_instructions').val(),
    			'address' 	: $('#address').val()
    		};
    
    		$.post(MyAjax.ajaxurl, dataString, function(response) {
    
                    // This is where we alert the response
    		alert('Got this from the server: ' + response);
    	});
        });
    });

    Thank You So Much.

    Thread Starter muazzamazaz

    (@muazzamazaz)

    global $wpdb don’t have direct access as shows no error when using its function and even not insert and record. Don’t have dbname property value inside it.

    Thread Starter muazzamazaz

    (@muazzamazaz)

    The whole code in PHP file:

    <?php
    
    // embed the javascript file that makes the AJAX request
    wp_enqueue_script( 'my-ajax-request',get_template_directory_uri() . '/js/countclicks.js', array( 'jquery' ) );
    // declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
    wp_localize_script( 'my-ajax-request', 'MyAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    
    add_action('wp_ajax_my_action', 'my_action_callback');
    add_action('wp_ajax_nopriv_my_action', 'my_action_callback');
    
    function my_action_callback() {
    global $wpdb;
    
    if($wpdb->insert( 'shopping_list', array(
    	'user_id'=>$current_user->ID,
    	'title'=>$_POST['title'],
    	'shopping_list'=>$_POST['shopping_list'],
    	'delivery_date'=>$_POST['delivery_date'],
    	'creation_time'=>date,
    	'delivery_slot'=>$_POST['delivery_slot'],
    //'has_file'=>$_REQUEST['has_file'],
    	'instruction'=>$_POST['instruction'],
    	'address'=>$_POST['address']
    	)
    ) != false) {
    	echo 'INSERT SUCCESSFUL';
    } else {
    	echo 'INSERT FAILED';
    }
    
    die();
    }
    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    Don’t cross post new topics, they get deleted when found. If you have something to add to this one please do. You can always marked this one unresolved.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Code Issue’ is closed to new replies.