0

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?

Salman A
  • 262,204
  • 82
  • 430
  • 521
Sean Carruthers
  • 414
  • 2
  • 10
  • The `'` isn't the *only* character that can break your JavaScript string. Newlines in your data could also produce the same error. – Salman A Oct 24 '11 at 08:22

4 Answers4

4

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
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • `$listings = addslashes(json_encode(bizdir_directory("", @$v["offset"])));` produces output, but it has no formatting. – Sean Carruthers Oct 24 '11 at 08:29
  • You don't need `addslashes` because the function (i) adds all necessary slashes (ii) encloses the string inside `"`. See revised answer. – Salman A Oct 24 '11 at 08:32
  • Yes, at first it didn't work, but that's because I tried enclosing it in ' in the javascript. Making that change worked. Thank you! – Sean Carruthers Oct 24 '11 at 08:33
0
$response = "clearMessage();";
$response .= "document.getElementById('bizdir_directory').innerHTML = '{$listings}';";
Riz
  • 9,703
  • 8
  • 38
  • 54
  • so it JS Error, you need to break $listings variable in multiple lines. – Riz Oct 24 '11 at 08:08
  • How do you propose I do that? And I guess the important question is, why? The other parts of that page that use AJAX function just fine, and pull businesses from the database, without returning any errors. – Sean Carruthers Oct 24 '11 at 08:10
  • http://stackoverflow.com/questions/227552/common-sources-of-unterminated-string-literal – Riz Oct 24 '11 at 08:13
0

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

Chris Kempen
  • 9,491
  • 5
  • 40
  • 52
  • The single quotes are necessary for the javascript, as $listings is a string. – Sean Carruthers Oct 24 '11 at 08:07
  • 1
    In PHP this value is inside the double quotes, so the value is actually evaluated. The single quotes are just part of the outputted string. – GolezTrol Oct 24 '11 at 08:09
  • Ooooo I see...have you thought about using PHP's [htmlspecialchars](http://www.php.net/manual/en/function.htmlspecialchars.php) function at all? I know managing quotes inside javascript variable passing can feel like pulling teeth ;P – Chris Kempen Oct 24 '11 at 08:14
0

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 63119314-862-5557gerdesv@central-realty.comReal Estate / Rental Property.Chat Mobility - EmersonTerry Gray404 HowlandP.O. Box 289Emerson, Iowa 515331-800-944-5526http://www.chatmobility.comtgray@chatmobility.comCommunication.Chat Mobility - GlenwoodSandy Dornburgh906 South Locust StreetGlenwood, Iowa 51534712-527-3020http://www.chatmobility.comsdornburgh@chatmobility.comCommunication.City of GlenwoodBrian Kissel107 South Locust StreetGlenwood, Iowa 51534712-527-4717city.hall@mchsi.comGovernment.Classic Cafe, LLCAlicia Bartley315 Main StreetMalvern, Iowa 51551712-624-8082http://www.classiccafeandcatering.combartleya14@hotmail.comRestaurant/Catering.Cohron Ready Mix, LLCSteve McCraken10001 192nd StreetCouncil Bluffs, Iowa 51503712-527-4696Concrete Company.Common Ground MinistryJon McNeel113 Center StreetGlenwood, Iowa 51534402-525-9642jmcneel@cqmglenwood.orgMinistry, youth group.Country TireTom Collinson611 South Hazel StreetGlenwood, Iowa 51534712-527-3621http://www.countrytire.biz/glenwoodfirestone.htmglenwoodfirestone@yahoo.comAutomotive Repair.Crash and BurnsCandice Burns1002 Lincoln StreetMalvern, Iowa 51551402-679-6927cburns.computer.repair@gmail.comComputer repair.Croatt Heating &amp; Air Conditioning, LLCMathew and Jodi Croatt23203 Kane Ave.Glenwood, Iowa 51534712-527-3455croatthvac@gmail.comFull service heating and air conditioning.Crouch, Richard &amp; BarbCrouch, Richard &amp; Barb59629 280th StreetMalvern, Iowa 51551712-524-8933Crobertson@millscoia.usIndividuals.Culligan Water ConditioningGlenn Robinson / Bill Cunard113 South 9th StreetP.O. 445Missouri Valley, Iowa 51555712-642-2695http://www.culligan.comglennculligan@yahoo.comWater conditioning.Davies AmphitheaterGordon &amp; Lori Woodrow301 North WalnutGlenwood, Iowa 51534712-527-3545http://www.glenwoodnet.com/daviesg.woodrow@mchsi.comRecreation.Davis, Dick &amp; JudyDavis, Dick &amp; Judy1007 4th StreetGlenwood, Iowa 51534712-527-9527rrjjdavis@gmail.comIndividuals.DeShawne Bird-Sell PLC Attorney At LawDeShawne Bird-Sell417 Sharp SteetGlenwood, Iowa 51534712-527-4026deshawne@aol.comLaw firm.Don&#039;s FurnitureDon Malcom419 Sharp StreetGlenwood, Iowa 51534712-527-4809setterrw@aol.comRetail.Dr. 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://www.edwardjones.comron.hanson@edwardjones.comInvestment 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 51534712-527-3171dmeggison@fnni.comBanks / Financial.Five Star Quality Care, Inc.Nate Parks114 East GreenGlenwood, Iowa 51534712-527-4841http://www.5sqc.comnparks@5sqc.comHuman 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://www.ryansgardentreasures.comryan.bichel@yahoo.comFull 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.

andyb
  • 43,435
  • 12
  • 121
  • 150