• Register
1 vote
1.1k 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);
                }
            }
14 9 7
2,200 points

Please log in or register to answer this question.

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

1 vote
1 answer 180 views
180 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.2k points
0 votes
1 answer 44 views
44 views
We are searching for a way to call a javascript number in the body of an html page.
asked Sep 25, 2020 Daniel Anderson 4k points
0 votes
1 answer 10 views
10 views
Problem: I'm pretty new with Javascript and I would like to use canvas with javascript in my Django template. I have a little html part and javascript part which looks like this : {% for document in publication.documents.all %} <td class="col-md-1"> <canvas ... .width = viewport.width; var renderContext = { canvasContext: context, viewport: viewport }; page.render(renderContext); }); }); </script>
asked Apr 26 anika11 32.2k points
0 votes
1 answer 9 views
9 views
Can any one tell me how to set current date in datepicker using JavaScript?
asked Sep 29, 2020 Daniel Anderson 4k points
0 votes
1 answer 4 views
4 views
Problem: I don't see how the callback function in JSONP is any different from the success callback function in AJAX.
asked Apr 20 ummesalma 29.2k points
0 votes
1 answer 2 views
2 views
Problem: Provide me the method for inserting image in database using ajax in php.
asked Apr 10 zayed1 61.8k points
0 votes
1 answer 4 views
4 views
Problem: Please let me know any solution regarding this .. How to pass multiple values in ajax to php?
asked Apr 7 Ifra 37.2k points
0 votes
1 answer 29 views
29 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, 2020 azriel 590 points