简体   繁体   English

将jQuery变量回显到PHP

[英]Echo jQuery variable into PHP

I have this code to export an image: 我有以下代码可以导出图像:

$(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>';

As you can see I have 1 jQuery variable frontPlateClass . 如您所见,我有1个jQuery变量frontPlateClass How can I add this in php with echo? 如何在echo中添加此内容?

PHP is executed before there is content on the screen, at the server. 在服务器上的屏幕上没有内容之前执行PHP。 jQuery / HTML is rendered by the browser once the page is sent to the client. 页面发送到客户端后,浏览器将呈现jQuery / HTML。

The only way you can do this (passing a JS variable to PHP) is by doing an AJAX call to a PHP file after the document has loaded, sending the variable as the data. 做到这一点的唯一方法(将JS变量传递给PHP)是在文档加载后对PHP文件进行AJAX调用,并将变量作为数据发送。

You cant do it like that. 你不能那样做。

One thing that you have to know, it the difference between server side and client side. 您必须知道的一件事是服务器端和客户端之间的区别。

Server side generate the html, the javascript etc... Php is executed on the server side. 服务器端生成html,javascript等。Php在服务器端执行。

Client side come after, it receive all html/javascript/etc... generate through the server. 客户端来了,它接收通过服务器生成的所有html / javascript / etc...。 Once it receive all this text, browser have mechanisms to parse the javascript and execute it. 一旦收到所有这些文本,浏览器就具有解析javascript并执行它的机制。

The solution for your problem: 您问题的解决方案:

You can use ajax, it gonna create a request to a php script on the server side. 您可以使用ajax,它将在服务器端创建对php脚本的请求。 Ajax is generated from the client side. Ajax是从客户端生成的。 So you will be able to send the content of frontPlateClass to a php script. 这样您就可以将frontPlateClass的内容发送到php脚本。

Develop following structure; 建立以下结构;

function.php 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);
    ........
?>

In your html: 在您的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) {} 
    });
});

By doing this, you can send js variable to php. 通过这样做,您可以将js变量发送到php。 And you can use that value in php side 您可以在php端使用该值

You can't 'echo' it using PHP. 您不能使用PHP“回显”它。 I would add a blank div using HTML in your PHP code and then have JQuery populate it. 我会在您的PHP代码中使用HTML添加一个空白div,然后让JQuery填充它。

So in your HTML something like this.. 因此,在您的HTML中是这样的。

<div class="inner">Goodbye</div>

Then in your script something like this. 然后在您的脚本中这样。

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

Hope this puts you in the right direction. 希望这能为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM