0

I have my Javascript inside echo, but i want it to have a php also inside. I dont know if it is possible, just want to know the right way. cheers.

<?php  
      if($_GET["main"]=="add-doc"){
            echo "<script> 
                       var r=confirm(\"Press a button\");
                       if (r==true)
                       {
                           <? require_once(\"doc_add.php\"); ?> <--- PHP here
                       }
                       else
                       {
                           x=\"You pressed Cancel!\";
                       }
                       </script>";
        } 
?>
Ikong
  • 2,540
  • 4
  • 38
  • 58
  • php can output javascript without problem. just not sure what you want to accomplish – Petros Mastrantonas Jun 06 '13 at 07:35
  • possible duplicate of [Reference: Why does the PHP code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-code-in-my-javascript-not-work) – PleaseStand Jun 06 '13 at 07:37
  • @PetrosMastrantonas - i want to confirm the user before going to certain page, if it answer yes i want to require the page. any other way or suggestions i could try? – Ikong Jun 06 '13 at 08:02

4 Answers4

1

Technically is there no difference including PHP in HTML or in CSS or in JavaScript. HTML is static. CSS and JavaScript are client side dynamic languages to complement HTML. They add to HTML what PHP or any other server side language can not do server side.

That is why using PHP in JavaScript is considered an anti-pattern. CSS and JavaScript should perform actions that the server side is not able to control. All the information for those actions should therefor be client side. Maintenance is more difficult when the code of JavaScript can be found within a PHP page. Create an .js file instead, make a function call returning what is in doc_add.php and go for it.

Loek Bergman
  • 2,192
  • 20
  • 18
  • I think i understand what you are trying to say...I'll just have to get used to it then..so much to learn – Ikong Jun 06 '13 at 08:07
  • We all have, that is why the community of stackoverflow exists in the first place. :-) Reading your comment that it is a confirmation before going to that certain page I can tell you that this is pure client side JavaScript. You can for instance make use of innerHTML or unhide a div based on the action. – Loek Bergman Jun 06 '13 at 08:22
  • yup i had just made an adjustment. i did my code something like this ``. Its not inside my PHP anymore. – Ikong Jun 06 '13 at 08:28
0
if($_GET["main"]=="add-doc"){
    echo "<script>
               var r=confirm(\"Press a button\");
                       if (r==true)
                       {
                           " . require_once('doc_add.php') . "  
                       }
                       else
                       {
                           x=\"You pressed Cancel!\";
                       }
          </script>";
}
som
  • 4,650
  • 2
  • 21
  • 36
0

Just echo the additional part.

By the way, you could use ' for echo so that you don't have to escape the " in javascript. (Although " and ' quotes in PHP are slightly different, it works functionally in this case.) Or you could use ' inside the javascript.

<?php  
  if($_GET["main"]=="add-doc"){
    echo '<script> 
         var r=confirm("Press a button");
         if (r==true)
         {';
    require_once("doc_add.php");
    // Make sure the file returns valid javascript
    echo '}
         else
         {
             x="You pressed Cancel!";
         }
         </script>';
  }
kevinamadeus
  • 404
  • 2
  • 7
  • actually i did this before posting this question but my problem is the output. insted the confirm it just echo '} else { x=\"You pressed Cancel!\"; }'. Same as the other solution – Ikong Jun 06 '13 at 07:58
  • @user1999194 What is in the `doc_add.php`? – kevinamadeus Jun 06 '13 at 08:02
  • i want to confirm the user before going to certain page (here add_doc.php), if it answer yes i want to require the page, if no then nothing happens. any other way or suggestions i could try? – Ikong Jun 06 '13 at 08:04
  • @user1999194 Post your `doc_add.php` in the original question – kevinamadeus Jun 06 '13 at 08:07
0

First you need to understand that PHP is a server-side script, and javascript is a client-side script. This means that once the PHP script finishes rendering the HTML file, it cannot do any more actions.

What you can do, is dynamically render the javascript content before javascript runs.

in your case:

<?php  
      if($_GET["main"]=="add-doc"){ ?>
           <script> 
                       var r=confirm("Press a button");
                       if (r==true)
                       {
                           <? require_once(\"doc_add.php\"); ?>
                       }
                       else
                       {
                           x="You pressed Cancel!";
                       }
                       </script>
           <?php
        } 
?>
Rodik
  • 4,054
  • 26
  • 49
  • actually i did this before posting this question but my problem is the output. insted the confirm it just echo '} else { x=\"You pressed Cancel!\"; }' – Ikong Jun 06 '13 at 07:59