1

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;
                    }

                });
            });
5
  • I don't really get your problem, the above seems to do what you want? Commented May 27, 2012 at 21:12
  • Since you didn't accept my answer, I'm really lost on what you actually wanted. Could you somehow clarify? Did you want to load the values from the file straight (with an AJAX call)? Commented May 27, 2012 at 21:19
  • stackoverflow.com/questions/6188062/…? Commented May 27, 2012 at 21:26
  • This new edit makes it look like something that's not gonna happen unless you echo everything. Commented May 27, 2012 at 21:31
  • If you're doing it like that, why not just store $_GET['country'] in a cookie or session on this page and use that in the next PHP file? Commented May 27, 2012 at 21:37

3 Answers 3

4

Updated Answer

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

Original Answer

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";
Sign up to request clarification or add additional context in comments.

19 Comments

That nice i get the country variables from get
@RobertMrsic Where would you like to use that?
Yes but the problem ist that you use allvays php i need exlude php code why when i use comiler i get allvays error
@Robert Mrsic: You...don't wanna use PHP because you have errors in your code? Then you'll need to parse the URL using Javascript.
@RobertMrsic You can assign $country = $_GET['country']; if you want. Just be careful the types of data you let people embed in your page via the URL.
|
0

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 :)

4 Comments

IMO it does violate good practice, because it creates needless HTML nodes.
Some people need to store JavaScript templates, and to avoid another AJAX/AHAH call they either store them like you did in a hidden div, or (better) in an HTML comment. Because HTML comments are nodes too, and they are retrievable from JavaScript.
Well, it would be one additional node (plus a few subnotes), so the impact should be around as large as the difference between single and double quotes in PHP. I didn't know you could use comments, but it sounds like an interesting approach.
It's more elegant when it comes to HTML, less elegant when it comes to JS usage. Either way my opinion wasn't that it adds complexity, just that it's less semantic and less elegant as grabbing the template(s) remotely via AHAH.
0

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']) . ";"; ?>

6 Comments

You're mixing up syntax, the concat operator in PHP is . not +. You're also missing quotes.
You need quotes around the country string. That will end up with something like var country = Brasil;
Whoops. I haven't been writing in PHP very often apparently. Thanks for the save. And the quotes are just my own mistake...
The problem ist that i not need to use php but only javascript
Ugh, you should really use json_encode to ensure you get a valid JS string.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.