agregar valor a una identificación dinámica

in my PHP code, I have tag which ID is the ID from the row in the database

<h1 id="totalValue'.$row['xID'].'">'."$row[xValue]".'</h1>

I have an anchor tag, which when clicked, will send the $row['xID'] a través de un onclick función javascript increment(this.id)

Now the problem I am facing is, when the value is sent to a php file to be incremented in the database and returned, I can't seem to target the response ID, which is the <h1> above, with the dynamic ID.

Is there a way to do this? Thank you

Esta es la función javascript:

function increment(id) {

    if (ajax) {

        ajax.open('get', 'update.php?xv=' + encodeURIComponent(id));

        ajax.onreadystatechange = handle_xvalue;

        ajax.send(null);

    }
    else {
        // can't use ajax
    }
} 

// hanlde response from php script
function handle_xvalue() {
    // if everything ok
    if ( (ajax.readyState == 4) && (ajax.status == 200) ) {

        document.getElementById('totalValue'+id).innerHTML = ajax.responseText;
    }
} 

Este es el evento onclick:

<a href="#" id="'.$row['xID'].'" name="xvalue" onclick="increment(this.id);"><span>+1</span></a>

preguntado el 10 de marzo de 12 a las 14:03

Can you add your js function for the onClick event please? And didn't really get your problem. -

Basically, when the link is clicked, it sends the xID through JavaScript to a PHP file, where the x field in the table (queried using the xID) is incremented using an UPDATE statement. When returned, I would like to target the h1 tag using the JavaScript response, with the updated value. But I am having trouble sending the response to the correct h1 because the ID of the h1 is dynamic. The id of the h1 is "totalValue + $row[xID]" e.g. totalValue12, where the 12 is from the database id column -

1 Respuestas

It looks like the function handle_xvalue() is not getting that ID from anywhere.

You can try passing that ID to that function

function handle_xvalue(id)
{
 //do stuff
}

respondido 12 mar '12, 03:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.