English 中文(简体)
Pp 到 Jquery 变量
原标题:Php to Jquery Variable

我在我的 config.php 文件中有这个

     $DOMAIN =  http://domain.com ;
 $DOMAIN_SSL =  http://domain.com ;
$DOMAIN_ROOT =  var/domain/root/html ;

config.php 包含在第一行的每一页中。

现在我有了这个 jQuery 代码

var country =  <?php echo $_GET[ country ];?> ; 
var category =  Cat  ;
var category =  <?php echo $DOMAIN. /link ; ?> ;

如何从 JQuery 代码的 php 中获取变量, 使 jQuery 代码变成这样?

var country =  $country ; 
var category =  Cat  ;
var url=  $domain , /link ;

ORIGINAL CODE I NEED TO EXLUDE PHP

var country =  <?php echo $_GET[ country ];?> ; 
                var category =  CAT1  ;               
                function loadData(page){
                    loading_show();                    
                    $.ajax
                    ({
                        type: "POST",
                        url: "<?php echo $DOMAIN ?>/post.php",
                        data: "page="+page +"&category="+category+"&country="+country,
                        success: function(msg)
                        {
                        loading_hide();
                        $("#content-large").html(msg);
                        }
                    });
                }
                loadData(1);  // For first time page load default results
                $( #content-large .pagination li.active ).live( click ,function(){
                    var page = $(this).attr( p );
                    loadData(page);

                });           
                $( #go_btn ).live( click ,function(){
                    var page = parseInt($( .goto ).val());
                    var no_of_pages = parseInt($( .total ).attr( a ));
                    if(page != 0 && page <= no_of_pages){
                        loadData(page);
                    }else{
                        alert( Enter a PAGE between 1 and  +no_of_pages);
                        $( .goto ).val("").focus();
                        return false;
                    }

                });
            });
最佳回答

Updated Answer

如果您试图从 URL 中获取值, 正如您在 PHP 中使用 $_GET 一样, 我鼓励您使用 jQuery URL Parser 这样的工具:

var country = $.url().param("country"); // Gets the  country  parameter

下载 jQuery URL Parser : < a href=> https://github.com/allmarkedup/jQuery-URL-Parser" rel= "nofollow noreferrer" > https://github.com/allmarkedup/jQuery-URL-Parser

Original Answer

我鼓励您保持简单; 只要使用格式化字符串和 json_encode 。 例如 :

$country  = "United States";
$domain   = "http://stackoverflow.com";
$category = 5;

printf( "var country  = %s;", json_encode( $country ) );
printf( "var category = %s;", json_encode( $category ) );
printf( "var domain   = %s;", json_encode( $domain . "/link" ) );

哪些产出:

var country  = "United States";
var category = 5;
var domain   = "http://stackoverflow.com/link";
问题回答

坦率地说,我认为你不需要在这里把事情过于复杂。这也许不是理想的解决方案或任何东西,但应该奏效。

意思是,在实际页面上(例如, home.php ),您会回响包含您需要的值的 HTML 元素, 例如 :

//Fill this array with all the values that you want available in JS
$JSONarray = array( all => the , values => you , want => in , JS );
//Later print it on the page
echo "<span class= hidden  id= transferred_values >"
    .json_encode($JSONarray)."</span>";

(请注意: 您会将类“ 隐藏” 设置为用户不可见 。 )

现在您的外部 JS (say, scripts.js ) 可以使用 JS 获取值, 只有JS 允许您使用压缩机 :

$(function(){
    var transfer = $.parseJSON( $( #transferred_values ).text() );
});

如果这违反了良好做法或任何我所不知道的规则,请告诉我:)

假设这在网页上, 而不是在联署材料中, 你可以在PHP中写下整个声明。

<?php echo "var country = " . json_encode($_GET[ country ]) . ";"; ?>




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签