2014-03-12 104 views
0

我有這樣的代碼,以導出圖像:回聲jQuery的變量到PHP

$(document).ready(function(e){ 
    var frontPlateClass = $('.choose-front-plate-size').find('a.active').parent().prop('className'), 
    // alert(frontPlateClass); 
}); 


$image = $_POST['plate_image']; 
$ebay_username = $_POST['ebay_username']; 
$decoded = base64_decode(str_replace('data:image/png;base64,', '', $image)); 
$date = date('m-d-Y', time()); 
$name = $ebay_username."-front-" . $date .".png"; 
file_put_contents("/var/makeaplate.justapplications.co.uk/cart/" . $name, $decoded); 
$full_path = "/var/wmakeaplate.justapplications.co.uk/cart/" . $name; 
imageCreateCorners($full_path, $name, 25); 
$name1 = 'cart/'.$name; 
echo '<div class="content-btns">'; 
echo "<img src='$name1' alt='image' id='front_img' />"; 
echo '<p class="download-btn"><a href="download.php?ebay_username='.$ebay_username.'&img=' . $name1 .'">Download front plate</a></p>'; 
echo '</div>'; 

正如你可以看到我有1 jQuery的變量frontPlateClass。 如何在echo中添加這個php?

+0

PHP在服務器上運行以創建HTML/JavaScript以供瀏覽器稍後運行。你到底想做什麼? –

+0

我想要將類名輸入到導出的圖像名稱中,如:「classname-image.png」 –

+0

總是首先執行php代碼,如果要訪問變量(不是php),那麼您使用AJAX或使用FORMS。 ... –

回答

0

開發下列結構;

function.php

<?php 
    $frontPlateClass = $_POST["frontPlateClass"]; 
    // Now you can use this vaiable anywhere you want 
    $image = $_POST['plate_image']; 
    $ebay_username = $_POST['ebay_username']; 
    $decoded = base64_decode(str_replace('data:image/png;base64,', '', $image)); 
    $date = date('m-d-Y', time()); 
    $name = $ebay_username."-front-" . $date .".png"; 
    file_put_contents("/var/makeaplate.justapplications.co.uk/cart/" . $name, $decoded); 
    $full_path = "/var/wmakeaplate.justapplications.co.uk/cart/" . $name; 
    imageCreateCorners($full_path, $name, 25); 
    ........ 
?> 

在你的HTML:

$(document).ready(function(e){ 
    var frontPlateClass = $('.choose-front-plate-size').find('a.active').parent().prop('className'), 
    $.ajax({ 
     url: "path_to_function.php", 
     method: "POST", 
     data: "frontPlateClass=" + frontPlateClass, 
     success: function(response) {} 
    }); 
}); 

通過這樣做,你可以把JS變量到PHP。你可以在PHP端使用該值

+0

非常感謝! :) –

0

在服務器屏幕上有內容之前執行PHP。一旦頁面發送到客戶端,瀏覽器就會呈現jQuery/HTML。

你可以做到這一點(將JS變量傳遞給PHP)的唯一方法是在文件加載完成後對PHP文件執行AJAX調用,並將該變量作爲數據發送。

0

你不能這樣做。

有一點你必須知道,它是服務器端和客戶端之間的區別。

服務器端生成的HTML,JavaScript等... PHP是在服務器端執行。

客戶端來了之後,它收到所有的html/javascript/etc ...通過服務器生成。一旦它收到所有這些文本,瀏覽器就會解析JavaScript並執行它。

你的問題的解決方案:

你可以使用AJAX,它會創建在服務器端PHP腳本的請求。 Ajax從客戶端生成。所以你可以將frontPlateClass的內容發送到php腳本。

+0

另外我建議你標記這個問題爲解決,如果你有一個Ajax問題,確保這個問題沒有被問到,如果沒有,創建另一個AJAX特定的問題。 –

0

你不能使用PHP'回顯'它。我會在您的PHP代碼中使用HTML添加一個空白div,然後讓JQuery填充它。

因此,在你的HTML這樣的事情..

<div class="inner">Goodbye</div> 
在你的腳本是這樣的

然後。

$(".inner").append(frontPlateClass); 

希望這會讓您朝正確的方向發展。