0

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)?

3
  • 1
    '#clientid' is just a string. Javascript does not have widely-supported string interpolation. Commented Jan 15, 2016 at 21:43
  • Please clarify what you mean by "crashes jQuery". Commented Jan 15, 2016 at 21:52
  • My page won't load clients when I made that change. Commented Jan 15, 2016 at 21:53

2 Answers 2

1

$('#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>

Sign up to request clarification or add additional context in comments.

2 Comments

It works for me. Something else must be causing a problem. Try outputting '#'+clientid to see what the value is.
It's outputting now. Thanks for your help, showdev and divix.
1

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

7 Comments

Your suggestion of concatenation causes JQuery to crash.
I don't know how to show errors in JQuery. I've just started using it.
how do you know it's crashed then?
As for the way I'm setting the ids for the textareas, it's the only way I can think of to make sure that the right text gets put with the right client when I write the record to the db. I'm just matching it up with the clientid.
If you are using Chrome click F12 and open Console tab and then check the message (by clickin on the filename on the right) Diagnose your problem first, cause the solution for the clientid is correct.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.