2014-03-12 107 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); 

希望这会让您朝正确的方向发展。