PHP动态$ _POST变量

时间:2014-12-31 02:31:17

标签: php

我希望以一种形式获取文本框的$ _POST值。

文本框的名称会根据与之关联的用户而更改。

<input type='text' name='$tmpUsername.mondstart' value='$tmpmonStart' placeholder='Start' size='3' maxlength='5'>

即。 name will = bing.lee.mondstart

我尝试过使用:

$tmp = $tmpUsername . $_POST['mondstart']
$tmp = $_POST[$tmpUsername . 'mondstart']

谢谢。

完整源代码:

echo "<form name='employeehours' method='post' action='../path/'>\n";
echo "<table align=center width=210 border=0 cellpadding=7 cellspacing=0 id='createformtable'>\n";



while ($row=mysql_fetch_array($result)){

$timeformat = "hh:mm";
$tmpUsername = $row['Username'];
$tmpmonStart = substr($row['monStart'], 0, 5);
$tmpmonStop = substr($row['monStop'], 0, 5);
$tmptueStart = substr($row['tueStart'], 0, 5);
$tmptueStop = substr($row['tueStop'], 0, 5);
$tmpwedStart = substr($row['wedStart'], 0, 5);
$tmpwedStop = substr($row['wedStop'], 0, 5);
$tmpthuStart = substr($row['thuStart'], 0, 5);
$tmpthuStop = substr($row['thuStop'], 0, 5);
$tmpfriStart = substr($row['friStart'], 0, 5);
$tmpfriStop = substr($row['friStop'], 0, 5);
$tmpsatStart = substr($row['satStart'], 0, 5);
$tmpsatStop = substr($row['satStop'], 0, 5);
$tmpsunStart = substr($row['sunStart'], 0, 5);
$tmpsunStop = substr($row['sunStop'], 0, 5);


echo "<tr class='border_bottom'>\n";
echo "<td colspan=14><input type='checkbox' name='users[]' value='$tmpUsername'> #$tmpUsername</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td colspan=2 align='center'>Monday</td>\n";
echo "<td colspan=2 align='center'>Tuesday</td>\n";
echo "<td colspan=2 align='center'>Wednesday</td>\n";
echo "<td colspan=2 align='center'>Thursday</td>\n";
echo "<td colspan=2 align='center'>Friday</td>\n";
echo "<td colspan=2 align='center'>Saturday</td>\n";
echo "<td colspan=2 align='center'>Sunday</td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td><input type='text' name='$tmpUsername.mondstart' value='$tmpmonStart' placeholder='Start' size='3' maxlength='5'></td> \n";
echo "<td><input type='text' name='$tmpUsername.mondstop' value='$tmpmonStop' placeholder='Finish' size='3' maxlength='5'></td>\n";
echo "<td><input type='text' name='$tmpUsername.tuedstart' value='$tmptueStart' placeholder='Start' size='3' maxlength='5'></td> \n";
echo "<td><input type='text' name='$tmpUsername.tuedstop' value='$tmptueStop' placeholder='Finish' size='3' maxlength='5'></td>\n";
echo "<td><input type='text' name='$tmpUsername.weddstart' value='$tmpwedStart' placeholder='Start' size='3' maxlength='5'></td> \n";
echo "<td><input type='text' name='$tmpUsername.weddstop' value='$tmpwedStop' placeholder='Finish' size='3' maxlength='5'></td>\n";
echo "<td><input type='text' name='$tmpUsername.thudstart' value='$tmpthuStart' placeholder='Start' size='3' maxlength='5'></td> \n";
echo "<td><input type='text' name='$tmpUsername.thudstop' value='$tmpthuStop' placeholder='Finish' size='3' maxlength='5'></td>\n";
echo "<td><input type='text' name='$tmpUsername.fridstart' value='$tmpfriStart' placeholder='Start' size='3' maxlength='5'></td> \n";
echo "<td><input type='text' name='$tmpUsername.fridstop' value='$tmpfriStop' placeholder='Finish' size='3' maxlength='5'></td>\n";
echo "<td><input type='text' name='$tmpUsername.satdstart' value='$tmpsatStart' placeholder='Start' size='3' maxlength='5'></td> \n";
echo "<td><input type='text' name='$tmpUsername.satdstop' value='$tmpsatStop' placeholder='Finish' size='3' maxlength='5'></td>\n";
echo "<td><input type='text' name='$tmpUsername.sundstart' value='$tmpsunStart' placeholder='Start' size='3' maxlength='5'></td> \n";
echo "<td><input type='text' name='$tmpUsername.sundstop' value='$tmpsunStop' placeholder='Finish' size='3' maxlength='5'></td>\n";
echo "</tr>\n";
echo "<tr><td colspan='14' style='height: 5%;'></td></tr>";
}

echo "<tr>\n";
echo "<td colspan='14' align = center><input type = submit></td>\n";
echo "</tr>\n";
echo "</table>\n";
echo "</form>";



if ($_SERVER["REQUEST_METHOD"] == "POST") {
$updateUsers = $_POST['users'];

foreach($updateUsers as $user){
var_dump($user);
$tmp = $_POST[$user . '.mondstart'];

//$tmp =  $_POST[$user.'mondstart'];
echo $tmp;

echo $user;

}

2 个答案:

答案 0 :(得分:0)

取决于是否为数组。如果它们是字符串,请尝试使用连接赋值运算符。

foreach($updateUsers as $user){

$tmp = $user;
$tmp .= $_POST['mondstart'];

echo $tmp;
}

此外,

$tmp = $user . $_POST['mondstart'];

应该有效。但如果你需要一个空格,

$tmp = $user." ".$_POST['mondstart'];

如果其中一个是数组,则需要使用implode()

答案 1 :(得分:0)

假设name属性正确并且与源中的name='bing.lee.mondstart'类似(请确保通过浏览器查看页面的来源),您缺少点$_POST变量中。因此,请尝试使用以下语句:

$tmp = $_POST[$tmpUsername . '.mondstart'];

$tmp变量将具有您需要的值。