将javascript值传递给PHP [英] Passing javascript value to PHP

查看:57
本文介绍了将javascript值传递给PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为Web开发人员我很新,我发现自己试图解决问题。

I am quite new as web developer, I find myself trying to resolve a problem.

我想将值JavaScript传递给PHP而不使用任何表单方法。

I would like to pass a value JavaScript to PHP without using any form method.

这是我的PHP代码(这是一个幻灯片,我在<$ c $中放了 onclick 事件c> img 标签为了获取被点击的图像的id,然后id信息存储在我的JavaScript文件的变量中。问题是:如何将该变量传递给PHP我的JavaScript文件?):

Here my PHP code (it is a slideshow in which I put an onclick event in img tag in order to get the id of image that is clicked, then the id information is stored in a variable on my JavaScript file. The question is: how can I pass that variable to PHP from my JavaScript file?):

提前致谢。
祝你好运,

Thanks in advance. Best regards,

function getIdImage(clicked_id)
{
    // alert(clicked_id);

    var ClickedId= clicked_id;

  }





<div class="row">
  <div class="col-xs-12 col-md-12 col-lg-12 widhest">
     <div class="bx_wrapper_photo_below">
        <ul id="bx_slider_photo_below" class="bx_viewport_photo_below">

         <?php while($line = mysqli_fetch_assoc($outcomesListPhotos1)) {?>
            <li>
                     <img src="photos/<?php echo utf8_encode($line['photo']); ?>" class="img-responsive" onclick="getIdImage(<?php echo utf8_encode($line['id_photo']);?>)">                
             </li>

         <?php }?>
        </ul>

        <a class="pager-prev_below"><i class="icon-chevron-left"></i></a>
        <a  class="pager-next_below"><i class="icon-chevron-right"></i></a>
    </div>
   </div>
</div>

Fabio

推荐答案

如果您使用的是标准设置,那么每次向服务器发出请求时,您的PHP代码都将从头到尾运行。它不会像您在页面上的Javascript代码那样按需运行。因此,为了将值传递给PHP脚本,您必须向服务器(PHP代码所在的位置)发送请求,并在请求有效负载内传递所需的值。请求可以通过简单链接,常规表单或XHR发送。如果您不希望页面重新加载,但是您想在服务器上运行一些代码,那么在完成后,在浏览器上执行一些操作,然后您选择的工具就是XHR。

If you're using a standard setup, your PHP code will run from start to finish each time a request is made to the server. It won't run "on demand" like the Javascript code you have on your page. In order to pass a value to a PHP script, you therefore have to send a request to your server (where the PHP code resides) passing the desired value(s) inside the request payload. The request can be sent through a simple link, regular form or an XHR. If you don't want the page to reload, but you want to run some code on the server and, when that is done, do some stuff on the browser then your tool of choice is the XHR.

为了发送XHR,您可以自己从头开始构建它,或者您可以使用其中的许多库中的一个,这当然是推荐的路径。假设你想使用jQuery:

In order to send an XHR you could build it yourself from scratch or you can use one of the many libraries available out there, which is of course the recommended route. Let's say you want to use jQuery:

function getIdImage(clicked_id)
{       
    jQuery.ajax({
       url: '/url/to/my/script.php',
       type: 'POST',
       dataType: 'json',
       data: { clicked_id: clicked_id },
       success: function(result) {
           // This function will be called when the server returns a success (200 OK) response
           console.log(result);
           alert('Yay!');
       },
       error: function(xhr) {
            // This function is called when the server fails to process the request, for whatever reason
            console.log(xhr);
            alert('Nay!');
       }
    });
}

在您的服务器上,您将拥有一个处理此请求的脚本,一个您在URL中指出,并且您将能够使用在请求中传递给脚本的任何有效负载

On your server you'll have a script to process this request, the one you pointed at in the URL, and you'll be able to use whatever payload was passed to the script in the request

<?php

$data = json_decode($_POST);

echo "The ID of the clicked image is: " . $data->clicked_id;

当然这个例子不会做太多,它只会给回显的字符串返回一个响应。根据你想要做的事情,你需要在做一些有意义的计算之后建立一个响应,并且响应应该本身是JSON格式的(使用旧的 json_encode 内置PHP函数。)

Of course this example won't do much, it will just return the echoed string a response. Depending on what you want to do, you'll need to build a response after doing some meaningful computation, and the response should probably be itself in JSON format (using the good old json_encode builtin PHP function).

这篇关于将javascript值传递给PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