7

How do I pass the values of txtname and tel as variables to the .load???

$(document).ready(function(){
    $("#add").click(function(){
        $("#result").load("add.php", {name: #txtname});
    });
});

The html:

<p>Name:<input type="text" name="name" value="" id="txtname" /></p>
<p>Telephone:<input type="text" name="tel" id="tel" value="" /></p>
<input type="submit" value="Submit" id="add" />
Joel
  • 19,175
  • 2
  • 63
  • 83

5 Answers5

15
$(document).ready(function() {
    $("#add").click(function() {
       $("#result").load("add.php", {
           name: $("#txtname").val(), 
           tel: $("#tel").val()
       });
    });
});
Tim S. Van Haren
  • 8,861
  • 2
  • 30
  • 34
7

I've found a good way to do this - use serializeArray() for the data part, if you are pulling from a form and still want to use .load(). It may save you some extra work.

var form_data = $('#my-form').serializeArray();
$('.my-container').load('/myurl/', form_data);
Charles Harmon
  • 380
  • 2
  • 10
2
$('.element').load('page.php', {var1:'value1', var2:'value2'}, function() {
// actions after load page (optional)
});
Brynner Ferreira
  • 1,527
  • 1
  • 21
  • 21
0

You can also wrap your inputs into a form

<form action="add.php" method="GET" id="my_form">
  <p>Name:<input type="text" name="name" value="" id="txtname" /></p>
  <p>Telephone:<input type="text" name="tel" id="tel" value="" /></p>
  <input type="button" value="Submit" id="add" />
</form>

So you can easily maintain your parameters

$(document).ready(function() {
  $("#add").click(function() {
    $("#result").load(
      $("my_form").attr('action')
    , $("my_form").serialize()
    });
  });
});
Pierre de LESPINAY
  • 44,700
  • 57
  • 210
  • 307
0
$(document).ready(function(){
  $("#add").click(function(){
    $("#result").load("add.php", {
      'name': $("#txtname").val(), 
      'telephone': $("#tel").val()
    });
  });
});
Sampson
  • 265,109
  • 74
  • 539
  • 565