Reputation: 53
I have this in my config.php
file
$DOMAIN = 'http://domain.com';
$DOMAIN_SSL = 'http://domain.com';
$DOMAIN_ROOT = 'var/domain/root/html';
config.php
is included in each page on first line
Now I have this jQuery code
var country = '<?php echo $_GET['country'];?>';
var category = 'Cat' ;
var category = '<?php echo $DOMAIN.'/link'; ?>';
How can I get variables from php with jQuery code to make jQuery code like this
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;
}
});
});
Upvotes: 1
Views: 3870
Reputation: 90
Assuming this is on the page and not in some separate JS file, you could just write the entire statement in PHP.
<?php echo "var country = " . json_encode($_GET['country']) . ";"; ?>
Upvotes: 0
Reputation: 2191
Frankly, I think you don't need to overcomplicate things here so much. This may not be the fanciest solution or anything but it should work.
The idea is, on the actual page (say, home.php
) you would echo an HTML element containing the values you need, such as:
//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>";
(Side note: you would set the class "hidden" to be invisible to the user.)
Now your external JS (say, scripts.js
) can retrieve the values using JS only which will allow you to use the compressor:
$(function(){
var transfer = $.parseJSON( $('#transferred_values').text() );
});
Let me know if this violates good practice or any rules I'm not aware of :)
Upvotes: 0
Reputation: 268462
If you're attempting to get values from the URL, much like you would using $_GET
in PHP, I would encourage you to use something like the jQuery URL Parser:
var country = $.url().param("country"); // Gets the 'country' parameter
Download jQuery URL Parser: https://github.com/allmarkedup/jQuery-URL-Parser
I would encourage you to keep it simple; just use formatted strings and json_encode
. For example:
$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" ) );
Which outputs:
var country = "United States";
var category = 5;
var domain = "http:\/\/stackoverflow.com\/link";
Upvotes: 4