Reputation: 490
I'm loading multiple clients on a page, each record with its own <textarea>
. I set each <textarea>
with the client's clientid.
if(isset($_POST['loaddata'])){
try{
$stmt = $db->prepare('SELECT clientid, fname, lname
FROM clients
WHERE memberid = :memberid
AND groupid = :groupid
ORDER BY lname');
$stmt->bindValue(':memberid', $_SESSION["memberid"], PDO::PARAM_INT);
$stmt->bindValue(':groupid', $_POST['groupid'], PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll();
foreach($result as $row ) { $i++;
echo '<tr style="'.getbgc($i).'">
<td style="display:none">'.$row[0].'</td>
<td style="width:auto;">'.$row[1].'</td>
<td style="width:auto;">'.$row[2].'</td>
<td><textarea id='.$row[0].' class="form-control" rows="1"></textarea></td>
</tr>';
}
} ...
After the data loads, I test the <textarea> id's
:
$.ajax({
url : 'wsparticipation.php',
type : 'POST',
async : false,
data : {
'loaddata' : 1,
'groupid' : temp
},
success:function(re){
$('#showdata').html(re);
$('#13').html("I am 13");
$('#15').html("I am 15");
$('#10').html("I am 10");
} ...
and the textareas
show the html.
When I get ready to save the the entered data, I set the clientid's
into an array.
if(isset($_POST['getarray'])){
$stmt = $db->prepare('SELECT clientid
FROM clients
WHERE memberid = :memberid
AND groupid = :groupid
ORDER BY lname');
$stmt->bindValue(':memberid', $_SESSION["memberid"], PDO::PARAM_INT);
$stmt->bindValue(':groupid', $_POST['groupid'], PDO::PARAM_INT);
$stmt->execute();
$result = $stmt->fetchAll();
$ret = array();
foreach($result as $row ) {
$ret[] = $row['clientid'];
}
echo json_encode($ret);
exit();
}
$.ajax({
url : 'wsparticipation.php',
type : 'POST',
datatype : 'JSON',
data : {
'getarray' : 1,
'groupid' : temp
},
success:function(re){
data = $.parseJSON(re);
$.each(data, function(i, clientid) {
alert(clientid);
$('#clientid').html("hahaha");
});
$('#2').html("Made it here with this");
}
});
The alert()
indeed shows me each of the clientid's
, but the next statement $('#clientid').html("hahaha");
does nothing. The last statement $('#2').html("Made it here with this");
puts that text in the appropriate textarea
.
So, since the alerts
are showing me the clientid's
, why am I not able to access the textarea
using the clientid
from $.each(data, function(i, clientid)
?
Upvotes: 0
Views: 38
Reputation: 29178
$('#clientid')
selects an element with the ID of "clientid".
I think you want to dynamically build the selector by concatenating the "#" symbol with your variable's value:
$('#'+clientid).html("hahaha");
Example below:
var clientid = 2;
$('#' + clientid).html('test');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="2"></textarea>
Upvotes: 1
Reputation: 1374
Change your selector from:
$('#clientid').html("hahaha");
to
$('#'+clientid).html("hahaha");
Also a side note: Don't use integers as ids, it's very bad idea and it's not really descriptive of what it is, a better choice might be: clientInfo1
, clientInfo2
.. etc or clientTextarea1
, clientTextarea2
.. etc
Upvotes: 1