php generated strings not passed correctly to a javascript function

  html, javascript, php

I have a form that is being processed by php file in the action. the code snippet below. It collects the form content passed by a GET and appends a JSON record to the end of the log file

$pathToJSONFile ="LHVC_Logs/LHVCResortMapLog.json";
$jsonToLog = array(
   "Date" => date("Y-m-d H:i:s"), 
);
foreach ($_GET as $field => $entry) {
   $jsonToLog[$field] = $entry;
}
$json = json_encode($jsonToLog);
$json .= PHP_EOL;
$bytes = file_put_contents($pathToJSONFile, $json, FILE_APPEND);

I also wrote a webpage to present the log in a table. with the entries from the form and also presents the json record so it can easily be moved from the log into another json file if accepted as valid update.

the relevant code to read the json is below. The log gile is read, converted to an array by the EOL record separater. The $log associative array is indexed by the json string and points to an associative array with the contents of each record.

$json = file_get_contents($logFile);
$lines = explode(PHP_EOL, $json);
$log = array();
foreach ($lines as $line) {
  $log[$line] = json_decode($line,true);
}

An html table is created inside a loop

foreach ($log as $field => $entry) 
          ...
    $field = addslashes($field);
    echo "<td><a HREF="javascript:PopUP('$field')">JSON</a></td>";
}

The PopUP script is very simple it displays the string passed to it in an alert box

function PopUP($str) {alert($str);}) 

the trouble I am running into is that the PopUP is not working because the json string is not being passed correctly.

<tr id=RV1 class=Royal> 
    <td style='text-align:right'> <a href="#RoyalVillas"> Royal </a> </td> 
    <td> <a HREF="javascript:PopUP('Link not yet available!nCheck back soon.')"> RV1 </a> </td> 
    <td>  </td><td>  </td> <td style='text-align:left'> <details> <summary> 3 </summary>Street:1 MBR,0 BR <br> Upper:0 MBR,2 BR <br> Lower: MBR, BR <br> </details></td> 
    <td style='text-align:left'> Very nice. Highly recommended. Upstairs Theatre Seating </td>
    <td><a HREF="javascript:PopUP('{"Sect":"Royal","Villa#":"1","VillaName":"","Owner":"","OwnerSite":"","BRs":"3","StreetMBR":"1","StreetBR":"0","UpperMBR":"0","UpperBR":"2","LowerMBR":"","LowerBR":"","Comments":"Very nice. Highly recommended. Upstairs Theatre Seating"}')">JSON</a></td></tr>

the last is what is not working right – the one in the 2nd does work right

I tried removing the addslashes function which changed the last entry to

<td><a HREF="javascript:PopUP('{"Sect":"Royal","Villa#":"1","VillaName":"","Owner":"","OwnerSite":"","BRs":"3","StreetMBR":"1","StreetBR":"0","UpperMBR":"0","UpperBR":"2","LowerMBR":"","LowerBR":"","Comments":"Very nice. Highly recommended. Upstairs Theatre Seating"}')">JSON</a></td>

which looks prettier but has the same symptom. I believe it is because of the quotes which is why I did the addslashes in the first place.

sorry for the long entry but I hope I can get some suggestions on how to accomplish what I intend.

Source: Ask PHP

LEAVE A COMMENT