V
V
Vyacheslav Yashnikov2016-04-13 20:35:55
PHP
Vyacheslav Yashnikov, 2016-04-13 20:35:55

How to pass multiple properties to a javascript object via echo in php?

Good evening, tell me how can I display several properties of a javaScript object via echo in php?
I send data to the server via ajax:

$.ajax({
      url: "./form/formprocessor.php",
      type: 'POST',
      data: payload,
      dataType: 'json',
      crossDomain: true
    }).done(function() {
      data.success = true;
      afterSubmit(data);
    }).fail(function() {
      afterSubmit(data);
    });

in case of successful sending via echo, echo '{"success":true}' is passed to the data object;
in case of unsuccessful sending echo '{"success":false}';
How can I make it show the property CaptchaError: false ?
echo '{"success":true}'; - first property
echo '{"CaptchaError":false}'; - the second property, if I do this (two echoes) then the second property is not added to the object.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey delphinpro, 2016-04-13
@VyacheslavY

$response = [
  'success' => true,
  'CaptchaError' => false,
];
echo json_encode($response);

// старый синтаксис
$response = array(
  'success' => true,
  'CaptchaError' => false,
);
echo json_encode($response);

or right on the forehead
echo '{
  "success" : true,
  "CaptchaError" : false
}';

Although this is wildly inconvenient, and I do not recommend writing like that.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question