doupaoyan6083
2018-03-25 02:47
浏览 36
已采纳

$ _POST和变量在字符串中 - 不知道为什么它不起作用

I have this non-dynamic script works like this:

<p>Environment: <select name="formEnvironment" value="" >
<option value="TEST" <?= $_POST['formEnvironment'] == TEST ? 'selected' : '' ?>>Test</option>
<option value="PROD" <?= $_POST['formEnvironment'] == PROD ? 'selected' : '' ?>>Production</option>
</select></p>

As I have tried to make it more dynamic as possible within variables. I have problems with code within while nest and I do think it has to do with strings and variables mixing together which makes it more complicated. Any assistance with what is right way to code in that regard. I would appreciate assistance as I am only few months into PHP.

function DropDownItems($itemName, $query, $connection)
{
    $result = mysqli_query($connection, $query);

    while ($row = $result->fetch_assoc()){
        $MAS = "$_POST['form"{$itemName}"']";
        echo "<option value=\"{$row['DDVALUE']}\" ";
        echo "<?= "{$MAS}" == "{$row['DDVALUE']}" ? 'selected' : '' ?>>"{$row['DDTEXT']}"</option>";

    }
}

$itemName="Environment";
$query = "SELECT DDVALUE, DDTEXT FROM DDLISTS WHERE FIELDNAME='".$itemName."' ORDER BY DDTEXT ASC";
echo "<p>$itemName.": <select name='form".$itemName."'>";
echo "<option value=''>Select below:</option>";
DropDownItems($itemName, $query, $connection);
echo "</select>";

2条回答 默认 最新

相关推荐 更多相似问题