I want to save values send via POST in a session array:
$reply = array('thread_id', 'reply_content');
$_POST['thread_id'] = 2; # test it
$_SESSION['reply'] = array();
foreach ($reply as $key)
{
if (in_array($key, $_POST))
{
$_SESSION['reply'][$key] = $_POST[$key];
}
}
var_dump($_SESSION['reply']);
For example I want to check if the keys 'thread_id' and 'thread_content' are send in post, if they are then I want to save them in a session array called reply, using the same keys.
So for example if 'thread_id' is send via POST:
$_POST['thread_id'] = 'blah';
Then this should get saved in a session called 'reply', using the same key:
$_SESSION['reply']['thread_id'] = 'blah';
How can this be done?
In general, your approach looks valid, but I'm going to guess that you may not be calling session_start(), which is necessary to persist session data.
session_start();
if(!$_SESSION['POST']) $_SESSION['POST'] = array();
foreach ($_POST as $key => $value) {
$_SESSION['POST'][$key] = $value;
}
var_dump($_SESSION['POST']);
in_array($needle, $haystack) checks whether $needle is a value in $haystack and not a key. Use array_key_exists or isset instead:
foreach ($reply as $key)
{
if (array_key_exists($key, $_POST))
{
$_SESSION['reply'][$key] = $_POST[$key];
}
}
Or:
$_SESSION['reply'] = array_merge($_SESSION['reply'], array_intersect_key($_POST, array_flip($reply)));
Use this
$reply = array('thread_id', 'reply_content');
$_POST['thread_id'] = 2; # test it
$_SESSION['reply'] = array();
foreach ($reply as $key)
{
if (isset($_POST[$key]))
{
$_SESSION['reply'][$key] = $_POST[$key];
}
}
Related
Im using a function to update $_SESSION data
function session_values($key, $value)
{
if(empty($_SESSION[$key])
{
$_SESSION[$key] = $value;
}else if($_SESSION[$key] != $value)
{
$_SESSION[$key] = $value;
};
};
session_values($key, $value);
question is, how to use this function, add a $_SESSION value equal to
$_SESSION['thispage']['signup']['name'] = 'Bob';
for example.
Like this:
$key = 'thispage';
$value = ['signup' => ['name' => 'Bob']];
session_values($key, $value);
This should have the same effect as
$_SESSION['thispage']['signup']['name'] = 'Bob';
My programm read info from textbox and show it to user, so the problems is in middle part of that operation. How to make $myArray don't re-create itself when i update page?
session_start();
if (!isset($myArray)) $myArray = array();
$myArray[] = $_POST['nameAuthor'];
foreach($myArray as $key=>$value)
{
echo "$key->$value";
}
//$_SESSION['arr'] = $myArray;
Try this:
session_start();
if(!isset($_SESSION['arr'])) $_SESSION['arr'] = array();
$_SESSION['arr'][] = $_POST['nameAuthor'];
foreach($_SESSION['arr'] as $key => $value)
{
echo "$key->$value";
}
If I have several post results that are like this:
$_POST["ResponseA"] = 1, $_POST["ResponseB"] = 1, $_POST["ResponseC"] = 2, $_POST["ResponseD"] = 3, $_POST["ResponseE"] = 1, etc.
How can I perform a loop that gets an array based upon the values? So If I'm checking for a value of 1, I get ResponseA, ResponseB, ResponseE ?
<?php
$results = array_keys($_POST, 1);
var_dump($results);
?>
Use array_flip() like this...
$flipped = array_flip($_POST);
echo $flipped['1']; // ResponseA
You will get issues doing this though as your values are not unique
simple build a loop
<?php
$fields = array('ResponseA','ResponseB','ResponseC','ResponseD','ResponseE')
function searchValue(array $fields, $value) {
$out = array();
foreach($fields as $name) {
if(isset($_POST[$name]) && $_POST[$name] == $value) $out[]=$name;
}
return $out;
}
var_dump(searchValue($fields,1));
You can loop through the $_POST:
foreach ($_POST as $key => $value) {
if ($value == 1) {
// $key will equal the value ResponseA if $_POST["ResponseA"] = 1
}
}
Hi want to build a program which creates surveys. I couldn' t figure out how can i assign value for a question which is unanswered. Thank you for your helps.
$dizi = array();
foreach ( $_POST as $key => $value){
if(empty($_POST)){
$_POST="bos";
}
$dizi[$key] = "'".$value."'";
}
Your code doesn't make sense, try this:
$dizi = array();
foreach($_POST as $key => $value) {
if (empty($value)) {
$value = 'your value';
}
$dizi[$key] = $value;
}
$_POST is an associative array
So you can access it with:
$bla = $_POST['bla'];
What you are trying to do is setting the whole array to a string which doesn't work.
You should set the new value when saving it to the $dizi array.
$dizi = array();
foreach($_POST as $key => $value) {
$newValue = $value;
if (empty($value)) {
$newValue = 'bos';
}
$dizi[$key] = $newValue;
unset($newValue);
}
But this only checks if answer string is empty. So this only works if all questions are mandatory.
If I understood you correctly, what you are trying to do is this:
foreach ( $_POST as $key => $value ) {
if(empty($value))
$_POST[$key] = 'This is an unanswered question!';
}
But this cannot work due to the fact that empty values aren't posted from the form.
How do you know that there is 'unanswered' question if it was not posted from the form?
You have to start from the list of the questions (which can not be forged by the user and is defined on the server-side) and check that answer for each of them exists in $_POST. If not - assign whatever you want to the skipped answers.
Try this:
if(isset($_POST) && (!empty($_POST))){
foreach ( $_POST as $key => $value ) {
if(empty($value)){
$_POST="bos";
} else{
//put your code
}
}
}
I have a small script that sets multiple cookies, they all have this format item_1928 item_3847 item_5782 etc.
I need to get all the values for the cookies that start with item and store them in an array.
Here's is some code I found on SO but I'm not sure it's what I'm looking for. It just stores the key, but not the values:
$matches = array();
foreach($_COOKIE as $key => $value) {
if(substr($key, 0, 20) == 'wordpress_logged_in_') {
$matches[] = $key;
}
}
You can try this:
foreach($_COOKIE as $key => $value) {
if(strstr($key ,"item_")) {
$matches[$key] = $value;
}
}
You should be able to modify that code like this:
$matches = array();
$values = array();
foreach($_COOKIE as $key => $value) {
if(substr($key, 0, 20) == 'wordpress_logged_in_') {
$matches[] = $key;
$values[] = $_COOKIE[$key];
}
}
Then you'll have all the values (and NOT the keys) in the $values array.