简体   繁体   English

如何在同一页面中将值从javascript传递到php

[英]How to pass value from javascript to php in same page

Im tying to implament a preloader for swf based on this Post , but im getting trouble passing the javascript value to a php variable. 我打算根据此帖子为swf填充一个预加载器,但在将javascript值传递给php变量时遇到麻烦。

     <script type="text/javascript">

        var swfJSPreLoaderConfig = {

         'assets':['swf/test.swf'],

            'assetLoaded': function( asset){

             alert(asset);

            },}

    </script>

The alert in the above code shows only when the swf file test.swf is fully downloaded. 上面的代码中的alert仅在完全下载swf文件test.swf时显示。 the value that im trying to get in php is asset ( this value contain the swf file path, in this example is swf/test.swf . the alert fires always when the swf files is fully dowloaded and it works very good.) 我试图获取php的值是asset (此值包含swf文件路径,在此示例中为swf/test.swf 。在完全下载swf文件时,警报始终会触发,并且效果很好。)

i tried something like this to get it in a php variable. 我试过这样的事情在php变量中获取它。 but no lucky. 但没有运气。

$filename = $_REQUEST['asset'];

also i tired using ajax but nothing. 我也厌倦了使用ajax,但一无所获。

    <script type="text/javascript">

     var swfJSPreLoaderConfig = {

       'assets':['swf/test.swf'],

         'assetLoaded': function( asset){   

                $.ajax ({ 
                type: "post",
                url: "index.php",
                data: { 'asset': asset },
                success: function()
                    {
                        alert(asset);
                    }
                });

            },}

    </script>

and then 接着

 $filename = $_REQUEST['asset'];

Whats worng? 怎么了?

Try this, 尝试这个,

data: { 'asset': asset },
success: function(response)
     {
        alert(response);
     }

instead of, 代替,

  data: { 'asset': asset },
  success: function()
        {
          alert(asset);
        }
  });

PHP: PHP:

 echo $filename = $_REQUEST['asset'];

Change from

url: "index.php"

to

url: "index.php?asset="+asset

Then in PHP, use: 然后在PHP中,使用:

echo $_GET['asset'];

to confirm that you are sending the correct value 确认您发送的是正确的值

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

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