I need to be able to store the $_POST data from a form (I'm doing that with the SESSION variable) and I need to add data to the $_POST array without overwriting the previous data.
Here's my original thread about the SESSION variables: Store Array Variables Through Page Refresh with PHP
Here's an example of what I need to do:
There's a form on a page that you can put the width and height of a wall to calculate out how much paint/labor would be needed to paint the walls. Once the first wall dimensions are entered, the user can click the submit button labeled "Add Wall" to add another wall to their order.
After the first wall input, the POST data would return something like this:
Array ( [wallW] => Array ( [0] => 9 ) [wallL] => Array ( [0] => 10 ) )
After the second wall is entered it would look something like this:
Array ( [wallW] => Array ( [0] => 9 [1] => 20 ) [wallL] => Array ( [0] => 10 [1] => 15 ) )
This would continue on until the user has entered all of their walls.
I've tried doing array_merge, array_append, etc, but none of those seem to be working; it keeps overwriting what's tin the POST data, even if I bring the POST data into another array.
I know the easy thing would be to throw some jQuery in there, but this is for a PHP class (and we're not allowed to use SQL yet) so I'd like to try to figure this out with just PHP.
Any help would be awesome.
Thanks,
Chelsea
I don't know why this has been downvoted, i find it rude as you are obviously new to php. But you just need to re-post your post vars in subsequent requests. So you would need to serialize you existing post objects as perhaps hidden fields. What you are missing is that php / http is stateless. So nothing is preserved between requests, another easy solution may be to move these POST variables to SESSION variables.
I see you've added the POST data to the session, but there is no reason to add it back to post, just keep appending to session and then use it from there. adding things to _POST wont force them to be posted next time if thats you are trying todo, see the part about serializing them to hidden fields.
Good luck, welcome to programming for the web.
The way you are constructing array seems little odd to me. Instead I would do something like.
if(!empty($_POST))
{
$wall = array('w' => $_POST['w'], 'h' => $_POST['h']);
$_SESSION['walls'][] = $wall;
}
Please give this a try.
Related
I'm trying to use this php library along with Sheetsu to pull single bits of data from a Google spreadsheet for output on a web page. My php skills are minimal and spotty, I'm afraid, and so I'm missing a final crucial component.
When I set up a test file and run my query, the code dumps everything into a $collection object. If I output print_r($collection); I get this:
Sheetsu\Collection Object (
[models:Sheetsu\Collection:private] => Array (
[0] => Sheetsu\Model Object (
[id] => 2.05.1
[title] => The Mead of Poetry
[answer] => Kvasir was created from the spit of the Aesir and Vanir. He was very wise.
)
)
)
My data's there, everything's working as expected, but I've never seen a structure like that before.
How can I just echo, say, just the [answer] value? I'm not sure what syntax to use to drill into that.
Thanks!
try
$collection->get(0)
or
$collection->getFirst()
or
$collection->getAll()[0]
etc.
models is a private property, you can't access it directly
so to access answer it would be something like
$collection->get(0)->answer
Try echo $collection[0]->answer;
I'm trying to make a custom management system for Telegram bots so that I can be able to send and read updates easily.. But my main problem is that I don't know how to retrieve data from an array. let me explain this in depth so you can understand what I'm talking about.
Basically when I try the getme command, I get this:
{"ok":true,"result":{"id":275700102,"first_name":"photoshop_post_production","username":"post_pro_bot"}}
So I converted this into an array using Php and it goes like this:
$_SESSION['website'] = "https://api.telegram.org/bot";
$_SESSION['url'] = "https://api.telegram.org/bot".$_SESSION['token'];
$_SESSION['me'] = file_get_contents($_SESSION['url']."/getme");
$_SESSION['meArray'] = json_decode($_SESSION['me'], TRUE);
So as you can see I have an array called meArray and if I print_r it ,this will be shown:
Array ( [ok] => 1 [result] => Array ( [id] => 275700102 [first_name] => photoshop_post_production [username] => post_pro_bot ) )
So basically I don't know how to take out the username or other information that I want from this array. I have tried several things but no success till now.
So, should be:
$_SESSION['meArray']['result']['username']
Try this.
I'm working on a script that will give a Magento user directions after selecting warehouse pickup (a plugin option). I already have the rest built. I'm simply missing one variable I need to call on success.phtml (the warehouse ID). The variable is tied to orders via stock_id.
This produces an array: I'm using $order successfully to pull the rest of the info I need for the script.
$order = Mage::getModel('sales/order')->loadByIncrementId($this->getOrderId());
$items = $order->getItemsCollection();
A shortened version of this array: can be printed with print_r($items->getData());
Array
(
[0] => Array
(
[item_id] => 223
[stock_id] => 15
[base_discount_refunded] =>
)
)
When I try to pull the data that I want out:
echo $items[0]['stock_id']; //the page breaks here and stops the page abruptly...
the page breaks and any logic that should take place after is ignored. What would cause this? I tried braking variables I'm calling in other similar arrays. None of my tests have replicated the page breaking. Why is this specific one breaking the page instead of returning 15?
You might try enumerating $items using var_export in your page, instead of print_r so you can see them as they truly exist:
foreach ($items as $item)
{
var_export($item->debug());
}
This will provide you with the results. Items is an object populated with more objects, not an array and should be treated as such. Try using
$itemId = $item->getStockId();
or
$itemId = $item->getData('stock_id');
Both accomplish the same thing.
FYI.. the debug() function shows relevant info and its built into magento for use with all mage objects.
EDIT: Try this:
echo $items[0]->getData('stock_id');
Well, $items is not an array, it is an object of Mage_Sales_Model_Resource_Order_Item_Collection. This should work:
$data = $items->getData();
echo $data[0]['stock_id'];
But using the enumeration interface of the collection, like already mentioned, would be much cleaner. Take a look at http://alanstorm.com/magento_collections.
You should also check your PHP configuration, to get more information on such errors. Take a look at http://alanstorm.com/magento_exception_handling_developer_mode.
I have several identical forms on a page (it's for rating stuff). The form looks like this:
<form id="contact" name="contact" action="#" method="post">
<input id="titel" name="titel">
<textarea id="msg" name="comment" class="txtarea"></textarea>
<button id="send">Send</button>
</form>
I'm sending the form data via ajax to a page where I want to store all the key=>value pairs in an array.
This is what I have on my php page where the form data is sent:
$arr = $_POST;
$array = array();
$array['titel'] = $arr['titel'];
$array['comment'] = $arr['comment'];
print_r($array);
When I send the first form prin_r gives:
Array ( [titel] => test [comment] => test2 )
Which is exactly what I want. But when I send the next form the values ar not inserted in the array, like:
Array ( [titel] => test [comment] => test2
[titel] => test3 [comment] => test4
)
In fact nothing happens. The values remain the same.
Any help much appreciated!
Mainly wrapping up the comments here.
1. Forms and request isolation
You said you submit several forms. Each time you submit form data to a PHP script, the script is executed line by line and when done, the whole process is terminated. When submitting another form, this will be another script execution which has none of the variables available you defined earlier.
This is how HTTP works. Each request is an isolated execution.
One technique to "remember" data bound to a specific client is to create a session.
A session consists of an id, which is stored on the client side, by default automatically into a cookie, and the session storage, which is saved on the server and can be reused over several isolated requests.
Learn more here
http://php.net/manual/en/features.sessions.php
2. Arrays in PHP
Array keys have to be unique.
Imagine - how PHP should know, which value you want to access if that would not be a unique constraint.
You said "array like several rows of a database result"
This looks like this
$result = array
(
0 => array ( 'col1' => 'colval1' ),
1 => array ( 'col1' => 'vol1val' )
)
You can do the same
$survey = array (
'step1' => array(...)
);
Access like this
$survey['step1']['..']
To save this into a session
session_start();
$_SESSION['step1'] = $survey['step1'];
To access this in a later request
print_r( $_SESSION['step1'] );
Hope this helps :)
Merry Christmas
i have a script (php+html) full of forms and inputs, after the form is submited via post i can give the user a code (like was a GET form) q=test&t=3&u=9&q2=test&t2=3&u2=9&q3=test&t3=3&3u=9, then the user can put this code in a textarea for editing what he previously submitted
how to transform the code inputed in the textarea (q&t&u) to work like if it was submitted like a normal POST? like if i where using $_POST['q'], $_POST['u'] but from that code submitted (like foreach)?
Look into parse_str
$str = 'q=test&t=3&u=9&q2=test&t2=3&u2=9&q3=test&t3=3&3u=9';
parse_str($str, $data);
print_r($data);
/*
Array
(
[q] => test
[t] => 3
[u] => 9
[q2] => test
[t2] => 3
[u2] => 9
[q3] => test
[t3] => 3
[3u] => 9
)
*/
What I understand from your question is. that you want save form and later want to allow ur visitor to update these details. Best way to do is.
you can generate a code(password) keep it in database along with record and provide it to your visitor
visitor will enter that code(unique) to get his record and can update that.