Подаване на променлива от PHP към Jquery

Jorko

Registered
Привет ползвам една upload форм с ajax,php,jquery обаче проблем ми е следния след като кача изображенито незнам как да подам променливата с името на изображението към jquery-to в индекс

ето и файловете който ползвам

PHP:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Basic Ajax and PHP tutorial</title>
<script type="text/javascript" src="Ajaxfileupload-jquery-1.3.2.js" ></script>
<script type="text/javascript" src="ajaxupload.3.5.js" ></script>
<link rel="stylesheet" type="text/css" href="Ajaxfile-upload.css" />
<script type="text/javascript" >
	$(function(){
		var btnUpload=$('#me');
		var mestatus=$('#mestatus');
		var files=$('#files');
		new AjaxUpload(btnUpload, {
			action: 'uploadPhoto.php',
			name: 'uploadfile',
			onSubmit: function(file, ext){
				 if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                    // extension is not allowed 
					mestatus.text('Файлът който се опитвате да качите е в непозволен формат.');
					return false;
				}
				mestatus.html('<img src="ajax-loader.gif" height="16" width="16">');
			},
			onComplete: function(file, response){
				//On completion clear the status
				mestatus.text('Успешно добавихте изображението.');
				//On completion clear the status
				files.html('');
				//Add uploaded file to list
				if(response==="success"){
					$("#me").hide();
					$("#formats").hide();
				} else{
					$("#me").show();
					$("#formats").show();
					$('<li></li>').appendTo('#files').text(file).addClass('error');
				}
			}
		});
		
	});
</script>

</head>

<body>
              <div id="me" class="styleall"><span style=" cursor:pointer;">Качи</span></span></div><span id="mestatus" ></span>
			  <span id="formats" style="font-weight: lighter;color: gray;"><i>Позволени формати: jpg, jpeg, png, gif</i></span>


</body>
</html>
- index.php


PHP:
<?php
$uploaddir = './images/'; 
$allowed = array("jpeg","jpg","png","gif");
$filee = $_FILES['uploadfile']['name'];
$fileext = strtolower(substr($filee,strrpos($filee,".")+1));
$file_name= md5(uniqid()); 
$file = $uploaddir . $file_name; 
$file = ''.$file.'.'.$fileext.'';
 
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
			$source_pic = $file;
			$destination_pic = $file;
			$max_width = 64;
			$max_height = 64;

			$src = imagecreatefromjpeg($source_pic);
			list($width,$height)=getimagesize($source_pic);

			$x_ratio = $max_width / $width;
			$y_ratio = $max_height / $height;

			if( ($width <= $max_width) && ($height <= $max_height) ){
				$tn_width = $width;
				$tn_height = $height;
			}elseif (($x_ratio * $height) < $max_height){
				$tn_height = ceil($x_ratio * $height);
				$tn_width = $max_width;
			}else{
				$tn_width = ceil($y_ratio * $width);
				$tn_height = $max_height;
			}

			$tmp=imagecreatetruecolor($tn_width,$tn_height);
			imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);

			imagejpeg($tmp,$destination_pic,100);
			imagedestroy($src);
			imagedestroy($tmp);
  echo "success"; 
} else {
	echo "error";
}
?>
uploadPhoto.php

Понеже съм нов в използването на ajax с php ще съм благодарен за помоща
 
Ако искаш да върнеш в отговора на php пътя заедно със success го направи така.
В php файла:
Код:
$arr = array('success'=>1,'path'=>$path);
echo json_encode($arr);
В js:
Код:
if(response.success == 1){
alert(response.path);
....
Гледам, че използваш някакви готови кодове май и предполагам, че си е предвидено в тях. Виж какво показва alert(file);
 
С json не стана ли? Ето ти едно просто примерче какво имах предвид.
html :arrow:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('a#upload').click(function(){
$.post('j.php', function(data) {
var object = $.parseJSON(data);
if(object.success==1){
$('div#result').html(object.success+' --- '+object.path);
}else{
$('div#result').html(object.success+' --- '+object.path);
}
});
return false;
});
});
</script>
<a id="upload" href="javascript:;">upload</a>
<div id="result"></div>
j.php :arrow:
<?php
$upload = true;//false
if ($upload) {
$response = array('success'=>1,'path'=>'url success');
echo json_encode($response);
} else {
$response = array('success'=>0,'path'=>'url error');
echo json_encode($response);
}
?>
Сменяй true на false
 

Back
Горе