• Register
1 vote
309 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);
                }
            }
2.1k points
reopened by

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

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');

 

15.3k points
edited by

Related questions

1 vote
1 answer 144 views
144 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 11 views
11 views
I am finding difficulty with following issue, any help appreciated: java.sql.sqlexception: can not issue data manipulation statements with executequery().
asked Jul 25 James Martin 1.3k points
0 votes
1 answer 8 views
1 vote
1 answer 33 views
33 views
Problem: I am looking for a solution to my existing code. Well, I am a new student of java. I was thinking about a program that can check whether the given char is an upper case or not! Let&rsquo;s say I&rsquo;ve given a char as &lsquo;a&rsquo; and I want ... , my question is, how to check whether a char variable ch is an uppercase letter, you write? Thanks for your valuable time and effort guys.
asked Mar 22 Gavin 15.2k points