0

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 Answers 3

2

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}";
Sign up to request clarification or add additional context in comments.

7 Comments

Careful about accidental HTML injection: use htmlspecialchars or something.
@Waleed Khan - Can u show me how to use htmlspecialchars in this situation? Many thanks. ($vals = response generate from hitting a submit button) thanks again
@Waleed Khan - thanks for this, but my concern was do I need to use full entity translation mechanism even when I'm getting a posted value from button as well?
As you seem to be working with numbers only, you could validate/sanitize your values with filter_var_array() - I'll update my answer after having finished this comment.
|
0

Instead of print_r, which prints out readable information about a variable, use the implode command:

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

Comments

0

You might want to look into implode().

Your entire for loop can be replaced by:

echo implode(" ", $a);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.