2015-12-02 126 views
1

我有一个页面,可以上传图像并剪裁图像(这样做我使用了this plugin),然后将此剪裁的结果保存在服务器中。无法使用ajax到达php文件

看文档后,我想这:

JQUERY

var zis = //some div where i uploaded the image; 
$('.export').click(function() { 
    var imageData = $('.image-editor').cropit('export'); 
    zis.find('#img_val').val(imageData); 
    zis.find('.salvaImmagine').click(); 
}); 

PHP

public function immagine_profilo(){ 
    if (isset($_POST['crop'])){ 

     var_dump($_POST['img_val']); 
     // Get the base-64 string from data 
     $filteredData = substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1); 

     // Decode the string 
     $unencodedData = base64_decode($filteredData); 

     // image new name 
     $newfilename = 'images/immProf' . $_SESSION['auth'] . '.png'; 
     file_put_contents($newfilename, $unencodedData); 

     // saves the path into a database 
     $query = "UPDATE users SET pic = '{$newfilename}' 
        WHERE id = {$_SESSION['auth']}"; 
     $result = mysqli_query($_SESSION['connessione'], $query); 
     return // function to retrive the image; 
    } 
} 

到目前为止好,图像保存在服务器上,它的路径是保存同样,只有一个问题仍然存在:页面需要重新加载才能看到图像更改,我们可以做得更好:在没有页面重新加载的情况下更新图像。

所以,我在网上搜索,发现关于AJAX,我想出了这一段时间之后:

JQUERY

$('.export').click(function() { 

    var imageData = $('.image-editor').cropit('export'); 
    zis.find('#img_val').val(imageData); 

    lightbox(false); 

    zis.find('.salvaImmagine').click(); 
}); 
$('.salvaImmagine').on('click', function(event) { 
    event.preventDefault(); 
    var imageData = $('.cop').cropit('export'); 
    $.ajax({ 
     url: 'lib/ottieniCose.php', 
     type: 'POST', 
     dataType: 'html', 
     data: { 
      crop: 'Salva', 
      crop_cop: 'Salva', 
      img_val: imageData 
     }, 
    }) 
    .done(function(response) { 
     console.log("success"); 
     $(".copertina").css('background-image', 'url(' + response + ')'); 
    }) 
    .fail(function() { 
     console.log("error"); 
    }) 
}); 

PHP(LIB/ottieniCose.php)

//rquire bla bla bla 
if (isset($_POST['crop']) || isset($_POST['crop_cop'])) { 
    var_dump("test"); 
    //calls the function to save the image 
    $login->immagine_profilo(); 
} 

现在,我得到绝对没有结果,图像不保存,路径不保存,php页面似乎根本不被调用(虽然没有404错误),但图像正在裁剪,我知道,由外观纳入代码。

我也尝试更改POST方法在Ajax中的GET方法,但我得到错误414,任何帮助?

的HTML

<div id="lightbox"> 
    <div class="image-editor"> 
     <div class="scegliImm"> 
      <p>Scegli un'imagine</p> 
     </div> 
     <input type="file" class="cropit-image-input"> 
     <div class="cropit-image-preview-container"> 
      <div class="cropit-image-preview" style="background-image: url(<?php print $login->get_profile_pic() ?>)"></div> 
     </div> 
     <div class="image-size-label"> 
      <p>Nessuna immagine selzionata,<br> seleziona un'immagine.<br>P.S. Puoi trascinare l'immagine<br> nuova sopra quella vecchia</p> 
      <div class="esci"> 
       <p>Esci</p> 
      </div> 
     </div> 
     <div class="neh"> 
      <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
       <div> 
        <input type="hidden" name="img_val" id="img_val" value=""> 
        <input type="submit" name="crop" value="Salva" class="salvaImmagine"> 
       </div> 
      </form> 
      <div id="salvaImma"> 
       <input type="range" class="cropit-image-zoom-input"> 
       <button class="export">Salva</button> 
      </div> 
     </div> 
    </div> 
</div> 

回答

0

尽量给绝对路径,Ajax调用,或者如果你的网站的根目录的lib文件夹,然后下的lib之前用反斜杠

so your url will be :- /lib/ottieniCose.php 

它可以帮助你。

+0

没有帮助,没有改变 – user3847141

+0

你能给我从您的网站根目录lib文件夹的路径吗? –

+0

我目前在本地工作所以根(localhost)/ lib/ – user3847141