2011-10-24 142 views
0

我一起工作的頁面是:http://glenwoodia.com/business-directoryAJAX意外的標記/未結束的字符串

我有得到分頁功能工作的問題。當您點擊其中一個數字(頂部的1 2 3 4 5 6 7)時,它應該動態拉取該組業務並將其顯示在頁面上。但是,在谷歌瀏覽器中,我收到了'意外令牌非法'錯誤,並且在firefox/IE中,我收到了'未終止的字符串'錯誤。

具體的PHP代碼我必須處理的AJAX應在我的腦海裏,採取任何惡意字符可能是導致該問題的護理:

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

任何想法?

+0

該'''不是*唯一的*字符,可以打破你的JavaScript字符串。數據中的換行符也可能產生相同的錯誤。 –

回答

4

使用json_encode()而不是那些醜陋的str_replaces - - 它負責編碼的任何事情,否則在JavaScript中無效:

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

輸出:

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

我覺得你的代碼可以這樣寫:

$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 
+0

'$ listings = addslashes(json_encode(bizdir_directory(「」,@ $ v [「offset」]));' 產生輸出,但它有沒有格式。 –

+0

你不需要'addslashes',因爲函數(i)添加了所有必要的斜槓(ii)將字符串包含在'''內部。「請參閱修訂後的答案 –

+0

是的,起初它不起作用,但那是因爲我試過把它放在'in the javascript。讓這個改變成功了,謝謝! –

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

這將返回相同的錯誤消息。 –

+0

所以它JS錯誤,你需要在多行中打破$列表變量。 – Dev

+0

你如何建議我這樣做?我想重要的問題是,爲什麼?該頁面的其他部分使用AJAX功能很好,並從數據庫中提取業務,而不會返回任何錯誤。 –

0

只是一個隨機的問題,而是應該:

'{$listings}' 

{$listings} 

呢?沒有單引號?我假設返回格式良好的JSON代碼 - 對於沒有機會查看您提供的網址而道歉 - 我可能會在這裏標記;

+0

JavaScript的單引號是必要的,因爲$ listing是一個字符串。 –

+1

在PHP中,該值位於雙引號內,因此該值實際進行評估。單引號只是輸出字符串的一部分。 – GolezTrol

+0

Ooooo我看到...你有沒有想過使用PHP的[htmlspecialchars](http://www.php.net/manual/en/function.htmlspecialchars.php)函數呢?我知道在JavaScript變量傳遞內部管理引號會感覺像拉動牙齒; P –

0

在鏈路上點擊時的響應是JavaScript的是eval()倒是

它設置元素的innerHTML但不帶有引號它。你只需要引用值,例如:

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

代替

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

響應點擊2頁時,我得到的是

"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://[email protected]y 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;" 

中,你可以看到缺少引號。

+0

我的原代碼是不是有'surround {$ listing}? –

+0

當我調試它時,沒有引號。看到。 – andyb