Here is what I'm dealing with:
Let's say I have some php page:
<?php
// do php stuff
?>
And I have two HTML pages where the action sends them to this php page. On this php page, I want it to do one thing if it comes from one of the html pages, and something else if it comes from the other. I cannot pass any explicit variables to the php page, I need to check which page called the php.
I have tried:
$_SERVER['PHP_SELF'] when I access this, it doesn't seem the send the referring page.
$_SERVER['HTTP_REFERER'] this gives the previous page to the one im looking for.
For example:
HTML PAGE1 form calls php page --> I want some variable in PHP that contains HTML PAGE1.
HTML PAGE2 form calls php page --> I want some variable in PHP that contains HTML PAGE2.
Thanks for the help. Please let me know if there's some way I can clarify the question.
If the domain differs you can use:
$_SERVER['SERVER_NAME'];
But in all scenarios this would help:
$_SERVER['REQUEST_URI'];
Giving you the complete URL that was called.
As Think Different said a hidden field in your form is probably your best bet because you don't want to modify the url. In this case you would have the form submit over a POST request and retrive your data with $page = $_POST['YOUR_HIDDEN_VARIABLE_HERE'].
Related
I have an website and have multiple projects. All the project pages has contact us button and its redirected to contact page with form integrated. So i want to find from which URL they are coming and filling the form. I am using pure HTML, CSS website and PHP for form submission.
Thanks in advance.!!
Two options come to my mind:
Easiest and recommended: you redirect adding a query parameter (eg, refirect to: contact.php?from=page1 instead of just contact.php) then read the 'from' value via PHP $_GET
You can also use the http-referrer header from PHP (i.e. read $_SERVER['HTTP_REFERER']), but this may be unreliable
You could also get more creative: you could set a cookie via JS just before redirecting :) There are possibly many other solutions.
You can use get request to achieve this task.
While redirection:
www.xyz.com/contact.php?form=project1
www.xyz.com/contact.php?form=project2
In Contact.php
if($_GET[from] == "project1")
{
// he is coming from project1
// Do something
}
You can use switch also.
I have a site with a page in it that dynamically pulls a family profile from a database that contains a bunch of family data.
The generated url looks something like this:
http://example.com/family/?name1-name2/
I have a contact form on this page, and I want to include the current url in each contact submission so that I can tell which family's page the form is coming from.
I don't know much php at all, I'm wondering what php can be used to grab the url.
Use this to get current URL of your page:
echo $_SERVER['REQUEST_URI'];
In general the $_SERVER variables will get you what you need. Try adding the following on your page to see what's available:
var_dump($_SERVER)
in general, $_SERVER['REQUEST_URI'] will do the job. It'd output /family/?name1-name2/
I have a webpage with a form on it. The heading of the form is this:
<form name="sw" METHOD ="POST" ACTION="logger1.php">
After some event has happened, a javascript function submits the form like this:
document.forms["sw"].submit();
The php file logs the data from the form in a text file on the server, and then redirects the browser to another page. My issue is that I want to examine one of the values in the form from the previous page on the page that the browser is redirected to. I am completely lost. help!
Have you considered using sessions? The $_SESSION[] array might keep your previously posted variable between pages.
Basically all the form information is being passed to the "logger1.php" file in the POST method.
So you need to see what the code in the "logger1.php" file is and see exactly how the file is redirecting once it's done doing what it does.
Then you can possibly append the variable you want passed on to the redirect method in the GET method.
Lets say the variable you want to pass on is:
$_POST['Some_variable']
and the redirect method is something like:
header('Location: some_file.php');
then you can append it this way:
header('Location: some_file.php?variable_name='.$_POST['Some_variable']);
You can append ?info=hello to the end of the URL it redirects to, then retrieve it in PHP with $_GET['info']
You can't. The information is not passed when the browser is redirected so there is no way to access it.
I think the best way to do this would be to store the value in a database. If you are using ASP.NET the easiest way to do this would be to set up a SQL Server Express database. They are free until you go over 10GB.
Cant you put the variable that you want to look at as a get variable added to the url of the redirected page like so http://yourdomain/thepage.php?var=variable
I have a page. The user submits the page and sends it to a PHP results page. It works fine. Now, I want the results page to link to another page, and for the elements on that page to depend on what was on the results page. I know how to pass form variables to another page, but I don't know anything about passing non-form variables.
From my searching on the web, my best guess is that I should be passing by URL. Is this correct? If so, a possible problem: the page I want the results page to pass to will have a form, and the user will go to yet another results page by clicking submit (the form data will be sent by POST). Can I send the non-form data (the old results page variable) along with the form data, if the user is going to the other page using POST?
I strongly suggest using sessions. It's not that hard to learn, php makes it VERY easy using http://php.net/session_start and the $_SESSION variable.
Advantage is that you will not have to submit a form on every click, and no information will be displayed in plain text in the URL.
There are several options. However, the easiest may be to simply pass the data on using hidden input fields.
Other options would be using the session, storing to a database between forms, or some combination therein.
If you are going to use POST to go to the next page the most simple option is to include the data you want to send along using an input type="hidden" element in your form.
You might consider using a session to pass the data along.
You can embed the non-form data into the second form as hidden fields. To make sure that it is also passed in the link, you can just add it to the URL as a query string, like:
http://..../blah.php?var1=val1&var2=val2
as long as the data you're passing can fit into a URL. Be sure to urlencode() anything you're appending to the URL.
<?php
start_session();
$_SESSION['Foo'] = 'Bar' // Add stuff here.
?>
Is it possible to use a variable from one page in a piece of code on another? e.g. submit a form on one page and on the second page use a PHP script to add the data from the form to a MySQL table
Thanks for all the help
Your best bet would be to use PHP's session functions. That way if the user navigates away from your page and then comes back, the session variables will still be there (provided the session hasn't expired). You can get more information here -- PHP Sessions. Basically all you have to do is call
session_start();
at the top of each php page (before anything is outputted to the browser) where you want to have access to the session variables. You can then set/retrieve a variable using
// set
$_SESSION['varname'] = "something";
// retrieve
$somevar = $_SESSION['varname'];
This is what the GET and POST 'super' globals are for.
http://www.tizag.com/phpT/postget.php
The script that generates the form does not have to be the script the processes the form. all it takes is for the FORM tag to point to the correct script. The only caveat is that whatever page you submit the form data to also has to then generate some suitable output (unless you do a AJAX POST).
If you're instead asking if two page executions can handle the same POSTed data, well that is quite a different question. At a simplistic level, you can have the first include() the second so it has access to all the same data. A bit more advanced is to re-create the original POST and synthesize a POST. This will probably require some JavaScript assistance. Another approach is to use PHP's session feature and put the value aside so a later script has access to it. This relies on the user then following the correct link, however!
Use session to access your variable to the second/other page.
Example:
index.php
<?php
session_start();
$_SESSION['yourData'] = 'yourData';
?>
=========================================================================
second_page.php
<?php
echo $_SESSION['yourData'];
?>
You may try this one. Visit this link for more details. Or PHP's official website.