Pass a variable value from one php page to another - php

I'm trying to send the value of this php get_field variable from a wordpress Archive page.
get_field('supplier_email_address', 'product_brand_' . $term->term_id );
to another sendmessage.php page which is never accessed other than with the of JS to send a form. The variable I need filled is called:
$sendto="value";
How can I achieve this?
EDIT: Apparently this requires more information.

Use session variables.
First of all every page you want to use sessions on needs to have the function session_start(); declared at the top of every page. You then can use the session superglobal to store things.
Pg1.php:
<?php
session_start();
$_SESSION['variable_name'] = 'string';
?>
Pg2.php:
<?php
session_start();
echo $_SESSION['variable_name'];
?>
Pg2.php will show: some string

Either POST your value so your second page can retrieve it in the $_POST array, or include it in the URL path so your second page can $_GET it.
Without more info, we can't really help you any more than that.

Related

php $_POST variable is not saving

I currently have some php code on a form action, and it is being updated to the form's next page with
<?php echo $_POST['var here']; ?>
and it is working, but I noticed when Im trying to refresh the page it asks to confirm resubmission. When I resubmit it works in that tab in which it was sumbitted, but in another new tab it does not show the displayed php post variables. I even took it the next step by seeing that when I open the 2nd page after the form action has been submitted the php post variables are gone...
Help!
Thanks!
When you submit a form with <form method="post" /> it does a post request to the server, thus populating $_POST. When you open the link in a new tab it is no longer a post request but a get request. That is why you'll see nothing in $_POST.
$_POST — usually from forms
$_GET - from values on the URL (the query string myscript.php?myvar=Joe)
You can find plenty of resource about it. You can start here
If you want to keep the values you can save them to the session:
<?php
session_start(); // should be at the top of your php
if (isset($_POST['var'])) {
$_SESSION['var'] = $_POST['var'];
}
$myvar = isset($_SESSION['var']) ? $_SESSION['var'] : "no var";
echo $myvar;
Now the value is stored in the session so you can visit the page in a new tab and it will still be there.
This sounds like desired behavior. The $_POST variable should only be filled when the post action is created. If you're looking to store variables across pages you could store it in either the $_SESSION var in PHP or deal with the front end $_COOKIE business. If you're always going to be rendering pages from the backend then $_SESSION is the way to go. It's never too late to read up on cookies and sessions.
The skinny of it is that you're going to want to do something like this:
<?php
session_start();
if ($_POST['var']) {
$_SESSION['var'] = $_POST['var'];
}
echo $_SESSION['var'] ?: $defaultValue;
Then you'll notice that the message changes only when you post and won't exist before then.

Calling a variable from another page in php

I have such a loop:
for($i=0;$i<count($dersler);$i++)
{
echo $dersler[$i].', ';
}
I want to run this loop on another page by capturing $dersler variable from another page. How can I manage it?
Thank you.
You could pass the variable as a query parameter and access it again using the $_GET superglobal. An other option is posting the variable to the page using a form, and accessing it through $_POST.
But the solutions mentioned above require sending the value to the client side where it could be modified. If this is not desired, you could use a session to store a value serverside, between requests from the same client. Take a look at the session documentation: http://www.php.net/manual/en/book.session.php.
You can store $dersler in a session.
on the first page :
session_start();
$_SESSION['dersler'] = $dersler;
and on the other page :
session_start();
$desler = $_SESSION['dersler'];
for($i=0;$i<count($dersler);$i++){
echo $dersler[$i].', ';
}

PHP - $_SERVER['HTTP_REFERER'] - how to store once, not again when a form submission occurs

In PHP, how would I go about storing the variable "backurl"? (retrieved as shown below at the bottom - when the following constraints need to apply)
The variable "backurl" is storing the last php script viewed.
On first execute the variable "backurl" is stored and works correctly
When a form is submitted through $_GET or $_POST within that script, the variable will update as the previous URL has changed. (I don't want this to happen)
i.e.
FROM -> TO (actual outcome - desired outcome)
page1.php -> page2.php (link goes to page1.php - as intended)
page2.php -> page2.php?test=yes (link goes to page2.php - I want it to go back to page1.php)
If anyone has any suggestions of how to do this, thank you very much!
$backurl = $_SERVER['HTTP_REFERER'];
you could use sessions
<?php
session_start();
if(!isset($_SESSION['backurl'])){
$_SESSION['backurl'] = $_SERVER['HTTP_REFERER'];
}
?>
$_SESSION['backurl']; //contains the back url;
and if you nee to unset the backurl:
unset($_SESSION['backurl']);
Set the backurl in session and access it whenever & however you needed in page2.php
There are several ways to do this. One could be updating your backurl only if the script page is different, using session.
e.g. (untested):
session_start();
if( $_SEVER['SCRIPT_NAME'] != $_SESSION['lastpage'] ){
$_SESSION['backurl'] = $_SERVER['REQUEST_URI'];
}
This way, you'll only update the backurl when you call a different php page, leaving it unchanged when you go to the same page with different parameters (based on your example).
Adapt the code to your needs.

Pass URLs between php pages

I was wondering how I can pass a URL to another web page using PHP. I know if I were to use an HTML form I could have a hidden field with the value being the URL I want to get.
How can I pass the URL along to another page without using a form?
If the page is your own, you could use a session variable.
More details here: http://www.w3schools.com/php/php_sessions.asp
You can use $_SESSION, or a cookie.
PHP has a server global variable which includes a referrer variable.
$_SERVER['HTTP_REFERER']
http://php.net/manual/en/reserved.variables.server.php
You can echo this and it will output the page which the user came from.
You could pass it using a get query,which is in a way form data, so i won't go into it
OR a session variable. On the sender page
session_start();
$_SESSION['url']="http://site.com"
and on the receiver
session_start();
$url=$_SESSION['url'];
unset($_SESSION['url'])l
You can initiate sessions.
Put session_start(); at the top of every page (normally by including a header or function). The session_start(); needs to come before any HTML output.
Then on your referring page, you can save values into the session array:
$_SESSION['URL'] = "myurl.com";
On your second page, you can reference it just like any other variable:
echo $_SESSION['URL'];
Link
somepage.php would then receive the URL in the variable $_GET['url'].

Sending message without input elements

I want to send a feedback message from one page to another without using input elements via POST method. Is that possible? Or is there other way around?
What i want is applying a value from one page to another page's variable.
i.e.: Sending "Yaba Daba Doo" string from 1.php to 2.php's variable, named $info.
1.php:
<?php
$info = "Yaba Daba Doo";
?>
2.php
<?php
$variable=$_POST['info'];
echo "The message is:".variable;
?>
How can i pass info variable to 2.php using POST method, without input elements?
You can use session variable to store data on server and access across the pages of your application.
If you want a secure way to send this variable you can use session so that end user can not see it
An example
page1.php
session_start();
$_SESSION['mes']=$info;
page2.php
session_start();
echo $_SESSION['mes'];
And the another way is to pass variable in GET which I will not recommend user can see the value of your input variable
Use a session to store your data, then when ever you want to store is call upon the $_SESSION global.
Here is more info about sessions:
php.net
W3schools
Tutorial
you can use php curl to make a post request to 2.php from 1.php

Categories