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
)
)
)
Related
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']
I'm trying to set a session array with some pre-defined values, which the user can then add to using a simple html form. My problem is that on the page where the array is set, any refresh or revisit of the page just duplicates the pre-defined values within the array. Not only that, but it also overwrites the value coming from the form each time at the end.
So in this basic example, I have a list of animals and a user can add another animal to the list. But this outputs the pre-defined animals again each time i.e. if I submit the form twice (e.g. adding chicken and then dog) I get the output:
Array ( [0] => pig[1] => cow[2] => sheep[3] => chicken[4] => pig[5] => cow[6] => sheep[7] => dog)
What I want is:
Array ( [0] => pig[1] => cow[2] => sheep[3] => chicken[4] => dog[5])
What am I doing wrong?
index.php
<?php
session_start();
//pre-defined list of animals
$_SESSION['animals'][] = 'pig';
$_SESSION['animals'][] = 'cow';
$_SESSION['animals'][] = 'sheep';
?>
<!--form to add another animal-->
<form action="go.php" method="POST">
<p><input type="text" name="entry1"></p>
<p><input type="submit" name="submit"></p>
</form>
go.php
<?php
session_start();
//add form entry1 to the session array
$_SESSION['animals'][] = $_POST['entry1'];
//print session array
print_r($_SESSION['animals']);
?>
Only initialize the session variable if it's not already set:
if (!isset($_SESSION['animals'])) {
$_SESSION['animals'] = array('pig', 'cow', 'sheep');
}
Check
in_array('YOUR_VALUE',$_SESSION['animals'])
before re inserting it to avoid duplication.
Reference: in_array
I would suggest to not insert data in the session directly, but adding hidden input values like:
<input type=hidden name=extraValue[] value="pig">
<input type=hidden name=extraValue[] value="cow">
etc
In your PHP page, unset the previous session since you want a 'fresh' dataset based on the input, not on the old values.
unset($_SESSION['animals']);
You can access your extra values in $_POST['extraValue']. You then could merge both arrays like
$postValues = array_merge($_POST['extraValue'], $_POST['entry1']);
I havent tested this code yet, but I would use this 'way', rather than setting SESSION values before input.
This is more of a technique question rather than maybe code. I am having a php form with many fields (items to select). Naturally some of the items might be selected and some not. How do I know which ones are selected when i post the data from page 1 to page 2? I thought of testing each one if empty or not, but there are just too many fields and it doesn't feel at all efficient to use or code.
Thanks,
UPDATE EDIT:
I've tried the following and maybe it will get me somewhere before I carry on testing the repliers solutions...
<html>
<body>
<form name="test" id="name" action="testprocess.php" method="POST">
<input type="text" name="choices[shirt]">
<input type="text" name="choices[pants]">
<input type="text" name="choices[tie]">
<input type="text" name="choices[socks]">
<input type="submit" value="submit data" />
</form>
</body>
</html>
and then second page:
<?php
$names = $_POST['choices'];
echo "Names are: <br>";
print_r($names);
?>
This gives out the following:
Names are: Array ( [shirt] => sdjalskdjlk [pants] => lkjlkjlk [tie]
=> jlk [socks] => lkjlkjl )
Now what I am going to try to do is iterate over the array, and since the values in my case are numbers, I will just check which of the fields are > 0 given the default is 0. I hope this works...if not then I will let you know :)
I think what you're looking for is this:
<form action="submit.php" method="POST">
<input type="checkbox" name="checkboxes[]" value="this" /> This
<input type="checkbox" name="checkboxes[]" value="might" /> might
<input type="checkbox" name="checkboxes[]" value="work" /> work
<input type="submit" />
</form>
And then in submit.php, you simply write:
<?php
foreach($_POST['checkboxes'] as $value) {
echo "{$value} was checked!";
}
?>
The square brackets in the name of the checkbox elements tell PHP to put all elements with this name into the same array, in this case $_POST['checkboxes'], though you could call the checkboxes anything you like, of course.
You should post your code so we would better understand what you want to do.
But from what I understood you are making a form with check boxes. If you want to see if the check boxes are selected, you can go like this:
if(!$_POST['checkbox1'] && !$_POST['checkbox2'] && !$_POST['checkbox3'])
This looks if all the three check boxes are empty.
Just an idea:
Create a hidden input field within your form with no value. Whenever any of the forms fields is filled/selected, you add the name attribute of that field in this hidden field (Field names are saved with a comma separator).
On doing a POST, you can read this variable and only those fields present in this have been selected/filled in the form.
Hope this helps.
Try this.....
<?php
function checkvalue($val) {
if($val != "") return true;
else return false;
}
if(isset($_POST['submit'])) {
$values = array_filter(($_POST), "checkvalue");
$set_values = array_keys($values);
}
?>
In this manner you can get all the values that has been set in an array..
I'm not exactly sure to understand your intention. I assume that you have multiple form fields you'd like to part into different Web pages (e.g. a typical survey form).
If this is the case use sessions to store the different data of your forms until the "final submit button" (e.g. on the last page) has been pressed.
How do I know which ones are selected when i post the data from page 1 to page 2?
is a different question from how to avoid a large POST to PHP.
Assuming this is a table of data...
Just update everything regardless (if you've got the primary / unique keys set correctly)
Use Ajax to update individual rows as they are changed at the front end
Use Javascript to set a flag within each row when the data in that row is modified
Or store a representation of the existing data for each row as a hidden field for the row, on submission e.g.
print "<form....><table>\n";
foreach ($row as $id=>$r) {
print "<tr><td><input type='hidden' name='prev[$id]' value='"
. md5(serialize($r)) . "'>...
}
...at the receiving end...
foreach ($_POST['prev'] as $id=>$prev) {
$sent_back=array( /* the field values in the row */ );
if (md5(serialize($sent_back)) != $prev) {
// data has changed
update_record($id, $sent_back);
}
}
How would I go about parsing incoming form data where the name changes based on section of site like:
<input type="radio" name="Motorola section" value="Ask a question">
where 'Motorola section may be that, or Verizon section, or Blackberry section, etc.
I do not have any control over changing the existing forms unfortunately, so must find a way to work with what is there.
Basically, I need to be able to grab both the name="" data as well as its coresponding value="" data to be able to populate the email that gets sent properly.
Well, you don't receive a HTML form, but just field names and values in $_POST. So you have to look what to make out of that.
Get the known and fixed fields from $_POST and unset() those you've got [to simplify]. Then iterate over the rest. If " section" is the only constant, then watch out for that:
foreach ($_POST as $key=>$value) {
if (stristr($key, "section")) {
$section = $value;
$section_name = $key;
}
}
If there are multiple sections (you didn't say), then build an section=>value array instead.
<form action="formpage.php" method="post">
<input type="radio" name="Motorola_section" value="Ask a question">
</form>
$motorola = $_POST['Motorola_section'];
if ($motorola =='Ask a question')
{
form submit code if motorola is selected
}
Well, first off, you shouldn't have spaces in the name field (even though it should work with them).
Assuming it's a form, you can get the value through the $_POST (for the POST method) and $_GET (for the GET method) variables.
<?php
if ( isset( $_POST['Motorola section'] ) ) // checks if it's set
{
$motoSec = $_POST['Motorola section']; // grab the variable
echo $motoSec; // display it
}
?>
You can also check the variables using print_r( $_GET ) or print_r( $_POST ).
Let's imagine I got this:
index.php generates form with unpredictable number of inputs with certain IDs/Names and different values that can be edited by user and saved by script.php
<form action="script.php" method="post">
<input id="1" name="1" type="text" value="1"/>
<input id="24" name="24" type="text" value="2233"/>
<input id="55" name="55" type="text" value="231321"/>
</form>
Script.php:
Here I need to get something like array of all inputs that were generated by index.php and save every value that corresponds to its id/name.
Is there a way to do this?
i may be missing something in your question, but the $_POST variable will contain all the name => value pairs you're asking for. for example, in your above HTML snippet:
print_r($_POST);
// contains:
array
(
[1] => 1
[24] => 2233
[55] => 231321
)
// example access:
foreach($_POST as $name => $value) {
print "Name: {$name} Value: {$value} <br />";
}
Use an array_keys on the $_POST variable in script.php to pull out the names you created and use those to get the values.
$keys = array_keys( $_POST );
foreach( $keys as $key ) {
echo "Name=" . $key . " Value=" . $_POST[$key];
}
It sounds like you're using a class or framework to generate your forms, you need to read the documentation for the framework to see if/where it's collecting this data.