-1

how to write JQuery full function in a PHP variable so that it gets called where ever I place the variable. And the variable has call back json text and php variables also. So how can this be possible or there is any other way to do this.

 <?php
 $somevar='<div class="test"><script>document.ready(function(){$.ajax}' ;

 $body="<div>$somevar</div>";

 echo $body;

 ?>
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
  • 1
    What is wrong with your current code? – Joren Dec 21 '13 at 06:09
  • 2
    The PHP will be written to the page just like HTML, so you can do exactly what you are doing and it should work. You should probably close the script tag – Leeish Dec 21 '13 at 06:09
  • Please read about the [relation between PHP and Javascript](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – John Dvorak Dec 21 '13 at 06:12
  • 1
    and use the correct syntax on the jQuery function and include jQuery `'` – mplungjan Dec 21 '13 at 06:12
  • It should be --> `$(document).ready(function(){` – Bryan Elliott Dec 21 '13 at 06:16

4 Answers4

5

When I have to write large html or js in PHP, I like to use Heredoc syntax

$variable = <<<EOT
  <script>
  $(document).ready(function(){
    //your stuff
  });
  </script>
EOT;

Read the reference http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Note in the manual

It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline.

If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.

Heredocs can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables.

UPDATE Based on the OP comment

<?php
$data = 'test';
$variable = <<<EOT
  <script>
  $(document).ready(function(){
    alert('$data');
  });
  </script>
 EOT;
 ?>
 <html>
   <head>
     <title><?php echo $data; ?></title>
    <?php echo $variable; ?>
    </head>
    <body>
      <div><?php echo "The alert showed $data";?></div>
    <body>
  </html>
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
  • ya but it also has php variables and i get an error in syntax where the above statement requires ) after argument list .actually i did not put the full code above it has everything completed is also there. – user3070287 Dec 21 '13 at 06:42
  • you can write php variables into the Heredoc syntax – Emilio Gort Dec 21 '13 at 06:45
1

Your current code is not that far off. You need a close script tag in there:

<?php
$somevar='<script>alert("something");</script>';
$body="<div>$somevar</div>";
echo $body;
?>

With the above code, you can put $somevar in a valid location in your output, meaning in between two other html tags, and your javascript function will print out. In my case it will say 'something', via alert box, every time it is in the code

loushou
  • 1,462
  • 9
  • 15
  • ya but it also has php variables and i get an error in syntax where the above statement requires ) after argument list .actually i did not put the full code above it has everything completed is also there – user3070287 Dec 21 '13 at 06:42
  • ok if it has php variables, then do this: `$somevar = '` – loushou Dec 21 '13 at 07:05
0

when i need call variable into variable seprate with '.$var.'

$var1=some codes;

$var2=codes'.$var1.'other codes;

 <?php
 $somevar='<div class="test"><script>document.ready(function(){$.ajax}' ;

 $body='<div>'.$somevar.'</div>';

 echo $body;

 ?>
Mohammad Fanni
  • 4,095
  • 3
  • 28
  • 52
0

Consider wrapping your javascript in PHP output buffer

<?php ob_start(); ?>
$(document).ready(function() {
    var x = <?php echo $someVar; ?>;
});
<?php $script = ob_get_contents(); ob_end_clean(); ?>
Andy Librian
  • 911
  • 5
  • 12