开发者

Passing Parameters To JQuery Ajax Call

开发者 https://www.devze.com 2023-04-05 08:46 出处:网络
I am new to JQuery.I\'ve figured out how to make an AJAX call, and how to create callback functions for success or failure.I am having difficulties figuring out how to pass parameters, though.Here is

I am new to JQuery. I've figured out how to make an AJAX call, and how to create callback functions for success or failure. I am having difficulties figuring out how to pass parameters, though. Here is my JQUery code:

$(document).ready(function(){
$('#submit_new_admin').click(function() {

    
    $.ajax({
      type: 'POST',
      url: '[mu_url]/process_request.php',
      data: { request: 'add_new_admin' },
      beforeSend:function(){
        $('#ajax-panel').html('<div class="loading"><img src="/images/loading.gif" alt="Loading..." /></div>');
      },
      success:function(data){
      alert('ok')'
      },
      error:function(){
        $('#ajax_response').html('<p class="error"><strong>Oops!</strong> Try that again in a few moments.</p>');
      }
    });
}); 
});

That works well enough, but I want to set the parameter variables dynamically. Here is a form that I am using:

    <form name="" action="" method="post">
        <select name="user_level">
            <option value="1">Full Admin</option>
            <option value="2">Junior Admin</option>
        </select><br/><br/>
        <label for="firstname">Firstname</label>  <input type="text" name="firstname"><br/><br/>
        <label for="lastname">Lastname</label>  <input type="text" name="lastname"><br/><br/>
        <label for="username">Username</label>  <input type="text" name="username"><br/><br/>
        <label for="fb_id">FaceBook ID</label>  <input type="text" name="fb_id"><br/><br/>
        <label for="password">Password</label>  <input type="password" name="password"><br/><br/>
        <label for="password_conf">Enter Password Again</label>  <input type="password" name="password_conf"><br/><br/>
        <input type="hidden" name="submitted">
        <input id="submit_new_admin" type="button" value="Add As Admin">
    </form>

What I am trying to do witht eh Jquery is pass the value of the text inputs.

example

data: {开发者_如何学编程 request: 'add_new_admin', firstname: [firstname value from form], lastname: [lastname value from form], username: [username from form]} and so on.

How can I do this?


you can use jquery to populate the dynamic values - e.g. data: {firstname : $('#firstname').val(), ......}

However, as you have a hugh form you can use the jquery form serialize apis to pass all the form elements as an url or array with the ajax call instead of populating individual elements.

e.g. $.post("test.php", $("#testform").serialize());

More information at serialize & Jquery.post


'content' is a parameter that can be passed into your ajax call, 'this' then refers to the contents of 'content'

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号