1

I'm trying to use my PHP response in a view and output to the console using JavaScript. I can pass the variable $e->getResponseBody()->errors['0']->detail and use it in my view, but how do I access it in JavaScript?

// My controller
try {

} catch (\SquareConnect\ApiException $e) {
    return View::make('reservations.paymentForm')->with('e', $e)->render();
}

// My view JavaScript
$(document).ready(function (e) {
    console.log(e.detail);
});

When a user submits, and an error occurs I want to redirect them back to the form view and show error in console with JavaScript (example: CVV code incorrect). Ultimately I won't be using the console but once I get that I feel confident manipulating the rest.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
Ralanyo
  • 61
  • 9
  • Possible duplicate of [How to pass variables and data from PHP to JavaScript?](https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – Randy Casburn Jan 27 '19 at 23:01
  • I'm able to access the variable in my javascript using var e = getResponseBody()->errors['0']); ?>; but it only works if there is an error. i get undefined variable if there is no error. This is because i'm resending them to the same form upon errors. But if they are first accessing the form it van't find the variable because the page loads from a different method. – Ralanyo Jan 27 '19 at 23:13
  • 2
    If you pass your PHP variables into your view template, and then within your template pass them into your JavaScript code, then when that template is interpreted and passed to the client, the JavaScript will have access to the data. I hope that answers your question. – Randy Casburn Jan 27 '19 at 23:17
  • @RandyCasburn thank you . Thats what i ended up doing and it works great when i post the form and there are errors, but when a user goes directly to the page php can't find the variables becuase they are loaded from the post method – Ralanyo Jan 27 '19 at 23:20
  • 2
    Ah, I see. So you've probably concluded (correctly so) that your PHP script that initially loads the page must pass that data and not only the PHP script you are posting the form to. – Randy Casburn Jan 27 '19 at 23:22
  • @RandyCasburn Yes. Im using the square sdk so it's not so apparent where that variable is being set and how to use it. – Ralanyo Jan 27 '19 at 23:29
  • 2
    @Ralanyo try to use the `isset()` function on the page, to verify the variable exist before display. Should fix the issue of error on the page before making a post – Jonathan K Jan 27 '19 at 23:54
  • The issett() did the trick. I don't know why i didn't think of that. Been starring at the screen too long going down too many rabbit holes. Thank you very much. – Ralanyo Jan 28 '19 at 00:38
  • 1
    You should answer your own question (its fine to do that), and mark it as accepted – Chris Jan 28 '19 at 00:42

1 Answers1

0

After much help here is the answer. I ended up not actually using javascript in the end but my answer will show it. If you pass the variable to the view, you can use it in javascript.

// My controller
try {

} catch (\SquareConnect\ApiException $e) {
return View::make('reservations.paymentForm')->with('myVariable', $myVariable)->render();

}

// My view JavaScript
$(document).ready(function () {
var myVariable = '<?php echo $myvariable ; ?>';
console.log(myVariable);
});
Ralanyo
  • 61
  • 9