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?
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 />";
}
}
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 />";
}
}
Related
This question already has answers here:
Passing an array using an HTML form hidden element
(8 answers)
Closed 3 years ago.
Hi so for the website I am designing I have been asked to use mal's eCommerce for the payment gateway. I have been trying to set up the remote call function they have so that once a purchase has been made it will make a call back to my website to run a script and sen email confirmation of their order.
This however has proved extremely frustrating as the session variables I have stored do not get passed through with the remote call, only certain values that they allow.
I tried storing them all in an array and putting them in one of the values I can change called sd:
$cart =array($_SESSION["date"], $_SESSION["name"], $_SESSION["email"], $_SESSION["number"], $_SESSION["address"], $_SESSION["town"], $_SESSION["postcode"],
$_SESSION["county"], $_SESSION["cake_type"], $_SESSION["collection"], $_SESSION["icingColour"], $_SESSION["trimColour"], $_SESSION["filling"], $_SESSION["wording"],
$_SESSION["cakePhoto"], $_SESSION["price"], $_SESSION["photo"] );
<input type="hidden" name="sd" value="<?php echo $cart?>">
Then in my script I tried:
$result = $_POST['sd'];
echo $result[0];
to test it
but the remote call just passed the value as "Array" so echo $result[0] just returned "A", the values was just passed as Array and none of the values in the array got passed with it.
So now I'm trying to store all my session variables in the sd value like this:
<input type="hidden" name="sd" value="<?php foreach ($_SESSION as $key=>$val) { echo $val;}?>">
and then I tested it by doing on my script:
$result = $_POST['sd'];
echo $result;
Now this does pass all the values but obviously just as one big value, is there a way I can split them up? Is there a better way with the array I haven't thought of? Any advice would be greatly appreciated because I'm at a complete loss
Edit:
Forgot to add this part:
So after the array has been split I would need to stored the values separately so something like:
$name = first value
$email = second value etc
try changing <input type="hidden" name="sd" value="<?php echo $cart?> to
<input type="hidden" name="sd[]" value="<?php echo $cart?>
or use serialize() then unserialize() the result.
Refer to Passing an array using an HTML form hidden element
You can use php implode and explode function.
The first part is implode.
We use ; as implode delimeter which joins array elements as single string using that delimeter.
<input type="hidden" name="sd" value="<?php foreach ($_SESSION as $key=>$val) { echo $val.';';}?>">
Explode converts that string to array again using the delimeter we passed.
foreach (explode(';', $_REQUEST["sd"]) as $key) {
echo $key;
echo "<br>";
}
Update : Try This
<input type="hidden" name="sd" value="<?php foreach ($_SESSION as $key=>$val) { echo $key.';'.$val.';;';}?>">
Explode converts that string to array again using the delimeter we passed.
foreach (explode(';;', $_REQUEST["sd"]) as $key) {
echo explode(';', $key)[0];
echo " = " ;
echo explode(';', $key)[1];
echo "<br>";
}
I have an html form where I am grouping inputs using name="array[]" then just looping through the array with PHP when submitted. Well I am using array[] to store the question, but when the question (array index) is longer than 64 characters then It will not pass that array key to my PHP.
HTML
<textarea name="corporate[CAN YOU SHOW US SIMILAR PROJECTS WITH THE SAME TARGET AUDIENCE? COMPETITORS?]"></textarea>
When I do:
var_dump($_POST['array']);
I get array(0)
But when I use a shorter index, it works.
Now if I manually create an associative array it works fine:
$array = array("CAN YOU SHOW US SIMILAR PROJECTS WITH THE SAME TARGET AUDIENCE? COMPETITORS?"=>"0");
What am I doing wrong?
I think it has to be a problem with going from the html form to the PHP. I am trying to loop through the inputs with my PHP so that I can loop through and display each question and corresponding answer with:
foreach ($array as $key=>$value) {
if ($value != NULL) {
echo '<strong>' . $key . '</strong><br/>';
echo $value . '<br/><br/>';
}
}
Which gives me:
Question
Answer
Question
Answer
etc.
How else could I do this without giving each input their own name to pass the question, or hard coding the question in my HTML?
It might be because you're doing this
var_dump($_POST['array']);
when you named the textarea "corporate". Try just doing
var_dump($_POST);
You can use a hidden input with the question and match up the indexes with the answer:
<input type="hidden" name="question[1]" value="CAN YOU SHOW US BLAH?">
<textarea name="answer[1]"></textarea>
I assume you are dynamically adding the question text? If so you should probably use htmlentities on it to avoid issues.
Then just loop one and access the other:
foreach ($_POST['answer'] as $key => $value) {
if (!empty($value)) {
echo '<strong>' . $_POST['question'][$key] . '</strong><br/>';
echo $value . '<br/><br/>';
}
}
in my $_POST, I have a variable whose name changes. the name is modify_0, but the number at the end changes depending on the button that was pressed.
Is there anyway to check what the number is for that variable in $_POST?
Say:
$_POST['modify_(check for number or any character)']
You would need to iterate over all of the keys within the $_POST variable and take a look at their format:
$post_keys = array_keys( $_POST );
foreach($post_keys as $key){
if ( strpos($key, 'modify_' ) != -1 ){
// here you know that $key contains the word modify
}
}
Besides the correct answers given above, I would recommend changing your code slightly so it's easier to work with.
Instead of having inputs with the format:
// works for all types of input
<input type="..." name="modify_1" />
<input type="..." name="modify_2" />
You should try:
<input type="..." name="modify[1]" />
<input type="..." name="modify[2]" />
This way, you can iterate through your data in the following way:
$modify = $_POST['modify'];
foreach ($modify as $key => $value) {
echo $key . " => " . $value . PHP_EOL;
}
This works especially well for multiselects and checkboxes.
Try something like this:
// First we need to see if there are any keys with names that start with
// "modify_". Note: if you have multiple values called "modify_X", this
// will take out the last one.
foreach ($_POST as $key => $value) {
if (substr($key, 0) == 'modify_') {
$action = $key;
}
}
// Proceed to do whatever you need with $action.
This is my actual code. I am trying to send the array "sku" which has copied the original array of "parent..." to php with $_post. But no matter what I try it won't send.
<script>
var sku = new Array();
for (q=1;q<parent.item_num;q++)
{
sku[q] = parent.itemlist[q].code;
}
</script>
Please help.
If the name attribute is something like name="item[]", then $_POST['item'] is an array, so you can use foreach loop to go through all items.
If your form method is post you can use <?php $_POST['item'] ?> to access the array.
You can also use foreach to loop through all of the items:
<?php
foreach($_POST['item'] as $item){
... do something with $item ...
}
?>
Arrays in HTML, e.g.
<input type="text" name="arr[]" id="arr1" />
<input type="text" name="arr[]" id="arr2" />
simply create arrays under their normal $_GET/$_POST variables, so, var_dump($_REQUEST['arr']) would yield:
arr => array(
[0] => "whatever was in arr1",
[1] => "whatever was in arr2"
)
maybe this will help :
$id = 4;
if (isset($_POST['item_'.$id]))
{
item[$id] = $_POST['item_'.$id];
} else {
item[$id] = 0;
}
PS : slugonamission way seems more appropriate
I have multiple input tag submitted from previous page, say step1.php like this :
<input type="hidden" name="block01" value="001"/>
<input type="hidden" name="block02" value="012"/>
<input type="hidden" name="block03" value="002"/>
<input type="hidden" name="block04" value="005"/>
<input type="hidden" name="block05" value="008"/>
<input type="hidden" name="block06" value="015"/>
now I want to process those inputs in the step2.php and I have 2 options to do it, either using Array or Loop.
If I'm using array, those inputs will be appended like this :
<?php
$stack = array(""); //empty array declared
// I assume I have some codes here to 'catch' those inputs and put it as array_push
array_push($stack, "001", "012", "002", "005", "008", "015");
print_r($stack);
?>
compare to array, I have this LOOP option too :
<?php
$i = 1;
$x = 'block0'.$i;
$webBlock = $_POST[$x];
while (!empty($webBlock)){
$x = 'block0'.$i;
$webBlock = $_POST[$x];
echo $webBlock . "<br />";
$i++;
}
?>
both are solutions of my problem on step2.php. I just need your opinion which is more less memory / cpu consuming? that's all...
thanks!
In first case, you are using array_push() and print_r(). The first function uses a loop to push arguments passed onto a stack. The second function that is print_r() also uses a loop to print all the values of the array. So, basically you are running loop twice to do the task.
Where as in second case, you have written a code to handle both things at once. So, this method just needs to loop once. Moreover, looking into the working of print_r() and echo, if you run echo X times and use print_r() to print X values, the echo is bit faster than print_r. Read php documentation for more info about all these function.
So, the second way is better.