开发者

Expected semicolon in dynamically created form element

I'm working on a php script which will:

1. Connect to a database

2. Query the database for a string input by the user

3. Display each row returned by the query as a dynamically created form. Each form has two 'hidden' input fields, which will be used to populate a separate page with some images and text. The form is submitted through a link, which uses the onclick attribute in conjunction with the javascript 'document.form.submit()' to handle submitting the form.

The problem that I'm having is that each form created causes the page to throw a 'semicolon expected' error, stating that I should have a semicolon in the middle of a word in my code! I am 99.99% certain that whatever is causing this error is somewhere in the code below, but I've been staring at it for quite a while now and I'm stumped.

// Run a query against each field to search for input
foreach($columns as $field){
$searchquery = 'SELECT * FROM mytable WHERE '.$field.' LIKE \'%'.$input.'%\'';
$results = $db->Execute($searchquery);

// We introduce HTML formatting for each result in $results
foreach($results as $resultset) {
    echo '<div style="border: 2px solid black;">';

    // Each result in $results is an array, where the keys and values correspond to the field names and values of the table
    foreach($resultset as $key=>$value) {
        if($key == 'ID'){ // Query the database by ID for the rows returned by the previous query
            $resultquery = 'SELECT field1, field2, field3 FROM mytable WHERE '.$key.' = \''.$value.'\'';
            $row = $db->Execute($resultquery);
            $row = explode(",", $row);
            $id = $row[2];
            $company = $row[3];
            $prodname = $row[4];
            echo "<form name='rowForm.$i' action='page.php' method='POST'>";
            echo "<input type='hidden' name='Company' value='$company'>";
            echo "<input type='hidden' name='ProductName' value='$prodname'>";
            echo "<a href='path/to/my/page' onclick='javascript:document.rowForm.$i.submit(); return false;'> $company - $prodname ($id) </a>";
            echo "</form>";
            $i++;
        }
    }
    echo '</div>';
}
}

Fyi, $columns is an array which holds the names of all columns in my table. All of the forms get displayed correctly, and everything looks as it should; the only issue being that the links don't work correctly, due to this semicolon error. I'm confident that I've posted all the relevant code, but feel free to let me know if it looks like there's something missing. If anyone can point me in the right direction on this I'd really appreciate it!

EDIT: Here's the 开发者_开发百科html code which is generated by the php above, with some additional whitespace added and the actual values for some variables replaced with {$varname's value} for readibility. Note that this code is generated for each row returned by the sql query.

<div style="border: 2px solid black;">
    <form name='rowForm.0' action='page.php' method='POST'>
        <input type='hidden' name='Company' value='{$company's value}'>
        <input type='hidden' name='ProductName' value='{$prodname's value}'>
        <a href='http://link/to/my/site' onclick='javascript:document.forms[\'rowForm.0\'].submit(); return false;'> {This is the link text} </a>
    </form>  
</div>


Not quite sure, but I guess the characters you are trying to escape in single-quotes aren't being escaped. You should write it like that instead:

$searchquery = "SELECT * FROM mytable WHERE " .$field. " LIKE '% ". $input . "%'";

$resultquery = "SELECT field1, field2, field3 FROM mytable WHERE " .$key. " = '" . $value . "'";

// edit:

My bad, I got the escaping-thing not quite right:

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings. 

http://php.net/manual/en/language.types.string.php


And, as already mentioned, your code above is missing a } at the bottom to close the first foreach ;)


Does this:

document.rowForm.$i.submit()

Amount to this in the HTML source?

document.rowForm..submit()

... cause then I can see a problem...

If not, I'd still go with:

document.forms['rowForm.$i'].submit()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