0

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?

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

4 Comments

ok any idea how to put this in my code so that i can print all my 8 elements of my array into my variables
Repeat 8 times?
when my form submits my data i get something like : array(8) { ["kentro_name"]=> string(6) "Spyrid" ["kentro_surname"]=> string(5) "Ronis" ["kentro_school"]=> string(10) "startupper" ["kentro_date_register"]=> string(19) "18/6/2018 (18:22:3)" ["kentro_active"]=> string(1) "1" ["kentro_id"]=> string(2) "69" ["kentro_session"]=> string(1) "1" ["tmima"]=> string(2) "a3" } How can I use your code to store these elements into variables using the code you wrote me before?
And this is what I meant with follow some courses or tutorials. You can't expect to write software if you don't know how to assign variables. Just as you can't expect to paint a painting if you don't even know how to hold the brushes. I'll update my answer, but please, take some classes or tutorials.
0
<?php

    var_dump($_POST);
    $array = array();

    foreach ($_POST as $key => $value) {
        echo  $value;
        $array[$key] = $value;
    }

    print_r($array);

?>

4 Comments

Isn't this just copying the array?
and how can i get for example the 3rd elemt of the array and print it in a variable
If the 3rd element is "customers": $customers = $array['customers'][0];
While this may answer the question it's better to add some description on how this answer may help to solve the issue. Please read How do I write a good answer to know more.

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.