How can i use variable on the previous page - php

I have a form for search user on like index.php?topic=pagename
I assign to variable what i wrote search form. So like ;
$name= $_POST['username'];
Everything okay, i can run this query on same page.
But i want to add new query under the previous query's result.
So like ;
$name have 5 euro.
Add money : NEW FORM
I can add form for add new money to under the previous query echo's. It seems. And it runs but i cant use previous variable $name .
Btw, add money form run on same page

What about just storing that value in a hidden input in the new form?
// new form
<input type="hidden" name="name" value="<?= $name ?>"/>

You can try $_SESSION:
http://php.net/manual/en/features.sessions.php
or $_COOKIE:
http://php.net/manual/en/features.cookies.php

Use sessions for this.
Store data in session, then use it in previous page.
http://www.w3schools.com/php/php_sessions.asp

Related

No $_GET = empty($_GET)?

I have a paginated table of names and ages that I want to filter by the "name" field.
For that I have an input called "name".
<input type="text" name="name" value="<?php echo $_SESSION['name']; ?>">
While switching between pages, in order to not carry around the filter in the url in the form of:
table.php?name=John&page=3
i am saving the $_GET['name'] to a $_SESSION['name'] and then apply that session to the SQL query string.
$_GET['name'] ? $_SESSION['name']=$_GET['name'] : null;
I would like to be able to clear the filter by having the user deleting the content from the input and pressing enter.
I have tried:
if($_GET['name']=="")
unset($_SESSION['name']);
The problem is that when I switch page and use the link:
table.php?page=7
it resets $_SESSION['name'] and takes me back to the selected page of the unfiltered table.
Any ideas on how I can work around this issue?
Many thanks in advance.
I think your problem is in your ternary operation. At first dont use this operation if you don't understand his logic. Ternary operation can reduce the amount of code, but in the same time can confuse you very much.
So:
$_SESSION['name'] = $_GET['name'] ? $_GET['name'] : null;
I managed to work around the issue by repacing
if($_GET['name']=="")
unset($_SESSION['name']);
with
if(isset($_GET['name']) and $_GET['name']=="")
unset($_SESSION['name']);
the isset() made all the difference.
Now it will properly check if an empty value is being passed in the $_GET['name'] and proceed with the IF accordingly.
Many thanks all of you for all your assistance.

Saving page ID within $_SESSION

I am new to sessions, and think I get the basics of them, they seem to act like containers holding information which you can use on at a later stage and are linked to your UID.
I am wondering how I would save the ID of a page (example ID123) on the click of a button, and what exact code I'd have in the header.
Much appreciated in advance!
EDIT:
This is on wordpress, sorry to add this, each page has an 'event ID' I want this to be stored for use later (at a kind of checkout page) sorry for not adding this!
Saving page ID within $_SESSION
session_start();
$_SESSION['pageId'] = (int) $_GET['ID'];
Now you can use $_SESSION['pageId'] to get its value where you need.
Make sure to put session_start(); at top of your script where you use session-related functions or $_SESSION array.
To unset it when you don't need it anymore, you would do:
unset($_SESSION['pageId']);
session_destroy();
session_regenerate_id();
update
On button click you would do something like this:
var btn = document.getElementById('btnId');
btn.onClick = function() {
window.location = 'saveSession.php?id=xxx' // replace xxx with your id value
};
Now in saveSession.php you wll have to use code like shown above for storing it in session.
You can also use Ajax though.
In wordpress you cannot pass custom url parameters. So you cannot send something like
http://yourwordpresswebsite.com/?custom_param='value'
WOrdpress doesnt allow it. SO if all you want is a Post ID or a Page ID. It is easy to grab in wordpress. Do:
$val= $post->ID
Thats it and $val is set. The way I do if I have to send any custom parameters to a different file is by sending it as a form variable. There might be better ways but I am new to Wordpress and php too. So this is what I do:
<?php
echo "<form action='php_file.php' method='post' name='form_name'>
<input type='hidden' name='eventid' value='$event_id' />
<input type='submit' name='submit' value='submit'/>
</form>";
?>
If you want this to be your session info you can just add it in your php file where you are collecting the above fields.
session_start();
$_SESSION['pageId'] = (int) $_GET['eventid'];
/* Do not forget to check if the get variable is clean before you perform any operations, use mysql_real_escape_string(). It is a very important security measure. */
This might not be the best approach as I told you I am new to wordpress too.

PHP: How to POST value of an input which contains [] in the name attribute?

