dsfds2353 2012-10-22 01:44
浏览 40
已采纳

在PHP中显示数组内容后跟一个字符串

Here is a concise of my code structure. What I need to do is print the values of the check boxes (only if those are checked by the user) along with a string attached as a message. In essence the message should be like 'Remove following? 10,20,30'

With my coding I get something like Delete following?Array ( [0] => 15 [1] => 35 )

can someone help me solve this. Many thanks. (Note: Not all the proper code is mentioned that should come under <html></html> and presented what is relevant for understanding (As I believe sufficient)

<?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>

EDIT : Also I'm thinking, if I'm to use a loop for printing how do I replace 1,2,3,4,5 values by check box values that a user picks? The loop I'm thinking would be as follows;

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

3条回答 默认 最新

  • douhandie6615 2012-10-22 01:49
    关注

    You can do it with the help of implode :

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

    UPDATE (after @Waleed Khan's warning about HTML injection):

    // 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}";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统