How to Put PHP Code INSIDE JavaScript Conditional Statements?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nicebasic
    New Member
    • Aug 2010
    • 91

    How to Put PHP Code INSIDE JavaScript Conditional Statements?

    I have the following JavaScript HTML page. Its name is "Delete.php " and I'm trying to write a PHP file that can delete a file passed to it as an argument.

    For example, if you run this code:

    Code:
    delete.php?fname=blue.jpg
    it's supposed to delete "blue.jpg", but only if you press the "Delete Button".

    Unfortunately, when the file is executed, without asking anything, it deletes the file given as the argument.

    How can I make a conditional statement in this JavaScript function to make it possible to choose what to do?

    If the user presses "Cancel" Button, nothing should happen and the page should be redirected to "list.php".

    If the user presses "Delete" Button, he/she will see a confirmation message. If he presses "OK", the file should be deleted, but if he pressed "Cancel", nothing should be deleted and the page should be redirected to "list.php".
    Code:
    <html>
    <head>
    <script type="text/javascript">
    <!--
    function confirmation() {
    	var answer = confirm("Delete this file? \n <?php echo $_GET["fname"]; ?>")
    	if (answer){
    		alert("File was deleted!");
    		<?php unlink($_GET["fname"]); ?>
    		window.location = "list.php";
    	}
    	else{
    		window.location = "list.php";
    	}
    }
    //-->
    </script>
    </head>
    <body>
    <form>
    <input type="button" onclick="confirmation()" value="Delete">
    <input type="button" onclick=window.location="list.php" value="Cancel">
    </form>
    </body>
    </html>

    I have even tried replacing Line 9 of the above code with this:

    Code:
    document.write("<?php unlink($_GET[fname]); ?>");
    but it was not successful either.

    Can anyone suggest a solution to this problem?

    Any help will be appreciated!
  • Monomachus
    Recognized Expert New Member
    • Apr 2008
    • 127

    #2
    All the lines are executed. Because it is a php script it runs all at once. So basically it goes until your line
    Code:
    <?php unlink($_GET["fname"]); ?>
    is executed and it deletes the file.

    So if you want to delete in the same page I would basically make an ajax request to another script which does just deletion deleteFile.php for example.
    And I would put it instead of

    Code:
    <?php unlink($_GET["fname"]); ?>

    Comment

    • nicebasic
      New Member
      • Aug 2010
      • 91

      #3
      I don't get what you mean.

      Do you mean we should put some AJAX code in Delete.php?

      Could you please supply a sample code?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        He's saying that you need to separate your confirmation page from your PHP delete page. You can't have them together because javascript can't interact with PHP and vice versa.

        Comment

        • nicebasic
          New Member
          • Aug 2010
          • 91

          #5
          Can I use JQuery in my Delete.php to do this? I know nothing about JQuery. I know that it's really powerful. Does it need any file to be added or referenced in the main page?

          I've seen some pages that have "JQuery.js" added to them.

          If you don't mind, please give me some hints on how to do this.

          Thank you very much.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            JQuery is a javascript library. It does not remove the need to separate your pages.

            Comment

            • wbevan20
              New Member
              • Feb 2012
              • 9

              #7
              Line 9 is not required, line 10 should redirect to:

              Code:
              delete.php?fname=blue.jpg
              not list.php

              all else seems ok

              Comment

              • Bharat383
                New Member
                • Aug 2011
                • 93

                #8
                [/code]
                <script type="text/javascript">
                var dlt = confirm("do you want to delete this file ?");
                if(dlt == true)
                {
                window.location .href="delete.p hp";
                }
                else
                {
                window.location .href="list.php ";
                }
                </script>


                [/code]
                Last edited by Niheel; May 8 '12, 04:18 AM.

                Comment

                • saravana 786
                  New Member
                  • Apr 2012
                  • 4

                  #9
                  you can try to write the value before the java script to assign the variable

                  eg,
                  <?php
                  $abc="sasa";
                  ?>
                  <script type="text/javascript">
                  var abc="<?php echo $abc;?>"
                  document.write( abc);
                  </script>

                  Comment

                  Working...