jQuery & PHP to delete a file - php

Got this following code:
HTML:
Delete
JS:
$('.deleteFile').click(function(){
if (confirm("Delete file ?")){
$imagefile = $(this).attr('id');
$.ajax({
type: 'POST',
data: {
action: 'deleteFile',
imagefile: $imagefile,
},
url: 'assets/php/deleteFile.php',
success: function(msg) {
alert($imagefile);
}
})
}
})
PHP:
if($_POST["action"]=="deleteFile") {
$imagefile = $_POST['imagefile'];
unlink("files/1/".$imagefile);
}
I do not why it does not work. My file is still here...
Could you please help me?
Thanks.

HTML:
Delete
PHP
unlink($SERVER['DOCUMENT_ROOT']."/files/1/".$imagefile);

You need to open the php declaration before writing a php variable:
<?php echo $entry ?>
Your html code must look like:
Delete
You need to save the file as php.

I think you need to echo $entry variable here
Delete

Related

script stop working when I load it inside a div

I got this function in my html for a simply validation...
<script>
function pwdcheck(dataPwd) {
var parametros = {
"dataPwd" : dataPwd
};
$.ajax({
data: parametros,
url: 'check.php',
type: 'post',
beforeSend: function () {
$("#infomsg").html("checking, please wait...");
},
success: function (response) {
$("#infomsg").html(response);
}
});
}
and this check.php
<?php
$clave = 'cocacola';
if(trim($_POST['dataPwd'])==$clave) {
// redirect to some page
}else{
echo "Incorrect Password!";
}
?>
and the problem is that when I use it directly from this first html that I showed you... all works perfect, but when I load this html inside a div (#section), its stop working displaying the "checking, please wait..." message.
Someone could please help me?! Thanks!
Well I solved the problem! Thanks to all! you gave me ideas to try some things step by step...
The problem was that the php file was in another directory... and ajax seems to arrive in one way but php cant find a way to return I think... php echo dont cross directories :P
What I tried was this.
I had main.html who loads in his #section validate.html who was the one who has the ajax inside to communicate with check.php, validate.html and check.php were in another directory.
The solution was only moving the check.php to the same directory than main.html, and it worked even keeping validate.html in the other directory... weird no?! at least for me that I dont know so much :P
Thanks again to all, Leandro.
<script>
function pwdcheck(dataPwd) {
var parametros = {
"dataPwd" : dataPwd
};
$.ajax({
data: parametros,
url: 'check.php',
type: 'post',
dataType:'html',
beforeSend: function () {
$("#infomsg").html("checking, please wait...");
},
success: function (response) {
$("#infomsg").html(response);
}
});
}
</script>

Execute php using jquery post

I've tried to go to php file using jquery.
Here is my code.
This is index.php
$.post('test.php',data,function(json){},'json');
This is test.php
//set session variable from passed data
$_SESSION['data1'] = $_POST['data1'];
<script>
window.open('test1.php','_blank');
</script>
This is test1.php
echo $_SESSION['data1'];
But this code is not working.
I want to pass data from index.php to test1.php.
How can I do this? I don't want to use GET method because of too long url.
Anyhelp would be appreciate.
I am not quite clear from you explanation right now. But I am here trying to resolve you problem as you can use the jquery post method as follows :
$.post('test1.php',{param1:value1,param2=value2,...},function(data){
//Here you can take action as per data return from the page or can add simple action like redirecting or other
});
Here is a simple example of register :
$.post('', $("#register_form").serialize(), function(data) {
if (data === '1') {
bootbox.alert("You have registered successfully.", function() {
document.location.href = base_url + '';
});
} else if (data === '0') {
bootbox.alert("Error submitting records");
} else {
bootbox.alert(data);
}
$("#user_register_button").button("reset");
});
Try this:
$.ajax({
url: 'test.php',
type: 'POST',
data: {
myData : 'somevalue'
},
success: function(response){ // response from test.php
// do your stuff here
}
});
test.php
$myData = $_REQUEST['myData'];
// do your stuff here
I like use jQuery post a url like this.
$('form').on('submit', function(e) {
e.preventDefault();
var $this = $(this);
$.ajax({
url: $this.attr('action'),
method: $this.attr('method'),
data: $this.serializeArray(),
success: function(response) {
console.log(response);
}
})
});
I you a beginner, you can reference this project
php-and-jQuery-messageBoard

Store ajax data to php

I have a function.I want Data passing through ajax is store in php variable.I tried below code but not work please some one help me.
function moreinfo(prodid,catid,price,type,catname) {
url2="<?php echo $this- >getUrl('compatibility/compatiblelist/moredetails'); ?>";
$j.ajax({
url:url2,
type: 'POST',
data: {"prodid": prodid},
success: function(response) {alert(console.log(response));}
});
<?php
$ms = $_POST["prodid"];
echo $ms;
?>
}
<?php ?> tags only work when file name must be .php.
may you are using that process in .js file kindly change the ext
consider separating the file, .js and .php then you can include .js file
function moreinfo(prodid,catid,price,type,catname) {
url2="something.php";
$.ajax({
url:url2,
async:false,
type:'POST',
data: {prodid: prodid},
dataType:'html',
success: function(response) {alert(console.log(response));}
});
}
In your something.php
<?php
$ms = $_POST["prodid"];
echo $ms;
?>

Need help in passing value form JS to PHP

I have some problem with passing value from javascript file to php file.
I make some calculations in JS, and after that i passing it to php.
This is a code of JS:
var price=100;// for example
$.ajax({
type: "POST",
url: "SecureRide.php",
data: { calculatedPrice: price }
});
And this is a code of PHP:
<?php
session_name("ridePrice");
session_start();
$_SESSION["calculatedPrice"]=$_POST["calculatedPrice"];
echo $_SESSION["calculatedPrice"];
?>
So what i do is a simple Ajax function that passes the value to php, very simple, but doesn't work!
When I do echo function there's nothing there.
Maybe there are another way to solve it?
Thank you very much!
Note : if you are put your ajax code in function and than call function from the "$(document).ready(function(){" then your code run perfectly
I write code in your .js file :
$(document).ready(function(){
functionName(price);
});
function functionName(price){
$.ajax({
url: 'SecureRide.php',
data:{calculatedPrice: price},
type: 'POST',
success:function(data)
{
alert(data);
},
error:function(data)
{
alert("mistake in your code");
}
})
}
And Code For PHP file for get .JS File Data
if(isset($_POST['calculatedPrice']) && !empty($_POST['calculatedPrice']))
{
$calculatedPrice1=$_POST['calculatedPrice'];
}

jQuery $.ajax and php to delete a file

I'm trying to delete a file with jQuery's $.ajax and php. I have the following.
/js/whatever.js
$('.deleteimage').live('click', function() {
$imagefile = 'http://domain.com/images/1/whatever.jpg';
$imagethumb = 'http://domain.com/images/1/thumbnail/whatever.jpg';
$.ajax({
type: 'POST',
data: {
action: 'deleteimage',
imagefile: $imagefile,
imagethumb: $imagethumb,
},
url: 'script.php',
success: function(msg) {
alert(msg);
}
})
})
/php/script.php
<?php
if($_GET["action"]=="deleteimage")
{
$imagefile = $_REQUEST['imagefile'];
$imagethumb = $_REQUEST['imagethumb'];
$imagefileend = '../images'.end(explode('images',$imagefile)); //This will get me the path to the image ../images/1/whatever.jpg without the domain which is the correct path to the file. I tried that path directly and it deleted the file.
$imagethumbend = '../images'.end(explode('images',$imagethumb));
unlink($imagefileend);
unlink($imagethumbend);
}
?>
All path are correct. In firebug i see the post variables are being sent correctly to script.php, however files are not being deleted. What am i doing wrong.
In jQuery you use POST but in PHP you use GET. Change to if($_POST["action"]=="deleteimage") and please don't use $_REQUEST but $_POST

Categories