简体   繁体   中英

How to get text filed value in ajax?

I have a table with dynamic data from database, each row consist of a text filed and 2 link (accept or reject). then if user clicks on any of these link, the row will disappear and the rest of the rows are only visible in table.

I get ID of each row with ajax by clicking on each link, however I also need to get the text-field value. how can I get it? I need to have in ajax cause after getting value I need to insert in database with php+sql.

this is my ajax part for link :

 $('a.accept').click(function(d) {
    d.preventDefault();
    var parent = $(this).parent();
    $.ajax({
      type: 'get',
      url: 'Test.php',
      data: 'ajax=1&accept=' + parent.attr('id').replace('record-',''),
      beforeSend: function() {
        parent.animate({'backgroundColor':'#fb6c6c'},300);
      },
      success: function() {
        parent.slideUp(300,function() {
          parent.remove();
        });
      }
    });
  });

});

how can I include text filed value in it?

please comment me which I'm really in need to solve it, Thanks

You can add manually your text to the GET request.

(snippet)

data: 'ajax=1&accept=' + parent.attr('id').replace('record-','') + '&text=VALUE',

Substitute text with the name you want to be received in PHP. Substitute VALUE with the text entered on the page that you want to grab - and don't forget to encode the value.

You will need the name of id of the textfield first. Then you could do something like this:

var textboxvalue = $('name or id of textfield').val();

Then you will need to append this value to your data string:

data: 'ajax=1&textvalue='+textboxvalue+'accept=' + parent.attr('id').replace('record-',''),

Then you can use $_GET['textvalue']; to get the value of textbox.

Use following and you're good to go. Also you had extra '});' at the end line of JS fragment. I had it removed. But make sure you give id to text fields in following pattern :

text-fld-RECORD_ID
where
$('a.accept').click(function(d) {
    d.preventDefault();
    var parent = $(this).parent();
    var id = parent.attr('id').replace('record-','');
    //make sure that text field has ID in pattern 'text-fld-RECORD_ID'
    var text_fld = $('#text-fld-'+id).val();
    $.ajax({
      type: 'post', // I suggest using post; get will be harmful in such occasions
      url: 'Test.php',
      data: {ajax:1,accept:id,text:text_fld},
      beforeSend: function() {
        parent.animate({'backgroundColor':'#fb6c6c'},300);
      },
      success: function() {
        parent.slideUp(300,function() {
          parent.remove();
        });
      }
    });  
});
is the ID of the record.

 $('a.accept').click(function(d) { d.preventDefault(); var parent = $(this).parent(); var id = parent.attr('id').replace('record-',''); //make sure that text field has ID in pattern 'text-fld-RECORD_ID' var text_fld = $('#text-fld-'+id).val(); $.ajax({ type: 'post', // I suggest using post; get will be harmful in such occasions url: 'Test.php', data: {ajax:1,accept:id,text:text_fld}, beforeSend: function() { parent.animate({'backgroundColor':'#fb6c6c'},300); }, success: function() { parent.slideUp(300,function() { parent.remove(); }); } }); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM