0

so i need to do something that based on user's session

if(!isset($_SESSION['userID'])){
$text = "<section>
        <table>
          <tr data-ng-repeat=\"msg in msgs\"
          data-ng-click=\"function($event, ms,'lalalala')\"
              <td>blablabla</td>
          </tr>
      </table>
      </section>";

echo $text;
}

The code above keep resulting error with

Undefined variable: event

Is there anything wrong??

Foster
  • 345
  • 1
  • 5
  • 18

2 Answers2

1

PHP is probably trying to replace $event

$text = "<section>
    <table>
      <tr data-ng-repeat=\"msg in msgs\"
      data-ng-click=\"function(".'$event'.", ms,'lalalala')\"
          <td>blablabla</td>
      </tr>
  </table>
  </section>";

Ideally really you want to just have your templating done client side and a clear separation between the API and the client.

shaunhusain
  • 19,630
  • 4
  • 38
  • 51
  • @Foster there are a couple of reasons I'd say not to write into the view from the service layer. 1 it makes debugging problems more complicated instead of testing/debugging an API and independently building/testing the views you are doing both at once. 2 if you decide to consume the data with some other client (native iOS, Android, and/or desktop app) then you have to parse HTML instead of a JSON response which is easily consumable. 3 if you decide to change the server side code it's going to affect the client. – shaunhusain Apr 22 '15 at 20:52
0

Check your code - php tries to evaluate what is within double quotes, and you have what looks like a variable inside ($event)

         data-ng-click=\"function($event, ms,'lalalala')\"

Use single quotes if you don't want php to try and evaluate the string. See What is the difference between single-quoted and double-quoted strings in PHP?

$text = '<section>
    <table>
      <tr data-ng-repeat="msg in msgs"
      data-ng-click="function($event, ms,\'lalalala\')"
          <td>blablabla</td>
      </tr>
  </table>
  </section>';
Community
  • 1
  • 1
copeg
  • 8,290
  • 19
  • 28
  • data-ng-click='function($event, ms,'lalalala')'. do you mean like that sir? it is still not working – Foster Apr 22 '15 at 20:46
  • No, the entire php String that you set to the variable $text (in this case, no need to escape the double quotes inside) – copeg Apr 22 '15 at 20:47