I swear i couldn't find a simple working solution for this.
On a form i have inputs that have names containing "[]" and i cant change the names of the inputs because they are part of a script.
I want to php POST the values of those inputs at the next page, after the form submit.
Example of input
<input type="text" name="CustomFields[13]" id="CustomFields_13_1" value="">
Anyone knows how to accomplish it?
I want to do it using PHP only
If the name is CustomFields[13], then you can access it at $_POST['CustomFields']['13'].
You "cannot" POST something with PHP. It's always the client that POSTs to the server. PHP is running on server side.
I recommend that you use sessions and save there the values that you need to have available in next pages.
This is how you set a session:
session_start();
$_SESSION['CustomField'] = "test";
And this is how you get it:
session_start();
echo $_SESSION['CustomField']; //Should display "test"

Storing redirect URL for later use

I'm trying to store the redirect URL for use a few pages later but I'm having trouble figuring out how to get it from one place to another.
Usually I'd just pass a variable thru the URL, but since my redirect URL contains URL variables itself, this doesn't exactly work.
To give you a better idea of what I'm trying to do, here's the structure.
PAGE 1: User can click a link to add content on PAGE 2
PAGE 2: User enters text. Submitting the form on this page calls "formsubmit.php" where the MySQL data entries are handled. At the end of this I need to redirect the user to PAGE 1 again. The redirect URL needs to exactly match what was originally on PAGE 1
Does anyone have any suggestions on how to go about this?
You should use $_SESSION to store the variable in session memory. As far as specifics go with how to handle this in particular, you should be able to figure it out (store the variable, check if it exists later, if so redirect etc etc) but $_SESSION is going to be much more efficient / less messy than trying to pass things back and forth in query strings.
To declare a session variable you would do something like this:
$_SESSION['redirUrl'] = "http://www.lolthisisaurl.com/lolagain";
And then to reference it you just do
$theUrl = $_SESSION['redirUrl'];
Here is some material to get you started: http://php.net/manual/en/reserved.variables.session.php
I would recommend either using session variables, or storing the redirect url in a hidden form parameter. Session variables are pretty simple; just initialize the session (once, at the top of each page), and then assign variables to the $_SESSION global var:
<?php
session_start();
...
$_SESSION['redirect_url'] = whatever.com;
...
Hidden form parameters work by sending the data from page to page as form data. On the backend, you would add code that would put the URL to be stored in a form variable:
<input type='hidden' name='redirect_url' value='<?php echo $redirect_url; ?>';
On each page, you can take the URL out of the $_POST or $_GET variable (whichever is appropriate) and insert it into a hidden form in the next page.
You can use urlencode and urldecode to pass a string that contains elements that would otherwise break a url in a url query.
I can see two possible solutions :
get the previous page from document.referrer ([edit] find more info on this SO thread : getting last page URL from history object - cross browser?)
store the previous url via a session variable ([edit] MoarCodePlz pointed this out in his answer)
Regards,
Max
You can add this hidden field in to your form:
<input type="hidden" name="referer" value="<?php echo $_SERVER['HTTP_REFERER']; ?>">
Then use header() to redirect to this page:
header('Location: '. $_POST['referer']);

Variable disappears when I log in

I have profile page where the profile is retrieved via GET. The index file has this:
$profile = $_GET['profile'];
When I log in on the profile page, the $profile variable disappears. Here is the form action on the login function:
<form name="login-form" id="login-form" method="post" action="./index.php">
(The $profile variable is separate of the login username.)
How could I make the page retain the $profile variable?
Thanks in advance,
John
You should use a session.
Set the variable in the first page and retrieve it in any other page.
GET and POST variables aren't preserved throughout. Only from one page to another. Unless, you specifically GET or POST the variables from page to page.
Set it in a session $_SESSION['profile'] = $_GET['profile']; and retrive it $profile = $_SESSION['profile'];
Here's a simple tutorial Session in Action
If you need to retain query string data through a form, use a hidden field:
<input type="hidden" name="profile" value="<?=$_REQUEST['profile'];">
The method="post" part of a form does not send variables the same way as a GET. Instead, it posts them in the background, invisible to the user.
If you wanted to keep the variable - you could do one of two things:
Change the form to method="get".
Change the action of the form to action="./index.php?profile=<?php=$profile?>"
The variables passed from this form is accessible only in the "index.php" page. You will be able to access it as $_POST['profile'] or $_REQUEST['profile'].
If you want to access this in all your pages you have to use session.
You need to assign the value to $_SESSION['profile'] = $_POST['profile']. After this you will be able to use $_SESSION['profile'] in all pages. Before assigning value to $_SESSION['profile'] you need to add session_start(); to the beginning of the code.
You can use unset($_SESSION['profile']); to remove value from the session variable

Categories