0

I'm not quite sure where I'm going wrong :(

if(isset($_POST['finish'])){
$objectives=addslashes(strip_tags($_POST['item']));

    foreach ($objectives AS $objective) {
        echo "$objective <br />";
    }

}

It's not showing anything.. what have I missed out? I'm trying to get data from multiple input entries..

<input class="item" id="objectives" name="item[]" />

Any ideas?

8
  • 2
    well, the first thing you missed is the type of your input... Commented May 24, 2013 at 16:17
  • addslashes and strip_tags work on string literals, not arrays. error_reporting(E_ALL) would have told you that immediately. Commented May 24, 2013 at 16:17
  • $_POST['item'] is an array Commented May 24, 2013 at 16:17
  • 1
    what is the value of $_POST['finish'] ? Is the block even getting run Commented May 24, 2013 at 16:18
  • 1
    @Bartdude Note: The default type is: text. see: w3schools.com/tags/att_input_type.asp Commented May 24, 2013 at 16:27

2 Answers 2

5

Well if you have multiple <input class="item" id="objectives" name="item[]" /> then $_POST['item'] will be an array and not a string. So you have to iterate over it or apply an array_map function.

$items = array_map('strip_tags',$items);
$items = array_map('addslashes',$items);

Your code would then be

if(isset($_POST['finish'])){
    $_POST['item'] = array_map('strip_tags',$_POST['item']);
    $_POST['item'] = array_map('addslashes',$_POST['item']);

    foreach ($_POST['item'] AS $objective) {
        echo "$objective <br />";
    }

}
Sign up to request clarification or add additional context in comments.

10 Comments

Ok, how do i go about doing that properly?
Haha, def need to keep practicing this... This most likely won't work.. well... it doesn't haha but I'm not getting any errors in my log... if(isset($_POST['finish'])){ $items=addslashes(strip_tags($_POST['item'])); $items = array_map('strip_tags',$items); $items = array_map('addslashes',$items); echo $items; }
You need to remove $items=addslashes(strip_tags($_POST['item'])); Also $items is intended to represent $_POST['item'].
Ok, starting to make sense, could you possible struture it to actually work for me... because i removed it.. it doesn't even echo anything out.. feel like a dumb ass at the moment, but yeah, it happens.. thanks nick!
"INSERT INTO objectives (objective) VALUES ('$objective')" Need quotes around a string you are inserting.
|
0

The direct answer is that any tag that has brackets([]) is put in the superglobal array as an array. You will need to loop over your this or use array_map to perform functions on this array.

My extended answer is that if you are using php 5.2 or later you can use the filter_var_array to perform this operation without iterating over your array in php. As well as do type checking. filter_var and filter_var_array have to many filter options for me to cover. Please see the docs http://php.net/manual/en/function.filter-var-array.php

if(isset($_POST['finish'])) {
    $objectives = filter_var_array($_POST['item'], FILTER_SANITIZE_STRING);

foreach ($objectives AS $objective) {
    echo "$objective <br />";
}

}

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.