I've got a from, with 2 information from 2 field.
Everytime you submit the form you came to another page where I want to display them in an array.
What I did is:
$buchtyp = $_POST['buchtyp'];
$a_id_warenkorb = $_POST['a_id_warenkorb'];
$_SESSION['warenkorb'][$a_id_warenkorb] = $a_id_warenkorb;
If I show the content of this array, it failed.
$buchtyp = $_POST['buchtyp'];
$_SESSION['buchtyp'][] = array($buchtyp);
$a_id_warenkorb = $_POST['a_id_warenkorb'];
$_SESSION['warenkorb'][] = array($a_id_warenkorb);
I changed it and now it works. Its not saved in one array, but its fine :)
Try
foreach($_POST as $key => $value) {
$_SESSION[$key] = $value;
}
Related
I have 2 pages in php. The 1st page includes a form which transimts data to the 2nd page. The form uses method=post. Data transmitted successfully in the 2nd page. I have the following code, which gets data and printing them using the code:
php?
var_dump($_POST);
foreach ($_POST as $key => $value) {
echo $value;
}
?>
All I want is to extract data from array and place them into variables, because I want to use these varaibles later in some if startments and mysql queries. Any idea how can i do this?
First, these really are basic PHP skills (or programming skills for that matter). Try to follow some tutorials or courses before attempting to write code in the "real world".
As long as you know the key for the value you want to store, this is how you do it:
$yourVariableName = $yourArray['yourKey']; // or just a number if the key is an int
You don't need for loops to do this.
EDIT
$kentroName = $_POST['kentro_name'];
$kentroSurName = $POST['kentro_surname'];
// And then the following six.
<?php
var_dump($_POST);
$array = array();
foreach ($_POST as $key => $value) {
echo $value;
$array[$key] = $value;
}
print_r($array);
?>
I've two page , first page there's an array data and second page I want call array data
Like this
First page index.php
$array_data[]=$array_tmp;
print_r($array_data); // array can display in this page
$_SESSION['one'] = $array_data;
Second page next.php
I want to call array from first page
session_start();
$array = $_SESSION['one'];
foreach( $array as $key => $value ) {
echo $value;
}
print_r($_SESSION['one'])
May I know what's wrong? As array can't display in second page.
I think you should change this code $array = $_SESSION['one']; to $array[] = $_SESSION['one'];. I`m not sure & not tested as well but I think so. Hope this will help.
You need to start the session if you already have not. Without starting the session, you can't assign value to the session variable. So the first code snippet would be like this:
session_start();
$array_data[]=$array_tmp;
print_r($array_data); // array can display in this page
$_SESSION['one'] = $array_data;
Second snippet looks fine but the last line is missing a semicolon. It might sound silly, but that can prevent the whole script from running. Here's the code fixed.
session_start();
$array = $_SESSION['one'];
foreach( $array as $key => $value ) {
echo $value;
}
print_r($_SESSION['one']);
Post more code if this does not work.
Okay so I have this page with a form where you can select a lot of stuff that is then send by POST to the next stage - The name field is tilbehor_ and then ther id from the db. - examble "tilbehor_23".
Now on the next page I need to make them into a variable so I can call on ther value later.
Example.
tilbehor_11, tilbehor_34 and tilbehor_65 are send by POST to the next page, and I needd to show ther value on the page somwere by the use of a variable
How would i do that?
You can use foreach:
foreach($_POST as $key => $val) {
// $key is now 'tilbehor_23' and $val is its value
}
foreach($_POST as $key => $value) {
echo "POST parameter '$key' has '$value'";
echo $_POST['tilbehor_'.$value];
}
The above will display all the data which is posted. and you just check what is coming and what you want to display.
Let's say a POST variable 'tilbehor_34' has a value of '1'. When you do this:
$key = 'tilbehor_34';
$$key = $_POST['tilbehor_34'];
The variable $tilbehor_34 will now have a value of '1'
I create a php array on the fly like below in php, which is then json_encoded and sent back to the ajax script that requested it.
$myarr['key_a'] = 'a';
$myarr['key_b'] = 'b';
$myarr['key_c'] = 'c';
Before I do the json_encode, since the values for this come from a database, is there someway I can check if all values are set and none are blank or unset without having to check each key individually?
if (count($myarr) != count(array_filter($myarr))) {
// Oops, empty values
}
//$arr is your array contains values from database
$newArr = array();
foreach($arr as $key => $val) {
if(trim($val) != ''){
$newArr[$key] = $val;
}
}
json_encode($newArr);
If you don't want to go running around and checking each key individually (by using foreach), you should be making sure that the generated array is already checked on creation.
Adding a if(empty($value)) { // Do stuff } might fix your problems at its core.
I've been working on trying to write a function that will grab the POST values of any given form submission, pop them into an array, loop through the array using trim, addslashes etcetera pass that value back to a variable where it can then be passed to a database.
Now the hurdle I have atm is getting all the input,textarea,select element data into an array upon form submission. code I have follows
$fields = array($_POST['1'], $_POST['2']);
$i = 0;
foreach ($fields as $field) {
$i++;
${'field'.$i } = trim(addslashes(strip_tags($field)));
echo "POST field info #". $i ." - ". ${'field'.$i }."<br />";
}
As you can see everything is fine here baring that the POST value names are still being in-putted statically, what I need is a way to get that POST data fed into a loop which dynamically calls the POST name using an increment variable and then pop all that data into the same array. Code I have tried follows.
for ($ii=0;$ii++;) {
foreach($_POST['$ii'] as $field) {
$fields = array($field);
}
}
$i = 0;
foreach ($fields as $field) {
$i++;
${'field'.$i } = trim(addslashes(strip_tags($field)));
echo "POST field info #". $i ." - ". ${'field'.$i }."<br />";
}
Now I know this wont work but I can sense I am relatively close, so I am wondering if any clever person can help me sort the last part out? I sadly am now going to sleep and wont be viewing this post for at least 9 hours, apologies.
Thanks in advance.
Dan.
$arrayOfPostValues = $_POST; // it already is an array
$arrayOfPostValues = array_map('strip_tags', $arrayOfPostValues);
$arrayOfPostValues = array_map('trim', $arrayOfPostValues);
Or, if you really, really want to use a loop:
foreach ($arrayOfPostValues as &$value) {
$value = trim(striptags($value));
}
I'd absolutely advise against the use of addslashes, it serves very little purpose. Use mysql_real_escape_string or prepared statements instead.
I'd also advise against breaking the vales out of the array into separate variables, it can only cause problems. If you really want to do it, there's the extract function, which does exactly that. But, again, don't do it. Arrays are the perfect way to handle this kind of data.
You need to assign values to $_POST[1] and $_POST[2] to begin with, I've done this for you but normally they would be populated from a form I assume?
I'm not sure why you want to do this sort of thing: ${'field'.$key}, but I've left that part as is as I assume you must have a reason.
Anyway I've modified your code a bit, see below.
$_POST['1'] = '<h1>variable 1</h1>';
$_POST['2'] = '<h2>variable 2</h2>';
foreach($_POST as $key => $value){
${'field'.$key} = trim(addslashes(strip_tags($value)));
echo "POST field info #". $key ." = ". ${'field'.$key}."<br />";
}
The above code outputs:
POST field info #1 = variable 1
POST field info #2 = variable 2
On a side note, using field names such as '1' and '2' is not very good. Try using something more descriptive but as I said above I assume you have a reason for doing this.
UPDATE:
You can still get this to work for any form even if you are using specific names for the form elements. I have added a few lines below as an example for you.
$_POST['email'] = 'example#example.com';
$_POST['password'] = 'hgbks78db';
$_POST['name'] = '';
foreach($_POST as $key => $value){
if($value==''){
echo 'POST field "'.$key . '" is empty<br />';
/* I added the name of the field that is empty to an error array
so you have a way of storing all blank fields */
$a_error[] = $key;
}
else{
echo 'POST field "'.$key . '" is not empty<br />';
}
}