PHP inside JavaScript -


i have 1 line of javascript i'd appreciate with.

req.open("get", 'update.php?id=<?php $id ?>', true); 

where $id = 12345, trying contents of update.php?id=12345.

using php inside javascript doesn't seem working me.

any suggestions?

first, make sure inside php file, or have server configured process whatever file extension using php.

then, can echo data directly javascript. best compatibility , avoid potential xss vulnerabilities, json-encode data.

req.open('get', 'update.php?id=' + <?php echo json_encode($id); ?>, true); 

personally, prefer have block of variables assigned on php. keeps javascript cleaner.

<?php     $options = new stdclass();     $options->id = 12345;     $options->dinnerselection = 'pizza';      echo 'var options = ', json_encode($options), ';' ?>  // later on in js... req.open('get', 'update.php?id=' + options.id, true); 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -