Sean Carruthers
Sean Carruthers

Reputation: 414

AJAX Unexpected Token / Unterminated String Literal

The page I am working with is: http://glenwoodia.com/business-directory

I am having issues getting the pagination functionality to work. When you click on one of the numbers (the 1 2 3 4 5 6 7 at the top), it is supposed to dynamically pull that set of businesses and display them on the page. However, in Google Chrome I get an 'unexpected token illegal' error, and in firefox / IE I get an 'unterminated string literal' error.

The specific PHP code I have to process the AJAX should, in my mind, take care of any rogue characters that could be causing the issue:

    $listings = trim(strip_tags(str_replace("'","\'",stripslashes(bizdir_directory("",@$v["offset"])))));
    $response = "clearMessage();
        document.getElementById('bizdir_directory').innerHTML = '{$listings}';
    ";

Any ideas?

Upvotes: 0

Views: 2420

Answers (4)

Salman Arshad
Salman Arshad

Reputation: 272056

Use json_encode() instead of those ugly str_replaces -- it takes care of encoding just about anything that would otherwise be invalid in JavaScript:

<script type="text/javascript">
var a = <?php echo json_encode("single quote ', double quote \", new
lines"); ?>;
</script>

Output:

<script type="text/javascript">
var a = "single quote ', double quote \", new\r\nlines";
</script>

I think your code can be written like this:

$listings = json_encode(trim(bizdir_directory("", $v["offset"])));
// #1: note that json_encode does not trim the input string
$response = "
    clearMessage();
    document.getElementById('bizdir_directory').innerHTML = {$listings};
"; // #2: note that {$listings} is not enclosed inside quotes

Upvotes: 4

andyb
andyb

Reputation: 43823

The response when clicking on a link is JavaScript that is eval()'d

It is setting the innerHTML of an element without surrounding it with quotes. You just need to quote the value, for example:

"clearMessage();document.getElementById('bizdir_directory').innerHTML = 'foo bar'"

instead of

"clearMessage();document.getElementById('bizdir_directory').innerHTML = foo bar"

The response I am getting when clicking page 2 is

"clearMessage();document.getElementById('bizdir_directory').innerHTML = Pages:&nbsp;&nbsp;1&nbsp;&nbsp;2&nbsp;&nbsp;3&nbsp;&nbsp;4&nbsp;&nbsp;5&nbsp;&nbsp;6&nbsp;&nbsp;7Central Realty, LLCVickie Gerdes / Greg Patterson331 West Thornton Ave.St. Louis, MO [email protected] Estate / Rental Property.Chat Mobility - EmersonTerry Gray404 HowlandP.O. Box 289Emerson, Iowa 515331-800-944-5526http://[email protected] Mobility - GlenwoodSandy Dornburgh906 South Locust StreetGlenwood, Iowa 51534712-527-3020http://www.chatmobility.comsdornburgh@chatmobility.comCommunication.City of GlenwoodBrian Kissel107 South Locust StreetGlenwood, Iowa [email protected] Cafe, LLCAlicia Bartley315 Main StreetMalvern, Iowa 51551712-624-8082http://[email protected]/Catering.Cohron Ready Mix, LLCSteve McCraken10001 192nd StreetCouncil Bluffs, Iowa 51503712-527-4696Concrete Company.Common Ground MinistryJon McNeel113 Center StreetGlenwood, Iowa [email protected], youth group.Country TireTom Collinson611 South Hazel StreetGlenwood, Iowa 51534712-527-3621http://www.countrytire.biz/[email protected] Repair.Crash and BurnsCandice Burns1002 Lincoln StreetMalvern, Iowa [email protected] repair.Croatt Heating &amp; Air Conditioning, LLCMathew and Jodi Croatt23203 Kane Ave.Glenwood, Iowa [email protected] service heating and air conditioning.Crouch, Richard &amp; BarbCrouch, Richard &amp; Barb59629 280th StreetMalvern, Iowa [email protected] Water ConditioningGlenn Robinson / Bill Cunard113 South 9th StreetP.O. 445Missouri Valley, Iowa 51555712-642-2695http://[email protected] conditioning.Davies AmphitheaterGordon &amp; Lori Woodrow301 North WalnutGlenwood, Iowa 51534712-527-3545http://www.glenwoodnet.com/[email protected], Dick &amp; JudyDavis, Dick &amp; Judy1007 4th StreetGlenwood, Iowa [email protected] Bird-Sell PLC Attorney At LawDeShawne Bird-Sell417 Sharp SteetGlenwood, Iowa [email protected] firm.Don&#039;s FurnitureDon Malcom419 Sharp StreetGlenwood, Iowa [email protected]. Barry JoseDr. Barry Jose1601 Ave. DGlenwood, Iowa 51534712-323-5213Individuals.Dr. Robert FryzekDr. Robert Fryzek14 North WalnutGlenwood, Iowa 51534712-527-9135Medical, general family practice.Edward JonesRon Hanson908 South Locust StreetGlenwood, Iowa 51534712-527-3520http://[email protected] Planning/Investing.El Portel Mexican RestaurantMartin Barajas612 Locust StreetGlenwood, Iowa 51534712-527-4014Restaurants.Embray, DevinEmbray, Devin103 CentralGlenwood, Iowa 51534712-527-9034Individuals.Evans Equipment Company, E.E.C., IncorporatedJeff &amp; Michelle Evans1305 South Locust StreetGlenwood, Iowa 51534712-527-1440http://www.evansrental.comRental equipment.Exclusive PaintingJamie &amp; Stephanie Todd25880 Noyes Ave.Glenwood, Iowa 51534402-740-9099Painting Service.Farm Bureau Insurance &amp; Financial ServicesJohn Gregory302 South Locust StreetGlenwood, Iowa 51534712-527-3153http://www.agentjohngregory.comInsurance and financial services.Feed EnergyJoe Liddick / Paul Knockel20159 Kelting Ave.P.O. Box 130Pacific Junction, Iowa 51561712-655-8293http://www.feedenergy.comIndustries.FFG Consulting, LLCMark FordP.O. Box 529Glenwood, Iowa 51534712-309-6336http://www.FFGroupConsulting.comConsulting.First National BankDoug Meggison102 South Locust StreetGlenwood, Iowa [email protected] / Financial.Five Star Quality Care, Inc.Nate Parks114 East GreenGlenwood, Iowa 51534712-527-4841http://[email protected] Services.Fountains Ballroom, Inc.Marty &amp; Erin Williams51496 - 230th StreetGlenwood, Iowa 51534712-526-2030http://www.thefountainsballroom.comWeddings, conferences, special occasions, photography and catering.Garden TreasuresRyan &amp; Connie Bichel22309 221st StreetGlenwood, Iowa 51534712-527-3602http://[email protected] service Garden Center, statuary, birdbathes, benches, fountains, landscape supplies, mulch, rock, sand, soil and so much more.Pages:&nbsp;&nbsp;1&nbsp;&nbsp;2&nbsp;&nbsp;3&nbsp;&nbsp;4&nbsp;&nbsp;5&nbsp;&nbsp;6&nbsp;&nbsp;7;"

in which you can see the missing quotes.

Upvotes: 0

Chris Kempen
Chris Kempen

Reputation: 9661

Just a random question, but should:

'{$listings}'

not be

{$listings}

instead? Without the single quotes? I'm assuming that well-formed JSON code is being returned - apologies for not having a chance to look at the URL you provided - I might be way off the mark here ;)

Upvotes: 0

Riz
Riz

Reputation: 10246

$response = "clearMessage();";
$response .= "document.getElementById('bizdir_directory').innerHTML = '{$listings}';";

Upvotes: 0

Related Questions