代码:
<?php for($i = 0;$i < count($smslist);$i++){ ?>
<td align="center"><?php echo $smslist[$i] -> getMo_from(); ?></td>
<td align="center"><?php echo $smslist[$i] -> getMo_to(); ?></td>
<td align="center"><?php echo $smslist[$i] -> getCreate_time(); ?></td>
<td align="center"><?php echo $smslist[$i] -> getMsg_content(); ?></td>
<td align="center">
<input type="hidden" name="status" value="<?php echo $status; ?>">
<input type="hidden" name="mobile" value="<?php echo $smslist[$i] -> getMo_to(); ?>">
<input type="hidden" name="msg_content" value="<?php echo $smslist[$i] -> getMsg_content(); ?>">
<input type="submit" name="button" id="button" value="确认" />
</td><?php } ?>
假设 echo $smslist[$i] -> getMsg_content();这里是从1~50,每条数据后有一个确认按钮,点击确认写数据库,现在的问题是我点击任意确认,写入数据库的都是第50条数据,请问那里出现问题了呀?
<?php for($i = 0;$i < count($smslist);$i++){ ?>
<td align="center"><?php echo $smslist[$i] -> getMo_from(); ?></td>
<td align="center"><?php echo $smslist[$i] -> getMo_to(); ?></td>
<td align="center"><?php echo $smslist[$i] -> getCreate_time(); ?></td>
<td align="center"><?php echo $smslist[$i] -> getMsg_content(); ?></td>
<td align="center">
<input type="hidden" name="status" value="<?php echo $status; ?>">
<input type="hidden" name="mobile" value="<?php echo $smslist[$i] -> getMo_to(); ?>">
<input type="hidden" name="msg_content" value="<?php echo $smslist[$i] -> getMsg_content(); ?>">
<input type="submit" name="button" id="button" value="确认" />
</td><?php } ?>
假设 echo $smslist[$i] -> getMsg_content();这里是从1~50,每条数据后有一个确认按钮,点击确认写数据库,现在的问题是我点击任意确认,写入数据库的都是第50条数据,请问那里出现问题了呀?
if(isset($_POST["submit"]))
{
print_r($_POST);
}
?>
<form method="POST">
<input type='text' name="content" value='content111'><br>
<input type='text' name="content" value='content222'><br>
<input type="submit" name="submit" value="提交">
</form>
不行的,把form扔进去一起循环还是提交最后一个
<input type="hidden" name="status" value=" <?php echo $status; ?>">
<input type="hidden" name="mobile" value=" <?php echo $smslist[$i] -> getMo_to(); ?>">
<input type="hidden" name="msg_content" value=" <?php echo $smslist[$i] -> getMsg_content(); ?>"> 你提交到的是同一个php页面,php里面写的是
$_post['status']
$_post['mobile']
$_post['msg_content']
这样你获得的肯定是50个中的最后一个;
解决方法: 把form表单一块写到循环中,是可以的
...
...
<form method="post" runat="server">
<input type="hidden" name="status" value="<?php echo $status; ?>">
<input type="hidden" name="mobile" value="<?php echo $smslist[$i] -> getMo_to(); ?>">
<input type="hidden" name="msg_content" value="<?php echo $smslist[$i] -> getMsg_content(); ?>">
<input type="submit" name="button" id="button" value="确定" />
</from>
我改成这样之后依然不可以啊,确定之后提交的依然是50,费解
$i = $_POST['i'];
$pbegin = $_POST['pbegin'];
$pend = $_POST['pend'] ;
$mobile = $_POST['mobile'];
$status = $_POST['status'];
$p_button = $_POST['button'];
$msg_content = $_POST['msg_content'];//判断是否写数据库
$smsinfo = new mm_full_sms_control($conn);
if ($p_button == "确定")
{
$smsinfo -> insert($mobile, $msg_content);
}
$smslist = $smsinfo -> selectBySMS($mobile, $pbegin, $pend);//列数据显示<td align="center">
<form method="post" runat="server">
<input type="hidden" name="status" value="<?php echo $status; ?>">
<input type="hidden" name="mobile" value="<?php echo $smslist[$i] -> getMo_to(); ?>">
<input type="hidden" name="msg_content" value="<?php echo $smslist[$i] -> getMsg_content(); ?>">
<input type="hidden" name="i" value="<?php echo $i ?>">
<input type="submit" name="button" id="button" value="确定"
onclick="return confirm('确认内容<?php echo $smslist[$i] -> getMsg_content(); ?>')" />
</from>
print_r($_POST);
for($i = 0; $i < 10; $i++)
{
echo "
<form method='post'>
<input type='hidden' name='status' value='$i'>
<input type='hidden' name='mobile' value='$i'>
<input type='hidden' name='msg_content' value='$i'>
<input type='submit' name='button' id='button' value='确认' />
</form>
";
}
运行以上代码你就知道加form到循环里面是可行的。至于为什么提交的都是同一条数据,那要看你是不是获取数据错误,查看页面源代码,是否每个表单的值都是相同的。
你是否是要提交到当前页?如果是,那你代码中没看到循环中的数据做循环
<?php for($i = 0;$i < count($smslist);$i++){ ?>
<td align="center"><?php echo $smslist[$i] -> getMo_from(); ?></td>
<td align="center"><?php echo $smslist[$i] -> getMo_to(); ?></td>
<td align="center"><?php echo $smslist[$i] -> getCreate_time(); ?></td>
<td align="center"><?php echo $smslist[$i] -> getMsg_content(); ?></td>
<td align="center">
<form method="post" runat="server">
<input type="hidden" name="status" value="<?php echo $status; ?>">
<input type="hidden" name="mobile" value="<?php echo $smslist[$i] -> getMo_to(); ?>">
<input type="hidden" name="msg_content" value="<?php echo $smslist[$i] ->getMsg_content(); ?>">
<input type="submit" name="button" id="button" value="确定" onclick="return confirm('确认内容 <?php echo $smslist[$i] -> getMsg_content(); ?>')" />
</from>
<?php } ?>
查看源代码的数据正常的,从1~50顺序排列.表单是提交到当前页面,上面的代码就是循环的完整代码了.$pbegin = $_POST['pbegin'];
$pend = $_POST['pend'] ;
$mobile = $_POST['mobile'];
$status = $_POST['status'];
$p_button = $_POST['button'];
$msg_content = $_POST['msg_content'];
上面是提交后的接收参数$smsinfo = new mm_full_sms_control($conn);
if ($p_button == "确定")
{
$smsinfo -> insert($mobile, $msg_content);
}
$smslist = $smsinfo -> selectBySMS($mobile, $pbegin, $pend);
}上面是判断参数插入数据库.18楼的代码是正确的,可是我插入的数据总是最后一条.
也可以把submit 改成 button 加个onclik事件,事件传参数i,取相应的值,用ajax提交,也可以用一个隐藏表单提交