开发者

Format PHP string for post action

开发者 https://www.devze.com 2023-03-16 16:23 出处:网络
I am using a dropdown box to perform a selection in a form. I have 4 values (\'value1\',\'value2\',\'value3\',\'value4\'). Each are listed in the dropdown box. But I also want an option for all 4 val

I am using a dropdown box to perform a selection in a form.

I have 4 values ('value1','value2','value3','value4'). Each are listed in the dropdown box. But I also want an option for all 4 values for example, here is the array I am currently using for the function that draws my dropdown menu:

$menuValues("'value1','value2','value3','value4'",'value1','value2','value3','value4');

This however produces an empty variable on the action page. I need to retain the single quotes around each value to feed a function that in turn drives a mysql query. Which is why I have used double-quotes to enclose the first value.

This is the dropdown function:

function frDrop($arr,$frm,$dropTi开发者_JAVA百科tle) {
echo "<select name='".$frm."' id='".$frm."'><option value=''>".$dropTitle."...</option>";
    foreach ($arr as $key => $value) {
    echo "<option value='".$value."'>".$value."</option>";
    }
echo "</select>";
}


It is not clear where is function frDrop() called and what does it's argument mean. But for my answer below, I'm assuming that $arr is the array that contains the dropdown options. So,

$arr = array(
"'value1','value2','value3','value4'",
'value1',
'value2',
'value3',
'value4'
);

Now, as I've guessed in my comment above, perhaps the single-quotes in the first array value - "'value1','value2','value3','value4'" - is conflicting with the single quotes in the tag in the foreach() in function frDrop(). This is how the tag will look like for the first value:

<option value=''value1','value2','value3','value4''>'value1','value2','value3','value4'</option>

You might try to escape the single quotes like this:

echo "<option value='".addslashes($value)."'>".$value."</option>";

OR, instead use single quotes to enclose your PHP statement:

echo '<option value="' . $value . '">' . $value . '</option>';

Hope this helps.


    function frDrop($arr,$frm,$dropTitle) {
    $temp   =''
    $temp.="<select name='".$frm."' id='".$frm."'><option value=''>".$dropTitle."...</option>";
        foreach ($arr as $key => $value) {
        $temp.= "<option value='".$value."'>".$value."</option>";
        }
    $temp.="</select>";
    }
echo $temp
0

精彩评论

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