send the variable to another page in php [duplicate] - php

This question already has answers here:
How to pass variables received in GET string through a php header redirect?
(8 answers)
Closed 7 years ago.
I have two pages in php ,one of them is exam page(exam.php) and another is result page(result.php), the result is calculated in exam page and must be sent to result page to display.(I don't have a form)
to send the result, Inside exam.php ,I write, header("location:result.php?result");
and to get the result inside result.php ,I write, $newresult=$_GET['result'];
but I receive error,and result didn't sent to the result page.
would you please guide me?

Using a URL to pass parameters can be done like so.
HTML
<a href='yourPage.php?name=Script47'>Send Variable</a>
PHP
<?php
if (isset($_GET['name') && !empty(trim($_GET['name'])) {
$name = htmlspecialchars(trim($_POST['name']), ENT_QUOTES);
}
?>
Explanation
The HTML is fairly simple, we create a link which holds a parameter specified after the page extension (?name=[...]).
The PHP first checks if the name parameter which was passed isset to prevent an undefined index error, and we check if it isn't empty. The trim function removes white spaces so an string with a space isn't outputted (" "). When we know that the string has a value in it we sanitize it (never trust user input) and then we output it.
Reading Material
htmlspecialchars();
trim();
empty();
isset();

Try use session
exam.php
<?php
session_start();
$_SESSION['result'] = $result;
?>
result.php
<?php
session_start();
echo $_SESSION['result'];
?>

Related

What does .php?action.. do? [duplicate]

This question already has answers here:
what does a question mark mean before a php form action
(4 answers)
Closed 2 years ago.
Can someone explain to me what is the use of ?action=add&code= and what they do in the code below? I have tried to search it on Google but they gave me HTML action atribute instead.
<form method="post" action="index.php?action=add&code=<?php echo $product_array[$key]["code"]; ?>">
Sorry for the noob question.Thanks for the reply.
Those are called query string values or parameters, they are one of several potential parts of a URL. Each key/value pair provides information that the server-side code can use when constructing the response to send back to the client. (Or the server-side code could even simply ignore them, they have no harmful effect.)
For example, given this key/value pair on the query string:
action=add
In the server-side code you can get the value "add" by fetching it from the query string by its key:
$action = $_GET["action"];
// $action now contains the string "add"
Presumably the logic in the code would then do something based on that value.
action is the name of a "normal" GET variable $_GET['action'].
You must look in the further code to see where it appears and what it is used for.
There is no standard for that
In the url after ? we can pass the values onto another webpage which can be used further.

How to check if ANY php parameter exists in url [duplicate]

This question already has answers here:
PHP check if url parameter exists
(6 answers)
PHP to check if a URL contains a query string
(4 answers)
Closed 5 years ago.
I am making a forum that accesses threads based off the category in the URL using the GET method. I want to redirect to an error page if no parameters exist in the url, but I want this to be a generic piece of code that can be used around my whole site.
For example:
The url would normally contain the category id:
localhost/myforum/threads.php?categoryid=1
I want it so that when the url is:
localhost/myforum/threads.php
it is to redirect to an error page, and that this piece of code is usable all around the website
The most reliable way is to check if the URL contains a question mark:
if (false !== strpos($_SERVER['REQUEST_URI'], '?')) {
// There is a query string (including cases when it's empty)
}
Try:
$gets = parse_url($url));
if($gets['query'] == "")
{
echo "No GET variables";
}
Just:
if (empty(array_diff($_GET, ['']))) {
header("Location: /path/to/error.php");
}
EDIT: Updated to remove empty values
You can use is_set to check if the parameter exists like this,
isset($_GET)

How to obtain part of the url with php [duplicate]

This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 6 years ago.
I am coming up with a way to display user profiles. Right now I am able to insert the user_id into the url when going to a user's profile, but I am unsure of how to get the user_id from the url.
So at the end of the url I have this:
profile?user=41
I just want to be able to get the 41 (please note this number will change per profile) and then set it to a variable.
How would I do this?
<?php
echo $_GET['user'];
?>
all the parameters of the url are stored in the $_GET variable...
The parameter that are in your URL can be retrieved with the
predefined variable:
<?php
//$userId = $_GET["pramName"];
$userId = $_GET["user"];
// or
// using the $_REQUEST variable
?>

How to make URL change content of a website? [duplicate]

This question already has answers here:
Correct syntax for hyperlink URL with PHP $_GET
(3 answers)
Closed 7 years ago.
Hope you'll be able to help me, as I searched all around Google with various keywords and couldn't find anything relevant to me, or I just don't know what this thing I'm curios about is called.
I have a website, which I want to be personalized, according to the URL string.
So I'd link to send my friend a link like http://example.com/index.php?name=Joanna and it would change the beggining of my page to "Hi Joanna!". I just want it to be simple as that and integrate it into PHP or HTML (which is easier), but don't know how to achieve that.
Any help would be appreciated :)
P.S. - Keep the "How you don't know that?" remarks away , I'm new at this and am still learning.
See HTTP GET Variables
If your URL is
www.example.com/index.php?name=Joanna
it means that $_GET["name"] is "Joanna".
So in PHP, you can output:
<?php echo "Hi ".$_GET["name"]."!"; ?>
and it will output, in this case:
Hi Joanna!
You can also check that the variable exists and isn't empty by doing one of the following:
isset($_GET["name"]) //will return true even if $_GET==""
!empty($_GET["name"])
$_GET["name"]!=""
So you can change your PHP output to:
if(!empty($_GET["name"])){
$name = $_GET["name"];
} else {
$name = "guest";
}
echo "Hi, ".$name."!";
which will output "guest" if $_GET["name"] is empty.
Shorter version of the above:
echo "Hi, ".(empty($_GET["name"]) ? "guest" : $_GET["name"])."!";

I keep getting "undefined" when using the function $_GET in PHP, why is this? [duplicate]

This question already has answers here:
PHP $_GET and $_POST undefined problem
(5 answers)
Closed 2 years ago.
I continue to get undefined printed out when I use print($_GET['user_username']); from the previous page. The URL of the page is page.php?user_username=Pete. Why is this happening?
$_GET manual says
An associative array of variables
passed to the current script via the
URL parameters.
First be sure that element exists
<?php
echo !isset($_GET["user_username"]) ? "undefined" : $_GET["user_username"];
?>
Or try var_dump against $_GET array to see if element with user_username key exists.
var_dump($_GET);
Is your request like this one?
http://www.mydomain.com/something.php?user_username=something
Try this code:
print_r($_GET);
You will get all the elements passed using get in array format. Then you can check it.. It also helps better in debugging many times.

Categories