1

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.

1
  • 1
    The issue is because you're using ' to delimit the onchange attribute and also the targetDept string within that. It's a syntax error. You need to escape the inner string, or better yet attach your event handlers using Javascript instead of ugly on* attributes. Commented Nov 10, 2015 at 13:28

1 Answer 1

2

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())'>";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.