2
Opa,
I have a page that runs a Setinterval of seconds in javascript, this page captures the data of Divs and sends via post, and this data sent is not fixed, sometimes, can be sent 1 or more of a.
When sending, I treat this amount of sent integers and separate it by commas, ie the file . php receives in the format registro_id = 1, 3, 4, 5
.
There in php give a explode in value and a foreach for data query and return in Json. The problem is that if more than one value is sent via javascript, the amount of response obtained in the query will be returned via json.
The json response format is:
Se enviado apenas um ID:
[{"registro":{"registro_id":"1","estado":"1","preco":"R$ 5,99","usuario_id":"18","usuario_nome" :"joao"}}]
Se enviado apenas mais de um ID:
[{"registro":{"registro_id":"1","estado":"1","preco":"R$ 5,99","usuario_id":"18","usuario_nome" :"joao"}}]
[{"registro":{"registro_id":"3","estado":"1","preco":"R$ 3,99","usuario_id":"48","usuario_nome" :"maria"}}]
The problem is that I need to receive these values in my javascript function and update the page fields, but, are received different values for fields with different classes, I tried so:
usuario_id = $('.user-id-'+ registro_id);
usuario_nome = $('.user-name-'+ registro_id);
The fields are with the appropriate classes, the variable registro_id
is the same received in the setinterval function, but, this only works if only one item is sent to . php, if two are sent, nothing happens.
Summary example:
DIVS
<div class="content_item" id="registro_1" title="registro_1">
<div class="user-name-1" id="coupon-time"></div>
<div class="user-id-1" id="coupon-price" style="text-align:center"></div>
</div>
<div class="content_item" id="registro_2" title="registro_2">
<div class="user-name-2" id="coupon-time"></div>
<div class="user-id-2" id="coupon-price" style="text-align:center"></div>
</div>
Javascript function
function atualizaregistro() {
var registros = '';
$.ajaxSetup({
cache: false
});
$('.content_item').each(function() {
var new_value = $(this).attr('title');
if (registros != '') registros = registros + ',';
registros = registros + new_value;
});
if (registros) {
$.ajax({
url: '_files/_update_information.php',
dataType: 'json',
type: 'POST',
data: 'registro_id=' + registros,
global: false,
success: function(data) {
$.each(data, function(j, item) {
var usuario_id = item.registro.usuario_id;
var usuario_nome = item.registro.usuario_nome;
field_user_id = $('.user-id-' + registro_id);
field_user_name = $('.user-name-' + registro_id);
field_user_id.text(usuario_id);
field_user_name.text(usuario_nome);
});
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
}
}
PHP
$registros = explode(',',$_POST['registro_id']);
foreach($registros as $registro_id)
{
...Código Consulta BD
$temp = '{"registro":{"registro_id":"'.$registro_id.'","estado":"1","preco":"'.$preco.'","usuario_id":"'.$usuario_id.'","usuario_nome":"'.$usuario_nome.'"}}';
}
echo "[".$temp."]";
Can anyone help?
When you send two items to PHP, how do you distinguish them in Javascript and HTML? Each one has its key as
user-name
or each item hasuser-name
anduser-id
?– Sergio
In html each div has the fields with user-name-ID and user-id-ID, this ID is the value sent to javascript, and the same query in php
– sNniffer
So the best thing would be to create a JSON in javascript with these Ids... how are you getting these values in Javascript? You can display an HTML example with two items?
– Sergio
Yes, I’ll make an example, please, just a moment
– sNniffer
@Sergio I added the short example of the code, directly in the question
– sNniffer