Hi,

I am tryng to populate a SELECT from PHP and Jquery but the output from PHP is adding a backslash to the closing </option> tag and removing the <option> opening tag like this:

Bank Facilities<\/option>
Embassy<\/option>
Government<\/option>

here is my code:

$request_category = $_GET['cboRequest'];

$mysql_query = $mysql_connection->prepare('CALL sp_get_request_processing_period(:param_request_category)');
$mysql_query->bindParam(':param_request_category', $request_category, PDO::PARAM_STR);

$mysql_query->execute();

while($mysql_row = $mysql_query->fetch())
{
	$request_name = $mysql_row["request_category_name"];
	$processing_days = $mysql_row["processing_period_in_days"];
}

$request_purpose = "";

$mysql_query = $mysql_connection->prepare("CALL sp_populate_request_purpose(:param_request_category)");
$mysql_query->bindParam(':param_request_category', $request_category, PDO::PARAM_STR);
$mysql_query->execute();

while($mysql_row = $mysql_query->fetch())
{
	$request_purpose .= "<option value=" . $mysql_row["request_purpose_id"] . ">" . $mysql_row["request_purpose_title"] . "</option>";
}

$response = array("name" => $request_name, "days" => $processing_days, "purpose" => $request_purpose);

echo json_encode($response);

    I'm not sure what you mean by unnecessary backslashes, iirc they are necessary. As for removing the opening tag... You might try var_dumping $response and making sure it still has them, and my little test script worked just fine:

    <?php
    
    $a = array(
         'name' => 'some category name',
         'days' => 123,
         'purpose' => '<option value=1>Title1</option><option value=2>Title2</option>'
    );
    echo json_encode($a);
    

    {"name":"some category name","days":123,"purpose":"<option value=1>Title1<\/option><option value=2>Title2<\/option>"}

    When I then JSON.parse in a browser or json_decode in php that string, I get the expected </option> in the resulting string, so the added \ is removed. If you're really concerned about the added slashes there, you could pass the JSON_UNESCAPED_SLASHES option as the second argument to json_encode (however, I make no promises this won't cause any problems down the road).

      this is my var_dump:

      array(3) { ["name"]=> string(22) "Employment Certificate" ["days"]=> string(1) "5" ["purpose"]=> string(141) "Bank FacilitiesEmbassyGovernmentPersonal" }

        [font=monospace]/[/font] gets escaped because having [font=monospace]</script>[/font] tag in the middle of a [font=monospace]<script>[/font] element's content could be Very Bad because it could easily be misinterpreted as a closing [font=monospace]</script>[/font] tag.

        It's perfectly legal JSON to have [font=monospace]\/[/font] in place of [font=monospace]/[/font]. The specification explicitly allows it.

        As for the missing opening tags: how are you viewing the output? If it's in a page that is being rendered as HTML, then what do you think the HTML interpreter would do if it saw "[font=monospace]<option[/font]...." - especially if this is outside a [font=monospace]<select>[/font] element?

        http://board.phpbuilder.com/showthread.php?10240608&viewfull=1#post10406054

          Hi,

          Problem not resolved yet!

          This is my var_dump:

          array(3) { ["name"]=> string(22) "Employment Certificate" ["days"]=> string(1) "5" ["purpose"]=> string(141) "Bank
          Facilities
          Embassy
          Government
          Personal" }

          and this is my json_encode($response):

          {"name":"Employment Certificate","days":"5","purpose":"Bank Facilities<\/option>
          Embassy<\/option>
          Government<\/option>
          Personal<\/option>"}
          
          
          
          

            Not sure why it would matter, but you should be quoting the HTML attribute:

            $request_purpose .= '<option value="' . $mysql_row["request_purpose_id"] . '">' . $mysql_row["request_purpose_title"] . '</option>';
            

            Also, if you are looking at the debug output in your browser, be sure to look at it in "view source" mode, since you want to see the HTML tags.

              Write a Reply...