V
V
Vladimir Golub2015-05-15 18:23:52
JavaScript
Vladimir Golub, 2015-05-15 18:23:52

Question about the each method?

My database query selects only one record. Next, I encode it in JSON. And I pass it to the script.

Here is just the question. Do I need to use each, because I don’t seem to have an array, but one record, but I can’t access the property directly.

$.each(json.client_date,function() {
               $('#changeClientDataForm')[0].reset();
               $('#changeClientDataForm .code').val(this['code_client']);
               $('#changeClientDataForm .name').val(this['name']);
               $('#changeClientDataForm .login').val(this['login']);
               $('#changeClientDataForm .password').val(this['password']);
           });


Here, I need to display this once, how can I do without each ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-05-15
@RazerVG

So if you are sure that only one record is always selected from the database (search by key, for example), then you should rewrite the php code

$row = mysql_fetch_array($result, MYSQL_ASSOC))
echo json_encode(array("client_date" => $row));

Then in the js code it will be easier to deal with the properties of the object
$('#changeClientDataForm')[0].reset();
$('#changeClientDataForm .code').val(json.client_date.code_client);
$('#changeClientDataForm .name').val(json.client_date.name);
$('#changeClientDataForm .login').val(json.client_date.login);
$('#changeClientDataForm .password').val(json.client_date.password);

PS Do you store the password in clear text in the database?

G
GreatRash, 2015-05-15
@GreatRash

Try like this:
console.log(json.client_date[0].name);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question