php - How to get variable value and pass it on to AJAX url -


here code:

function download () { var val1 = $('#id').val(); var val2 = $('#user').val(); var url = "includes/downloads.php?id=";   $.ajax({     url: url,      type: "post",      data: { id: val1, user: val2 },     error:function(){      alert('error!');    }  }); 

}

the issue want pass "val1" value url can process on downloads.php page because have multiple items in loop, i'm trying figure out how pass id. reason, keeps passing first item in loop only.

this inside loop:

echo '<input type="hidden" id="id" value="'.$row['id'].'">'; echo '<input type="hidden" id="user" value="'.$user.'">'; echo '<div class="download" onclick="download()"><a href="'.$row['filename'].'.zip">download</a></div>'; 

how can that?

edit:

downloads.php page:

$id = $_post['id']; $user = $_post['user'];  // insert query 

your code fine. 1 thing change. note fields defined in data posted url type u defined. u can choose type either post or get.just assign url in url no need pass id it, automatically go in data.

  function download (a) {   var val1 = $('#id'+a).val();  var val2 = $('#user'+a).val();  var url = "includes/downloads.php";   $.ajax({   url: url,    type: "post",    data: { 'id' : val1, 'user' : val2 },   error:function(){   alert('error!');     }   });     echo '<input type="hidden" id="id.$i." value="'.$row['id'].'">';   echo '<input type="hidden" id="user.$i." value="'.$user.'">';   echo '<div class="download" onclick="download(.$i.)"><a href="'.$row['filename'].'.zip">download</a></div>'; 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -