Matthew Goulart
Matthew Goulart

Reputation: 3065

DOM does not match PHP output

I have the following PHP script:

    function drawDepts(mysqli $con, $defaultDept) {

    $deptQuery = "SELECT * FROM depts";
    $deptResult = $con->query($deptQuery);
    global $request;

    echo "<select id='targetDept' width='200' style='width:200px;' onchange='updateRequest(".$request['id'].", 'targetDept', $('#targetDept').val()'>";

    while ($deptRow = $deptResult->fetch_array(MYSQL_ASSOC)) {
        echo "<option value='" . $deptRow['id'] . "' ";
        if ($deptRow['id'] == $defaultDept) {
            echo "selected='selected'";
        }
        echo ">" . $deptRow['name'] . "</option>";
    }

    echo "</select>";
}

This basically just creates a select box with options from a DB. The onchange allows the user to update the DB without re-loading the page (ajax).

The weird part is the DOM shows this (IE 11):

<select id="targetDept" style="width: 200px;" onchange="updateRequest(212, " $('#targetdept').val())'="" targetdept',="" width="200">

Specifically, why are there " , ="" and width="200px"

echoing the javascript function as text works just fine... It seems that IE is screwing up the onchange tag.

Upvotes: 1

Views: 28

Answers (1)

ahervin
ahervin

Reputation: 461

You have a syntax error on your onchange with ' and " use \ to escape the ' used within your onChange with the ''s

echo "<select id='targetDept' width='200' style='width:200px;' onchange='updateRequest(".$request['id'].", \"targetDept\", $(\"#targetDept\").val())'>";

Upvotes: 2

Related Questions