• Register
search
Log In
1 vote
271 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');

 

12.8k points
edited by

Related questions

1 vote
1 answer 142 views
142 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
1 vote
1 answer 29 views
29 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
1 vote
2 answers 159 views
159 views
Problem : I am very new to the Node.js and I am having few issues. I am using the Node.js 4.10 and Express 2.4.3. While trying to execute my code I am receiving below error : Error: Can't render headers after they are sent to the client. ... , scope: 'offline_access,email,user_about_me,user_activities,manage_pages,publish_stream', failedUri: '/noauth' }) ])); realapp.use(realapp.router); }); . . .
asked Dec 12, 2019 alecxe 7.5k points
1 vote
1 answer 16 views
16 views
Problem: Hello guys, I have a very simple question regarding Java Programming. I&rsquo;ve searched your website before posting this question but I haven't got any appropriate answer. Yes, there are a couple of answers but not as specific as I needed. I want to generate some random numbers in an array of Java programs. So, help me to fill an array with random numbers java. Many thanks...
asked Apr 6 Gavin 15.2k points
0 votes
2 answers 177 views
177 views
Problem : I have installed Java application on my Linux machine and now i am receiving following error : no x11 display variable was set, but this program performed an operation which requires it.
asked Nov 18, 2019 peterlaw 6.9k points