Convert code to POST - php

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.

Related

How to get the name of Telegram bot with PHP

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.

Oauth2 Data from Google after authorization

While learning about Google Oauth2, I saw two options for implementation
Option 1) Using PHP client library installed locally on server
example - http://phppot.com/php/php-google-oauth-login/
Option2) Using Javascript, AJAX, PHP & the google platform library (from CDN directly)
example - http://w3lessons.info/2015/05/19/google-oauth-2-0-ajax-login-using-jquery-php-mysql/
I have been able to implement both. However I have following questions:
Question 1)
In option 1 [$profile = $this->client->verifyIdToken()->getAttributes();] gives me profile data which contains only email[email] & google ID[sub].
Output: of $profile
Array
(
[envelope] => Array
(
[alg] => R256
[kid] => ca6f0ece055ffa823454e56da
)
[payload] => Array
(
[iss] => accounts.google.com
[at_hash] => e5l-tvQpWQ
[aud] => 8pps.googleusercontent.com
[sub] => 186524872
[email_verified] => 1
[azp] => 1go.apps.googleusercontent.com
[email] => sagl#gmail.com
[iat] => 14543
[exp] => 14543
)
)
In option 2 [var profile = googleUser.getBasicProfile();] gives me a profile data which contains the full name & the profile picture link on top of google ID & email
Is there any function which can get me the Profile name & picture link using option 1 (only PHP based implementation)
Question 2) For option 2 how can I modify the default "Google button" logo being sent by Google through the div:
<div class="g-signin2" data-longtitle="true" data-onsuccess="Google_signIn" data-theme="light" data-width="200"/>
Question 1: $profile['payload'] now has an additional key named picture and its value is a URL to the profile picture.
Question 2: There are a limited number of attributes you can use to customize your sign-in button: data-scope, width, height, data-longtitle, data-theme, data-onsuccess, and data-onfailure. data-scope is used to request permission scopes. data-longtitle takes a true or false(default) value to determine whether you want to display "sign in with google" or "sign in", respectively. data-theme takes values light or dark. And data-onsuccess and data-onfailure take js functions to be called on successful/failed sign in attempts.
Beyond that, you'll have to do some reading to really customize the button here: https://developers.google.com/identity/sign-in/web/build-button#customizing_the_automatically_rendered_sign-in_button_recommended

Add to A $_POST Array through Multiple Refreshes

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.

Insert many key=>value pairs in array

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

Transferring a (fairly) large amount of data across a single page load

I have an application that generates an array of statistics based on a greyhounds racing history. This array is then used to generate a table which is then output to the browser. I am currently working on creating a function that will generate an excel download based on these statistics. However, this excel download will only be available after the original processing has been completed. Let me explain.
The user clicks on a race name
The data for that race is then processed and displayed in a table.
Underneath the table is a link for an excel download.
However, this is where I get stuck. The excel download exists within another method within the same controller like so...
function view($race_id) {
//Process race data and place in $stats
//Output table & excel link
}
function view_excel($race_id) {
//Process race data <- I don't want it to have to process all over again!
//Output excel sheet
}
As you can see, the data has already been processed in the "view" method so it seems like a massive waste of resources having it processed again in the "view_excel" method.
Therefore, I need a method of transferring $stats over to the excel method when the link is clicked to prevent it having to be reproduced. The only methods I can think of are as follows.
Transferring $stats over to the excel method using a session flash
The variable may end up being too big for a session variable. Also, if for some reason the excel method is refreshed, the variable will be lost.
Transferring $stats over to the excel method using an ordinary session variable
As above, the variable may end up being too big for a session variable. This has the benefit that it wont be lost on a page refresh but I'm not sure how I would go about destroying old session variables, especially if the user it processing alot of races in a short period of time.
Storing $stats in a database and retrieving it in the excel method
This seems like the most viable method. However, it seems like a lot of effort to just transfer one variable across. Also, I would have to implement some sort of cron job to remove old database entries.
An example of $stats:
Array
(
[1] => Array
(
[fcalc7] =>
[avgcalc7] =>
[avgcalc3] => 86.15
[sumpos7] =>
[sumpos3] => 9
[sumfin7] =>
[sumfin3] => 8
[total_wins] => 0
[percent_wins] => 0
[total_processed] => 4
[total_races] => 5
)
[2] => Array
(
[fcalc7] => 28.58
[avgcalc7] => 16.41
[avgcalc3] => 28.70
[sumpos7] => 18
[sumpos3] => 5
[sumfin7] => 23
[sumfin3] => 7
[total_wins] => 0
[percent_wins] => 0
[total_processed] => 7
[total_races] => 46
)
[3] => Array
(
[fcalc7] => 28.47
[avgcalc7] => 16.42
[avgcalc3] => 28.78
[sumpos7] => 28
[sumpos3] => 11
[sumfin7] => 21
[sumfin3] => 10
[total_wins] => 0
[percent_wins] => 0
[total_processed] => 7
[total_races] => 63
)
)
Would be great to hear your ideas.
Dan
You could serialize the array into a file in sys_get_temp_dir() with a data-dependet file name. The only problem left is cleaning up old files.
Putting it into the database is also possible as you said, and deleting old data is easier than on the file system if you track the creation time.

Categories