• Register
1 vote
464 views

i want to add some string to the url in ajax process, but fail every time i access the function. when i submit the form it only access this url 'Master_data/'

var jenis_submit = document.getElementById('jenis_submit');
            var submit_url = '';
            if(jenis_submit == '1'){

                submit_url = "func_save_instansi";
            }else if(jenis_submit == '2'){

                submit_url = "func_update_instansi";
            }

            $.ajax({
                url: '<?php echo base_url();?>Master_data/'+ submit_url,
                method: 'POST',
                data: dataKirim, 
                contentType: false,
                cache: false,
                processData: false,
                success:function(data){
                    // console.log(data);
                    $('#notif').html(data);
                }
            }
13 9 3
2,100 points

2 Answers

1 vote
jQuery(document).ready(function($) {

   var PostCode=1;
   $.ajax({ url : "http://SomeAddress.com/"+PostCode +".json",
   dataType : "jsonp"
   //.... more stuff
  });


  $("#SetPostCode").click(function() {
     PostCode = document.getElementById("GetPostCode").value;
     $("#GetPostCode").val(" ");
     return false; 
  });

});

Above would work as PostCode is now global to the jQuery and can be accessed anywhere

1
540 points
0 votes

Solution:

Code:

jQuery(document).ready(function($) {

   var PostCode=1;
   $.ajax({ url : "http://SomeAddress.com/"+PostCode +".json",
   dataType : "jsonp"
   //.... more stuff
  });


  $("#SetPostCode").click(function() {
     PostCode = document.getElementById("GetPostCode").value;
     $("#GetPostCode").val(" ");
     return false; 
  });

});

Above would perform as PostCode is now global to the jQuery and can be accessed everywhere

Or, you can also use variables for the parameters.

var url="";
var var1 = "somevalue"

function ajaxDialog() {
  $.ajax({
    url: url,
    type: "GET",
    data:{
       data1: var1   
         }
      });
}

Ajax url must be place to wp-admin/admin-ajax.php as ajax requests must be handled via wp-admin/admin-ajax in wordpress. In case ajaxurl is not defined you can have it by defining html attribute data-url as data-url="<?php echo get_admin_url().'admin-ajax.php'?>".

jQuery(document).ready(function($){
   $("#wp-submit").on('click',function(e){
      e.preventDefault();
      username = $("#userName").val();
      password = $("#passWord").val();
      ajaxurl  = $(this).data('url');
      $.ajax({

      type: "POST",
      url: ajaxurl,
      data : {

            name     : username,
            password : password,
            action   : 'check_user_login'

        },
       success: function(response){    
         if(response =='true')    {
             $("div#result").css('display', 'inline', 'important');
             $("div#result").html("Login Successful");
             console.log('true');
         }else{
            $("div#result").css('display', 'inline', 'important');
            $("div#result").html("Wrong username or password");
         }

       } 
    });
  });
});

Now make your ajax function as

function user_login(){


   //do your stuff here
    global $wpdb;
    $username=$_POST['name']; 
    $password=$_POST['password']; 
    $hash = md5($password);
    $query = $wpdb->prepare( "SELECT * FROM `wp_users` WHERE `user_email` = %s AND `user_pass` = %s", $username, $hash );
    $result = $wpdb->get_results($query);
     if (count($result)==1) { echo 'true';
     echo '<span>Login Successful</span>'; exit(); 
    } else if(count($result) < 1) {
     echo 'false'; exit(); }
}

Foe this time the ajax function must be hooked to hook wp_ajax_* and wp_ajax_nopriv_*. Here, * is your action value for example- data.action so it becomes

 add_action('wp_ajax_nopriv_check_user_login','user_login');
 add_action('wp_ajax_check_user_login','user_login');

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 8 views
8 views
We are searching for a way to call a javascript number in the body of an html page.
asked Sep 25 Daniel Anderson 4k points
0 votes
1 answer 5 views
5 views
Can any one tell me how to set current date in datepicker using JavaScript?
asked Sep 29 Daniel Anderson 4k points
1 vote
1 answer 153 views
153 views
I'm building an Email verification script and i'm using ajax so whenever the user fill in a false or an invalid email, an error bar with the error name will be displayed, And when the email is true and belongs to an active account, an email will be sent to the user with ... saying email sent. So i wan't some lines to detect if the email wasn't sent to show "email was not sent" in the error bar.
asked May 29, 2019 sadmin 2.1k points
0 votes
1 answer 9 views
9 views
Is there any tutorial that will show how to display database record without without pressing the submit button? For example, <select name="cars"> <option value="">Select a car:</option> <option value="1">Volvo</option> <option value="2">BMW</option> ... > </select> In this example, the user will select anything from the list and should automatically display the information based on the selection.
asked Oct 11 azriel 360 points