Passing all values into one variable then split them [duplicate] - php

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>";
}

Related

submit form data from a foreach loop

I have a foreach loop with a form inside for each result like so:
foreach($this->results() as $that) {
<form>
<input type="text" name="name[]">
<input type="text" name="this[]">
</form>
}
and so on. My question is how do I each forms data. I understand you can do something like the following:
$_POST['name'][0];
$_POST['name'][1];
etc, but is their a way to get this done without knowng how many forms their will be. I mean like foreach loop the $_POST data and get each form?
Many thanks
foreach ($_POST['name'] as $val) { /* do what you want, want you want with my value */ }
$_POST['name'] is just an array. Use count or any array function you want on it then.
You can do it like this:
foreach ($_POST['name'] as $val => $value) {}
assuming these rows are being generated by a while loop, and the variable is named like this
$val = $row['val'];
And then in your form you'd have something like this:
echo '<input type="hidden" value="'.$val.'" name ="val[]" /><input type="text" name="name['.$val.'] />";
basically the name variable would be identified by the value itself being generated but also appended on a named variable, and then can be fed into your foreach.
foreach ($_POST['name'] as $key => $value) {
echo 'key: '.$key.' Value : '.$value;
}
--------------------------
output key: 0 Value : nameValue1
key: 1 Value : nameValue2
$_POST['name'] is an array, if you get multiple value with key, just get with foreach value

HTML PHP array index lenght error

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/>';
}
}

Handling Multiple Input : Array vs Loop, Which Is Low Resource Consuming?

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.

how to fetch data using foreach

<input name="auth[]" type="text" id=" " value="<?php echo $cite_aut[$i]; ?>">
Above are text fields in html. Now how to get text fields values in a array variable in another php page.
I tried it by using foreach loop (foreach($val as $_POST['author'])... but it fetch 1 extra value for $val, if there are 5 text boxes then it is fetching 6 values, 6th value is "array".
can someone explain how to do it?
Should be:
foreach($_POST['auth'] as $key => $val) {
......
}
TRY
foreach($_POST['auth'] as $key => $val) {
echo $val;
}

How do I echo each value of a foreach loop?

Not sure if the question title actually makes sense.
Anyway, what I'd like to do is be able to echo an individual value from a foreach loop.
Here's my code:
$headlines = array('primary_headline' => $_POST['primary_headline'], 'secondary_headline' => $_POST['secondary_headline'], 'primary_subline' => $_POST['primary_subline'], 'secondary_subtext' => $_POST['secondary_subtext']);
$city_name = "Dallas";
$ref_name = "Facebook";
$searches = array('$city_name', '$ref_name');
$replacements = array($city_name, $ref_name);
if(isset($headlines)) {
foreach($headlines as $headline) {
$headline = str_replace($searches, $replacements, $headline);
echo($headline['primary_headline']); // I thought this would do the trick
}
}
I thought that this would've echoed my city is Dallas when my city is $city_name was posted, unfortunately, this isn't the case and it merely echoes msps, which is the first letter of each input value:
<input name="primary_headline" type="text" value="my city is $city_name" />
<input name="secondary_headline" type="text" value="secondary headline" />
<input name="primary_subline" type="text" value="primary subline" />
<input name="secondary_subtext" type="text" value="secondary subline" />
<input type="submit" value="submit" />
If anyone could point me in the right direction, it would be very much appreciated!! :)
$searches = array('$city_name', '$ref_name');
The single quotes are making $searches literally contain the word $city_name, not the VALUE of $city_name. You don't need quotes while assigning variables:
$searches = array($city_name, $ref_name);
unless, of course, you're doing some kind of templating system and trying to do variable interpolation without eval().
Change
echo($headline['primary_headline']); // I thought this would do the trick
To
echo($headline) . PHP_EOL; // I thought this would do the trick
When you are using foreach you do not need to specify an index to the element, because foreach will handle iterating for you, so when you dereference something inside the loop, you are asking for a character from the string. Here you get the first character because 'primary_headline' is being interpreted as a 0.
$headlines = array('primary_headline' => $_POST['primary_headline'], 'secondary_headline' => $_POST['secondary_headline'], 'primary_subline' => $_POST['primary_subline'], 'secondary_subtext' => $_POST['secondary_subtext']);
This creates an array with key=>value pairs, not a multidimensional array. Looping through this array in a foreach loop will return only the values, i.e. $_POST['primary_headline'] for the first iteration, $_POST['secondary_headline'] for the second iteration, etc. This is why you're unable to access $headline['primary_headline'].
If you want to access "my city is Dallas" per your example, simply echo $headlines['primary_headline'].
If you want to echo each value:
foreach($headlines as $headline) {
echo $headline . PHP_EOL;
}
For good measure figured I should close this question with an answer (I've already placed it in the comments section as I couldn't add an answer).
The ampersand (&) must be used in order to assign a reference to the original array $headlines instead of copying it's valuables, thus updating it with any values created within the foreach() loop.
So, my code is now;
$headlines = array('primary_headline' => $_POST['primary_headline'], 'secondary_headline' => $_POST['secondary_headline'], 'primary_subline' => $_POST['primary_subline'], 'secondary_subtext' => $_POST['secondary_subtext']);
$city_name = "Dallas";
$ref_name = "Facebook";
$searches = array('$city_name', '$ref_name');
$replacements = array($city_name, $ref_name);
if(isset($headlines)) {
foreach($headlines as &$headline) {
$headline = str_replace($searches, $replacements, $headline);
}
echo ($headlines['primary_headline']) // This now has the value of $_POST['primary_headline'] along with the str_replace() function assigned to $headline
}

Categories