How Can I Post inpputed Array Data in PHP? - php

I've already tried this stuff
$item=$_POST($val['item_id'])
And
$item=$_POST[$val['item_id']]
Any Idea on how to Post my inputted data ?

$_POST isn't a function, it is a special PHP array that reflects the data submitted from a form. So, the second line you got there can work only if the $val['item_id'] has a valid post name key. You should always first check if that key actually exists in the $_POST data array by using isset function like this:
if (isset($_POST[$val['item_id']]) {
$item = $_POST[$val['item_id']];
}
To debug and see all $_POST data, use this code:
<pre><?php
print_r($_POST);
?></pre>

1) form.html
Make sure your form uses POST method.
<form action="submit.php" method="post">
<input name="say" value="Hi">
<input name="to" value="Mom">
<input type="submit" value="Submit">
</form>
2) submit.php
var_export($_POST);
Will result in:
array (
'say' => 'Hi',
'to' => 'Mom',
)
$_POST is not a function, but an array superglobal, which means you can access submitted data thus:
print $_POST['field_name']

Related

Changing value inside array to be editable input field

have a small problem. In this part of code:
<?php
$data = [
"eCheckDetails"=>[
"paymentsReceived"=>$history["transactionSummary"]["eCheckTotal"],
"revenueReported"=>$history["transactionSummary"]["eCheckTotal"],
"fundsDeposited"=>$history["transactionSummary"]["eCheckTotal"],
"accountAdjustment"=>0.00],
"paymentCardDetails"=>[
"paymentsReceived"=> $history["transactionSummary"]["paymentCardTotal"],
"revenueReported"=> $history["transactionSummary"]["paymentCardTotal"],
"fundsDeposited"=> $history["transactionSummary"]["paymentCardTotal"],
"accountAdjustment"=>0]
];
data " $history [...][...]"
is taken from another file or database (its not really important from where)
Point is, that this data is sometimes incorrect, and needs to be changed manually. And this is my question. How to make this fields (where $history [..] [..] is) editable, to be
<input type="text">
(with small button ACCEPT or smg somewhere aside) with default value hidden under $history[..][..].
I tried to do it, but its inside array and didnt have any luck. Maybe someone knows?
Best regards
You can use named keys in your HTML attributes, for example <input ... name="history[transactionSummary][eCheckTotal]">. Submitting this back to the server will fill your array.
<?php
$form = <<<EOS
<form method="post" action="">
<input type="text" value="" name="history[transactionSummary][eCheckTotal]">
</form>
EOS;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
var_dump($_POST);
} else {
echo $form;
}
The content of the $_POST superglobal will be:
Array
(
[history] => Array
(
[transactionSummary] => Array
(
[eCheckTotal] => dsdsa
)
)
)

can't be able to understand $_post in codeigniter

using codeigniter mvc pattern I create form in view that take only two values form user
<form action="<?php base_url(); ?> blogs/new_post" method="POST">
<label>Title</label>
<input type="text" name="post_title" />
<label>discription</label>
<input type="text" name="post_detail" />
<input type="submit" value="post" />
</form>
now when i submit the form, data goes to the controller now here confusion created in my code that i can't able to understand i use three cases in controller fist is if i use !empty($_POST) in controller and in view weather i fill the form or not fill the form message displayed in controller is post
my question is why always displaying post why not displaying not a post when i fill nothing in the form
if(!empty($_POST)) {
echo "post";
} else {
echo "not a post";
}
my second question is same related to the first condition now i use isset instead of !empty
public function new_Post() {
if(isset($_POST)) {
echo "post";
} else {
echo "not a post";
}
}
in this case either i fill form or not fill form when i submit the form the result always same that is "post"
and in third case if i use !isset the the result is always not a post eiter i fill or not fill the form
hope so you will understand my problem when i comes to if(!empty($_POST)) this condition then my mind is confuse what is the purpose of $_post
This is because you are using the whole global variable $_POST to check empty and isset(). $_POST is not empty when you click on the button. You just Print_r the $_POST it will have the value of submit button. You need to print the value of $_POST on click and see the values in the array
Well, in CodeIgniter (and most of the framework) doesn't allow you to access $_POST directly due to security reason.
You must access $_POST values through $this->input->post()
For more information read Input Class from CodeIgniter's docs
https://www.codeigniter.com/user_guide/libraries/input.html
$_POST is exist when you click on the button:
so empty and isset cant work Correctly
the value at the first is
Array
(
)
SO U should check
if($_POST)
instead
if(!empty($_POST)) OR if(isset($_POST))
AND best way is u check
if ($this->input->post())

