How to pass hidden value to ajax ?

How to pass hidden value to ajax ?

mlotfimlotfi Posts: 60Questions: 5Answers: 0
edited February 2014 in General
Hi,

I am trying to pass data value Ihidden in datatable) from jsp to js file :

[code]















Edit





[/code]


js file :

[code]
$('#metadataListTable input.delete').live('click', function() {
// alert("attached delete");
var ans = confirm("Do you want to delete agency?");
if(ans==true){
var nRow = $(this).parents('tr')[0];
var ifaceData = $("#ifaceData").val();
alert(ifaceData); // This alert display undefined <-------------------
oTable.fnDeleteRow(nRow);
$.ajax({
url: "deleteMetadata.do",
data: "id=" + nRow.id + "&flag=" + "T" + "&ifaceData=" + ifaceData,
success: function(response) {
alert("response from the action : " + response);
}
})
}
});
[/code]

Please your help is appreciated.

Replies

  • kwol80kwol80 Posts: 2Questions: 0Answers: 0
    There are two ways that I know of, which will allow you to do this (these come from some PHP code that I have written, but the AJAX should be the same). The first is to wrap your table in a form, and when you submit, you do something like this:

    [code]$("#form1").submit(function(event){
    event.preventDefault();
    var str = $(this).serialize(); //Contains the data from all input fields in the form
    $.ajax({
    url:"deleteMetadata.do",
    type:"post",
    data: str,
    success:function(response){
    alert("response from the action : " + response);
    }});
    });[/code]

    This way, whatever is in the hidden field is included in [code]var str[/code]

    or, you could pass your variable like this:

    [code]var ifaceData = $("#ifaceData").val();

    $.ajax({
    url:"deleteMetadata.do",
    type:"post",
    data: {submitThis:ifaceData},
    success:function(response){
    alert("response from the action : " + response);
    }}); [/code]

    I have had trouble trying to build a data string within the AJAX call, using the [code]variable="somedata"&variable2="moredata"[/code] format. If you want to go that way, build it in a variable before you call AJAX, and pass that variable as the data.

    I hope this helps. Good luck.
  • ashiersashiers Posts: 101Questions: 8Answers: 7
    There is also an example that shows how to do this using the aoData property: https://datatables.net/examples/server_side/custom_vars.html If you haven't yet been there, you might want to check out the JED website and discover how to use DataTables on the java platform. Check out: http://jed-datatables.ca/jed/
  • mlotfimlotfi Posts: 60Questions: 5Answers: 0
    Thank you kwol80 and kwol80, I used the html5 data attribute, it worked fine.

    I have a question, if I use :
    var str = $(this).serialize();

    do I have to parse it in the server side to get each data that was submitted in the form ?
    Thanks
  • kwol80kwol80 Posts: 2Questions: 0Answers: 0
    edited February 2014
    You would just use something like [code]var = $_POST['inputName'];[/code]
This discussion has been closed.