0

这是我的代码结构的简明。我需要做的是打印复选框的值(仅当用户选中这些值时)以及作为消息附加的字符串。本质上,消息应该类似于“删除以下内容?10,20,30'

通过我的编码,我得到了类似的东西Delete following?Array ( [0] => 15 [1] => 35 )

有人可以帮我解决这个问题。非常感谢。(注意:并非所有正确的代码都应该被提及<html></html>并呈现与理解相关的内容(我认为足够了)

<?php
if(isset($_POST['submit']))
{
echo "Remove following?";
print_r ($_POST['del']);
}
?>

<html>
<form>
// inside html in a dynamically created table I have the following
<?php
echo "<td class='tbldecor1'><input type='checkbox' name='del[]' value='$cxVal'></td>";
echo "<td class='tbldecor2' style='text-align:center'>".trim($rec["firstrow"])."</td>";
<?
<div><input type="submit" name="deleterec" id="deleterec" value="Delete"></div>
</form>
</html>

编辑:另外我在想,如果我要使用循环打印如何用用户选择的复选框值替换 1、2、3、4、5 值?我想的循环如下;

 <?php 
 $a = array(1, 2, 3, 4, 5); 
 foreach ($a as $b) 
 { 
 print $b . " "; 
 } 
 ?> 
4

3 回答 3

2

你可以在implode的帮助下做到这一点:

$vals = $_POST['del'];
echo "Remove following? " . implode( ", ", $vals );

更新(在@Waleed Khan 关于 HTML 注入的警告之后):

// Make sure everything is a number (non-numeric will be replaced with boolean falses)
$vals = filter_var_array( $_POST['del'], FILTER_VALIDATE_INT ); 
// Remove all boolean falses
$vals = array_filter( $vals );
// String output
$valString = implode( ", ", $vals );
echo "Remove following? {$valString}";
于 2012-10-22T01:49:07.523 回答
0

而不是print_r打印出有关变量的可读信息的 ,请使用以下implode命令:

echo "Delete following? " . implode(", ",$_POST['del']);
于 2012-10-22T01:50:53.153 回答
0

你可能想调查一下implode()

您的整个 for 循环可以替换为:

echo implode(" ", $a);
于 2012-10-22T01:50:59.743 回答