473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to Put PHP Code INSIDE JavaScript Conditional Statements?

nicebasic
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:

Expand|Select|Wrap|Line Numbers
  1. 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".
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. <!--
  5. function confirmation() {
  6.     var answer = confirm("Delete this file? \n <?php echo $_GET["fname"]; ?>")
  7.     if (answer){
  8.         alert("File was deleted!");
  9.         <?php unlink($_GET["fname"]); ?>
  10.         window.location = "list.php";
  11.     }
  12.     else{
  13.         window.location = "list.php";
  14.     }
  15. }
  16. //-->
  17. </script>
  18. </head>
  19. <body>
  20. <form>
  21. <input type="button" onclick="confirmation()" value="Delete">
  22. <input type="button" onclick=window.location="list.php" value="Cancel">
  23. </form>
  24. </body>
  25. </html>

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

Expand|Select|Wrap|Line Numbers
  1. 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!
Apr 9 '12 #1
8 30105
Monomachus
127 Expert 100+
All the lines are executed. Because it is a php script it runs all at once. So basically it goes until your line
Expand|Select|Wrap|Line Numbers
  1. <?php unlink($_GET["fname"]); ?>
  2.  
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

Expand|Select|Wrap|Line Numbers
  1. <?php unlink($_GET["fname"]); ?>
  2.  
Apr 9 '12 #2
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?
Apr 9 '12 #3
Rabbit
12,516 Expert Mod 8TB
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.
Apr 9 '12 #4
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.
Apr 10 '12 #5
Rabbit
12,516 Expert Mod 8TB
JQuery is a javascript library. It does not remove the need to separate your pages.
Apr 10 '12 #6
Line 9 is not required, line 10 should redirect to:

Expand|Select|Wrap|Line Numbers
  1. delete.php?fname=blue.jpg
not list.php

all else seems ok
May 7 '12 #7
[/code]
<script type="text/javascript">
var dlt = confirm("do you want to delete this file ?");
if(dlt == true)
{
window.location.href="delete.php";
}
else
{
window.location.href="list.php";
}
</script>


[/code]
May 8 '12 #8
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>
May 8 '12 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Max | last post by:
Hi, I'm a perl programmer and am trying to learn PHP. So far I have figured out most of the differences, but have not been able to find out how to do the following: When running through a...
3
by: Patrice | last post by:
Hi, I need to do multi-conditional statements like below, but this error is displayed : Expected 'End' /myFilepath, line x else response.write(arrCorpo(sparam,sdiv)) end if I don't...
1
by: Matt | last post by:
Is it possible to put XSL instructions inside JavaScript? For example, pop up a message box with the xml value: alert(value in xml); Approach #1: <script type="text/javascript">...
1
by: Frederik S | last post by:
Hi all, Is it possble to do a HTTP post request inside JavaScript? - That is I don't want to use a HTML FORM, but need a JavaScript to post a value to a PHP script not encoding the parameters...
3
by: Sergej Andrejev | last post by:
Not long ago I was asked to give PHP lections to some private IT school. I'm on second lection now, and will be teaching my :) students conditional statements (if..else and switch) and loops (while...
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
2
by: prasad | last post by:
hai all users i resquesting the conditional statements examples. thaniking you inagaprasdreddy
1
by: Daniel Andrade | last post by:
Hello, How can I write a C# code inside a javascript code, before body tag, as we wrote in classic asp? (I have a code file separated with a lot of C# code) I would like anything like this: ...
3
by: swethak | last post by:
Hi, i am getting the problem with php code inside javascript function in Internet Explorer.Here is my javascript and php code. function addPoints() { <? ...
9
by: jayvi | last post by:
I used this code inside of a javascript file to switch out an 'add to cart' button: var addDisplay = document.getElementById('cart-add'); addDisplay.innerHTML = "<a class='item_add'...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.