dou6577
2016-05-13 05:24
浏览 207

如何将JavaScript变量的数据传递给另一个php页面?

I have two pages: test.php and backend.php.

test.php has three images which when clicked takes the user to backend.php. I want the src of the image clicked to be displayed on backend.php. I am trying to achieve this task via JavaScript and Ajax but the src of the image doesn't appear on backend.php. Code for the same are given below:

test.php:

<?php
    session_start();
?>

<html>
    <head>
        <script src="jquery-1.9.1.js">
        </script>

        <script>
            function clickIt(data){
                $.ajax({
                           url: 'backend.php',
                           data: {"imgsrc":data},
                           type: 'post',
                           success:function(res){
                              alert(res.message);
                           }
                });
            }
        </script>
    </head>

    <body>
        <a href="backend.php">
            <img onclick="clickIt(this.src)" src="img1.jpg"/>
        </a>
        <a href="backend.php">
            <img onclick="changeIt(this.src)" src="img2.jpg"/>
        </a>
        <a href="backend.php">
            <img onclick="changeIt(this.src)" src="img3.jpg"/>
        </a>
    </body>
</html>

backend.php:

<?php
    session_start();
?>

<?php
    echo $_POST['imgsrc'];
    echo '<a href="test.php">Back</a>';
?>

What am I possibly doing wrong?

图片转代码服务由CSDN问答提供 功能建议

我有两页: test.php backend.php

test.php 有三张图片,点击后会将用户带到 backend.php 。 我想点击图片的src显示在 backend.php 上。 我试图通过JavaScript和Ajax来完成这项任务,但图像的src没有出现在 backend.php 上。 下面给出了相同的代码:

test.php:

 &lt;?php 
  session_start(); 
?&gt; 
 
&lt; html&gt; 
&lt; head&gt; 
&lt; script src =“jquery-1.9.1.js”&gt; 
&lt; / script&gt; 
 \  n&lt; script&gt; 
函数clickIt(data){
 $ .ajax({
 url:'backend.php',
 data:{“imgsrc”:data},
 type:'post',  
成功:功能(res){
 alert(res.message); 
} 
}); 
} 
&lt; / script&gt; 
&lt; / head&gt; 
 
&lt; body&gt  ; 
&lt; a href =“backend.php”&gt; 
&lt; img onclick =“clickIt(this.src)”src =“img1.jpg”/&gt; 
&lt; / a&gt; 
&lt;  ; a href =“backend.php”&gt; 
&lt; img onclick =“changeIt(this.src)”src =“img2.jpg”/&gt; 
&lt; / a&gt; 
&lt; a href =  “backend.php” &GT; 
  &lt; img onclick =“changeIt(this.src)”src =“img3.jpg”/&gt; 
&lt; / a&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
  <  /pre>
nn

backend.php:

nn
<?php
 session_start(); 
?&gt;  
 
&lt;?php 
 echo $ _POST ['imgsrc']; 
 echo'&lt; a href =“test.php”&gt;返回&lt; / a&gt;'; 
?&gt; 
   
 
 

我可能做错了什么?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dtfo55908 2016-05-13 05:37
    已采纳

    Try this;

    HTML:

    <body>
        <a href="javascript:void(0);">
            <img onclick="clickIt(this.src)" src="img1.jpg"/>
        </a>
        <a href="javascript:void(0);">
            <img onclick="changeIt(this.src)" src="img2.jpg"/>
        </a>
        <a href="javascript:void(0);">
            <img onclick="changeIt(this.src)" src="img3.jpg"/>
        </a>
    </body>
    

    Jquery:

    function clickIt(data){
        window.location.href = 'backend.php?imgsrc='+data; // redirect to backend.php
    }
    

    In backend.php

    <?php
        echo $_GET['imgsrc']; // use $_GET
        echo '<a href="test.php">Back</a>';
    ?>
    
    已采纳该答案
    打赏 评论
  • douwu0882 2016-05-13 05:47
    1. Click event of tag is not firing because tag takes the page to backend page before tag click event gets fired. You can resolve this problem simply by removing the tag from each line.
    2. You cannot access ajax response as res.message as you haven't echo the response in json format. You can access response simply from res variable
    3. Also change the function name to changeIt to clickIt as you have defined function has clickIt

    // use only res success:function(res) { alert(res);

    }

    <img onclick="clickIt(this.src)" src="img1.jpg"/>
    <img onclick="clikcIt(this.src)" src="img2.jpg"/>
    <img onclick="clickIt(this.src)" src="img3.jpg"/>
    
    打赏 评论

相关推荐 更多相似问题