Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6683861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:52:18+00:00 2026-05-26T04:52:18+00:00

I’m attempting to echo a PHP variable into my script, however it seems to

  • 0

I’m attempting to echo a PHP variable into my script, however it seems to break the rest of the script:

Here’s my PHP:

$geo_options = "<option value=\"0\">Select country</option>
<option value=\"AF\">Afghanistan</option>
<option value=\"AL\">Albania</option>
<option value=\"DZ\">Algeria</option>
<option value=\"AS\">American Samoa</option>
<option value=\"AD\">Andorra</option>
<option value=\"AG\">Angola</option>
...";

And my Javascript/jQuery:

$(document).ready( function() {
    var id = $(this).val();

    function countProperties(obj) {
      var prop;
      var propCount = 0;

      for (prop in obj) {
        propCount++;
      }
      return propCount;
    }
      var options = "<?php echo $geo_options; ?>";
      $('#geo-location').append(options);
      $.get('json.php',
        function(data) {
            $('input[name=url]').val(data.url);
            $('select[name=r_id]').prepend("<option selected='selected' value='" + data.rid + "'>" + data.rname + "</option>");
            $('input[name=r_min]').val(data.rmin); $('input[name=r_max]').val(data.rmax);
            if(data.blank == 1) {
            $('input[name=blank]').attr("checked", "checked"); 
            }

            var geo = (countProperties(data)-5)/2;


            }, 'json');

    });

If I comment out var options = '<?php echo $geo_options; ?>'; so this must be the part stopping the rest of the script from executing correctly (from that point downwards, as even $('#geo-location').append(); doesn’t work).

I just can’t figure out why this would be though?

Any help, would be greatly appreciated.

(Note: I’ve attempted to use both ' single quotes and " double quotes to encase the variable, thinking that the escapes in the string might be the problem, but neither have made a difference).

Update:

I’ve now placed my entire code, both JS and HTML into a JSFiddle as I figure there might be something I haven’t spotted at all. To recap on what I’ve tried so far; using different quote types (single, double, none), re-escaping the variable $geo_options with add_slashes(), str_replace() and json_encode(), echoing the variable $geo_options into a random HTML element, which worked fine.

My JsFiddle: http://jsfiddle.net/SFwB6/

I must confess, I’m totally stumped.

Update 2:

I’ve attempted to make it much easier, and place the entire $geo_options string in var options from the beginning; http://jsfiddle.net/NUCRG/.

However, both JSLint and Dreamweaver display a syntax error, but I’m not sure why. Perhaps this is the root of the problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:52:19+00:00Added an answer on May 26, 2026 at 4:52 am

    What’s happening is that you need to sets of escapes, when you only have one.
    When you echo that string into the javascript, because the string is encapsulated with double quotes ("), this means that each occurence of \" in the string is just printed as ", because the \ is being removed as an escape character in php.

    But then javascript comes up, and also needs that escape character because you are, again encapsulating it in a string.

    So to print it, use something like
    var options = "<?php echo str_replace('"', '\"', $geo_options); ?>";

    EDIT:
    If you want to go the json route, try this:

    var options = <?php echo json_encode($geo_options); ?>;
    for (var i in options)
        $('#geo-location').append('<option name="'+i+'">'+options[i]+'</option>');
    

    Instead of

    var options = "<?php echo $geo_options; ?>";
    $('#geo-location').append(options);
    

    Note, the php array geo_options will need to look something like:

    $geo_options = array ("0"=>"Select country",
    "AF"=>"Afghanistan",
    "AL"=>"Albania",
    "DZ"=>"Algeria",
    "AS"=>"American Samoa",
    "AD"=>"Andorra",
    "AG"=>"Angola");
    

    EDIT 2:
    Alright then, try this

    var options = '<?php echo str_replace(array("\n","\r"), '', $geo_options); ?>';
    

    and make sure the $geo_options string is formatted like (and there are NO single quotes allowed):

    <option value="0">Select country</option>
    <option value="AF">Afghanistan</option>
    <option value="AL">Albania</option>
    <option value="DZ">Algeria</option>
    <option value="AS">American Samoa</option>
    <option value="AD">Andorra</option>
    <option value="AG">Angola</option>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.