你能从img src请求/获取变量吗?

时间:2022-08-25 23:23:15

This is a two part question, but I want to make sure the first is actually achievable.

这是一个两部分问题,但我想确保第一个实际上是可以实现的。

Firstly, can you get access variables set by img src? I am attempting jQuery, including MySQL UPDATE etc, but this I needed to make sure would work?

首先,你能获得img src设置的访问变量吗?我正在尝试jQuery,包括MySQL UPDATE等,但我需要确保它能工作吗?

<a href="#" class="chnge"><img src="filename.jpg?id=1&open=1" /></a>

The jQuery would then have something similar to:

然后jQuery会有类似的东西:

      $("img#chnge").click(function() {

        var id      = $('#id').attr('value');
        var open    = $('#open').attr('value');
            $.ajax({
                type: "POST",
                url: "update.php",
                data: "id="+ id +"& open="+ open,
                success: function(){
                    $('img#chnge').fadeTo('slow',0.4);

                }
            });
        return false;
        });

I hope it is obvious, basically, I have an image, if it's "open" is set to 1, when you click it the opacity changes, but it would also send off a query to my update.php.

我希望很明显,基本上,我有一个图像,如果它的“打开”设置为1,当你单击它时不透明度改变,但它也会发送一个查询到我的update.php。

I know where the errors are in this form, I just dont know how to fix them. #1 = variables in the img src (i dont know if i can put them in the href instead?), and the 2nd is the $('#id') and $('#open') I don't think are correct, but I don't know what to change them to either.

我知道这种形式的错误在哪里,我只是不知道如何解决它们。 #1 = img src中的变量(我不知道我是否可以把它们放在href中?),第二个是$('#id')和$('#open')我认为不是没错,但我不知道要把它们改成什么。

Any assistance would be much appreciated.

任何援助将不胜感激。

Phillip.

UPDATE: After reading Otars reply, I thought I had better add the full code re: how the images are to be where they are...

更新:阅读Otars回复后,我想我最好添加完整的代码:图像是如何在他们所在的位置...

Thank-you. Because these images are being generated through a for() loop (php), is that going to have an affect on how this code will work?

谢谢。因为这些图像是通过for()循环(php)生成的,这会对这段代码的工作方式产生影响吗?

<?php
$query = mysql_query("SELECT * FROM catalogue");
$num = mysql_num_rows($query);

for ($x=1;$x<=$num;$x++) {

  $row = mysql_fetch_row($query);

  ?>

  <a href="#" id="chngeHref" /><img src="<?php echo $row[2]; ?>?id=<?php echo $row[0]; ?>&open=<?php echo $row[1]; ?>" id="chnge" /></a>

<?php

  /* row[0] = id 
     row[1] = open (1 or 0)
     row[2] = image url
  */

  }

?>

2 个解决方案

#1


2  

Anyway your image has to be a PHP script...

无论如何,你的图像必须是PHP脚本......

Pass GET parameters to it, do whatever you want in the script and then output the image like this:

将GET参数传递给它,在脚本中执行任何操作,然后输出如下图像:

Let's say file is called image.php

假设文件名为image.php

<?php

    $par = $_GET['test'];

    // Do some PHP related job here


    // Load the image
    header('Content-Type: image/jpeg');
    header('Location: /path/to/your/image.jpg');

?>

Then use it like this: <img src="image.php?test=value">

然后像这样使用它:你能从img src请求/获取变量吗?

It gonna call the PHP script first and then display image.

它将首先调用PHP脚本然后显示图像。

#2


0  

Sorry I don't exactly understand what your trying to do, but think this might help. You can store the images "open" state as a variable inside $(function(){..}). You will then be able to get and set that variable value from within your click event.

对不起,我不完全明白你想做什么,但想想这可能会有所帮助。您可以将图像“打开”状态存储为$(function(){..})中的变量。然后,您将能够从click事件中获取并设置该变量值。

$(function() {

    var open = false; // store the image state in the closure not global
    var id = .. 

    $(".chnge img").click(function() {
        if (!open) {
            // fade-in, post to server or whatever

            $.ajax({
                type: "POST",
                url: "update.php",
                data: "id="+ id +"& open="+ open,
                success: function(){
                    // handle response here.
                    // you can still access the open variable here!
                }
            }

            open = true; // want to update open?
        } 
    });
});

#1


2  

Anyway your image has to be a PHP script...

无论如何,你的图像必须是PHP脚本......

Pass GET parameters to it, do whatever you want in the script and then output the image like this:

将GET参数传递给它,在脚本中执行任何操作,然后输出如下图像:

Let's say file is called image.php

假设文件名为image.php

<?php

    $par = $_GET['test'];

    // Do some PHP related job here


    // Load the image
    header('Content-Type: image/jpeg');
    header('Location: /path/to/your/image.jpg');

?>

Then use it like this: <img src="image.php?test=value">

然后像这样使用它:你能从img src请求/获取变量吗?

It gonna call the PHP script first and then display image.

它将首先调用PHP脚本然后显示图像。

#2


0  

Sorry I don't exactly understand what your trying to do, but think this might help. You can store the images "open" state as a variable inside $(function(){..}). You will then be able to get and set that variable value from within your click event.

对不起,我不完全明白你想做什么,但想想这可能会有所帮助。您可以将图像“打开”状态存储为$(function(){..})中的变量。然后,您将能够从click事件中获取并设置该变量值。

$(function() {

    var open = false; // store the image state in the closure not global
    var id = .. 

    $(".chnge img").click(function() {
        if (!open) {
            // fade-in, post to server or whatever

            $.ajax({
                type: "POST",
                url: "update.php",
                data: "id="+ id +"& open="+ open,
                success: function(){
                    // handle response here.
                    // you can still access the open variable here!
                }
            }

            open = true; // want to update open?
        } 
    });
});