Insert _Post response into avariable

Search page looks like this
<form method="post" action="response.php">
<input type="text" name="varianta" value="var_varianta">
<input type="submit">
</form>
<?php
session_start();
$var_varianta= $_POST['varianta'];
?>
The response page has the below line code:
$filters[] = array("filterType" => "resultLimit", "filterValue" => "varianta");
How i can i enter tn the "filterValue" => "**varianta**"); the $var_varianta that is inputed in the search field
Thanks
It's not clear what you're asking, but it seems like there's some confusion. $_POST contains the values that have been submitted, not the values that a user is entering. So, $_POST variables are set on the action page, not on the page showing the form. You can't set a $_POST variable in PHP on the page showing the form and expect the action page to know about that.
If I understand your question correctly, you should use $_POST['varianta'] on the action page to access whatever was submitted.

Handling post and session values in form submit PHP

I have a code like this.
What I'm trying to do is:
Check whether post values is there, if yes - execute my query and display the result.
Or check for session value, if session is there play with session value and display the result.
When I print the session value, it is getting printed (i.e author_id is there in session)
But it is not coming inside the IF loop. Why?
In other words, I want to check for post values or session value, and display the record accordingly.
PS: If i remove the || condition (i.e isset ( $_SESSION ['author_id'] ) ) my code words perfect.
<?php
session_start();
print_r($_SESSION);
if (isset ( $_POST ['submit'] ) || isset ( $_SESSION ['author_id'] )) {
.. do something with my post values or session values ..
}
?>
<form method="post" id="myform1" name="myform1">
........... my form fields..............
<input type="submit" name="submit" value="Submit" />
</form>
Important: Instead of using session or cookie, is there a way to handle the form input field values in the same page?
Try check with Only $_POST
if (isset( $_POST)||isset ( $_SESSION ['author_id'] )) {
.. do something with my post values or session values ..
}
You can pass the value to java script function in onclick() and you can do certain amount of processing in that function

Is there a way to use multiple variables inside a $_GET, $_POST or $_SESSION declaration?

Is there a way to use multiple variables inside a $_GET, $_POST or $_SESSION declaration?
For example: $_SESSION['session_array{$i}'].
$i being a counter variable so that each array I save has a different name.
I need this for saving multiple associative arrays in the $_SESSION, if there is another way this could be done this would also be helpful.
You can (for $_POST, $_GET, $_SESSION, $_REQUEST, respectively) do
$_SESSION["session_array{$i}"]
But, you probably are looking for, or should rather do
$_SESSION['session_array'][$i]
Also, don't forget to use session_start() before trying to use session variables.
All of these support multidimensional arrays.
i.e:
$_SESSION['fruit']['apple']['green'];
$_GET['country_list']['US'];
or with variable:
$_GET['count'][$i];
You can store an array in $_SESSION (I wouldn't recommend doing it in the other ones, though):
$session_array = array();
$session_array[$i] = 'some value';
$_SESSION['session_array'] = $session_array;
http://www.phpriot.com/articles/intro-php-sessions/7 looks like a good intro for you.
Session:
For a Session, you can do:
$_SESSION['key']=array('one','two','three');
echo $_SESSION['key'][1] // echos 'two'
POST:
For a form submit with post you can add [] to the end of the input name to put it in an array
<form method="POST" action='/' >
<input name='arr[]' type="text" value="a">
<input name='arr[]' type="text" value="b">
<input name='arr[]' type="text" value="c">
</form>
To be accessed like:
echo $_POST['arr'][0] // echos 'a'
GET:
Same as with the form, you just add [] to the variable name and it can be accessed as an array.
if you visit www.yoursite.com/index.php?test[]=a&test[]=b
you can do:
echo $_GET['test'][1] // echos 'b'
Why not just make the array multidimensional:
$_SESSION['session_array'][$i]
Regarding the $_GET and $_POST superglobals: You don't want to store stuff into that manually but rather by a get or post request. So that's not really an issue IMHO.
Still you can have a multidimensional $_POST superglobal when using input form like:
<form method="post" action="">
<input type="text" name="name[]">
<input type="text" name="name[]">
</form>

Categories