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
Related
Hi this is my first time posting here and I need a bit of help, I am having problem echoing back the result from php to ajax. I want to show the filename once new canvas is created and store to server.
AJAX
$.ajax({
url: 'save_map.php',
data: { img_data:img_data },
type: 'post',
dataType: 'json',
success: function (response) {
window.location.reload();
}
From this PHP I want to print the name of new image that has been just created. I want to get the string value created in $filename and then print it.
PHP
<?php
$result = array();
$imagedata = base64_decode($_POST['img_data']);
$filename = md5(date("dmYhisA"));
//Location to where you want to created sign image
$file_name = './doc_map/'.$filename.'.png';
file_put_contents($file_name,$imagedata);
$result['status'] = 1;
$result['file_name'] = $file_name;
echo json_encode($result);
?>
You are reloading the page after a successful request.
You should use the response variable to display whatever you return back from your PHP code.
success: function (response) {
window.location.reload();
}
Try instead:
success: function (response) {
alert(response.file_name);
}
I am trying to return an image when Ajax is done, but i don't know how to include a session variable in the response, this is my code:
$(document).ready(function(){
$("#process").click(function(){
$.ajax({
type: 'POST',
url: 'process.php',
success: function (msg) {
$('#new-image').html('<img src="uploads/img/$_SESSION["sess_img"];" class="img-upload" alt="new image">')
}
});
});
});
Then, I want show the image $_SESSION["sess_img"]; within the div #new-image
You can echo that image filename in process.php like this:
<?php
// using time() as the cache buster, the image will never be cache by the browser
// to solve it, you need to add condition that will check if the image has change or not.
// or maybe you need to change the filename if it changes without adding a cache buster.
echo $_SESSION['sess_img_chofer']."?v=".time();
And in your javascript code:
$(document).ready(function(){
$("#process").click(function(){
$.ajax({
type: 'POST',
url: 'process.php',
success: function (image) {
$('#new-image').html($('<img>').attr('src', 'uploads/img/' + image).attr('class', 'img-upload'));
}
});
});
});
Actually the following function works fine, but now I need to add other variable in order to return from the php file the right statement.
function sssssss1(page) {
loading_show();
$.ajax({
type: "GET",
url: "load_data.php",
data: "page=" + page,
success: function (msg) {
$("#search").ajaxComplete(function (event, request, settings) {
loading_hide();
$("#search").html(msg);
});
}
});
}
I need to add the following two variable to be read by my php file. I have tried different solution, but nothing seem working
var form2 = document.myform2;
var dataString1 = $(form2).serialize();
How to add those variable in my existing function? Any idea?
You can send object as data,
this line:
data: "page="+page,
could be
data: {mypage:"page="+page, form2:document.myform2, dataString1:$(form2).serialize()}
and your PHP can get it like:
$page = $_GET['mypage'];
$form2 = $_GET['form2'];
$dataString = $_GET['dataString1'];
Hope it Help.
I have this ajax code
$.ajax(
{
type: "POST",
url: "getData.php",
data: ValueToPass,
cache: false,
success: function(html)
{
LastDiv.after(html);
}
});
I am new with this Ajax thing.
This code is to load getData.php file and send variables through type POST.
The variables are in this var ValueToPass = "lastid="+LastId+"&br="+br;.
Other thing this code does is return the getData.php's HTML after loading.
Probably with this. success: function(html)
How can I return this $br variable from getData.php after loading, so I can use it again through the next cycle. Cuz what happens here is that I can put the variable in the getData.php with the Ajax and working with it, but when the file getData.php is loaded, outside this file, the variable is not known(not declared). And I'm losing the counting :S
I want to return the HTML and the variable.
You can return json data in your php file like
$response = array ('br'=> $br, 'html'=> $html);
echo json_encode($response);
Here both html and data are returned.
And this to use it in your ajax callback :
success: function(data)
{
br = data.br;
LastDiv.after(data.html);
}
I'd consider setting a Session variable with the value from the $br variable passed via AJAX. Then when you call getData.php from another file or location, you can use the Session variable since session variables retain their value anywhere in the session.
You can try this to get the data from your `getData.php' :
$.ajax(
{
type: "POST",
url: "getData.php",
data: { ValueToPass: ValueToPass},
cache: false,
success: function(data)
{
LastDiv.html(data);
}
});
and in your getData.php you have to pass ValueToPass
maybe like this:
$ValueToPass = mysqli_real_escape_string($db, $_POST['ValueToPass']);
If I understand your question correctly, and if you want to return the $br variable then include it in a JSON object in the successs callback function. So, something like this (I'm not familiar enough with PHP so my PHP syntax might be incorrect):
// create JSON object
<?php
$result = array('br' => $br, 'html' => 'htmlContent);
echo json_encode($result);
?>
// return JSON object
$.ajax(
{
type: "POST",
url: "getData.php",
data: ValueToPass,
cache: false,
success: function(result)
{
var $br = result.br;
LastDiv.after(result.html);
}
});
I have the following variable in Javascript. I want to know how to pass this data to a PHP so that I can display the contents of the data once redirected.
postData = {
'dates_ranges': datesAndRanges,
'action':'build',
'output_type': output_type,
'form_html': formHtml,
'width': formBuilder.width(),
'rules':validationRules,
'theme': theme,
};
Use JQuery post method to pass data to PHP file:
$.post("/path/to/script.php", postData, function(result) {
// work with result
});
In PHP use $_POST global to get the variables:
print $_POST['dates_ranges'];
print $_POST['action'];
// ...
using jquery it goes easy & clean like this:
$.post('script.php', postData, function(response){
// process/display the server response
});
you can use:
$.post("YOUR_URL", postData, function(response) {
// handle with response
});
OR:
$.ajax({
url: YOUR_URL,
data: postData,
type: 'post',
success: function(response) {
// handle with response
}
});
And In your PHP file:
if(isset($_POST) && !empty($_POST)) {
$d = $_POST;
echo $d['date_range']; // and so more
}