Now without having to read boring code let me just summarise what is going on.
An XHR request goes out via jQuery post function when the document loads. The post sends a POST header to a PHP file that calls another php file to process some strings and return it. Finally an echoed json_encode function returns the string to the jQuery post function which the jQuery then manipulates into the DOM.
The result that is returned by JavaScript’s post function is this:
<option value="\"auto2\"">auto2<\/option></option>
See all those escape characters? Well I tried just echoing the result on the PHP side instead of echoing with json_encode function and it returns:
<option value="auto">auto</option>
Without any other special characters. This is what is desired. My question is, is this okay to use? And why does json_encode add special characters? Is there a way to use json_encode without those other characters being returned?
EDIT
JS
$(document).ready(function() {
var PROCESSOR_FILE = "http://address/include/processor.php";
$.post(PROCESSOR_FILE, {"task": "verticalSelect"}, function(data) {
$("#verticalSelect").html(data);
});
});
PHP
$IMEXporterpath = dirname(__FILE__);
$IMEXporterFile = $IMEXporterpath . "/IMEXporter.class.php";
if (isset($_REQUEST["task"]))
$task = $_REQUEST["task"];
else
$task = "";
require_once $IMEXporterFile;
$IMEXp = new IMEXp();
$result = $IMEXp->returnHtml($task);
echo json_encode($result);
MORE PHP
public function returnHtml($element) {
return $this->returnHtml_HTML($element);
}
private function returnHtml_HTML($element) {
if($element == "verticalSelect") {
$dbTables = $this->get_Tables();
$nbTable = count($dbTables);
for ($a = 0; $a < $nbTable; $a++) {
$ZHTML .= '<option value="'.$dbTables[$a].'">'.$dbTables[$a].'</option>';
}
}
return $ZHTML;
}
On PHP 5.3.13:
Outputs
No converted special chars here.
Prints the same to the console.
Without AJAX does json_encode($str) display
"etc. ?