i m use php which print my table in which first column is sr.no but in which i want to store value of exam_id.
how can i get this Exam_id value by javascript
foreach ($data as $key) {
echo "<tr>";
print'
<td class="contact" id="exam_id" name=' . $key['exam_id'] ." width="5%" value='.$key['date'].'>' . $i . '</td>
<td class="contact" width="20%">' . $key['date'] . '/' . $key['month'] . '/' . $key['year'] . '</td>
<td class="contact" width="30%">' . $key['Title'] . '</td>
<td class="contact" width="20%">' . $key['Branch'] . '</td>
<td class="contact" width="6%">' . $key['Sem'] . '</td>
<td class="contact" width="24%" ><a onclick="javascript:create_result();" href="#">Create Result</a></td>';
$i++;
echo "</tr>";
}
javacript is
<script type="text/javascript">
function create_result() {
var exam_id = document.getElementById('exam_id').value
$.ajax({type:'POST', url: 'result/create_result',data:exam_id, success: function(response) {
$('#content').html(response);
}});
return false;
}
</script>
Firstly you need to clean up your PHP code. The syntax is a bit off, and is very hard to read.
Once you have managed to get the PHP to output the HTML correctly you need to change to JavaScript you are using to
gettheexam_idvalue.As @MarcB said, you have duplicate IDs in your HTML, since you will be running the PHP snippet multiple times as it is a foreach loop. That will result in you having many cells with the id
exam_id.You need to be able to reference a specific cell in order to get the value of
exam_id. Since the id of the cell in question is not unique, it should be converted to a class.<td class="contact exam_id" name... </td>Then you need to find a way to uniquely identify each row. This could be done by attaching the exam_id of each row into the row (tr) declaration like so,
"<tr id='exam-" . $key['exam_id'] . "' >";(This would replace line 2 of your PHP)Now you have to find a way of identifying which row you wish to have as your context before you can select the value.