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

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.

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.

send the variable to another page in php [duplicate]

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'];
?>

Convert Query String to Array [duplicate]

This question already has answers here:
Parse query string into an array
(12 answers)
Closed 9 years ago.
How can I convert this to an array in PHP?
&height=0&weight=2&width=10
I'm passing a data from a jquery function using .serialize() to a PHP function.
Any ideas?
Can be done within one line. :)
parse_str('&height=0&weight=2&width=10', $array);
print_r($array);
Depending on what type of request you are performing, it may already be in an array. Have a look at the PHP documentation on $_GET and $_POST global variables.
To view the contents of said array. You can use the function print_r() which will show you the contents of the array.
print_r($_GET)
print_r($_POST)
Access individual items in the array by the item's key. For example:
echo $_POST['height'];

PHP/HTML syntax error [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”
first time asking questions here. I am trying to create a small, primitive forum. I am basing it on this: while making changes to it where it is needed.
So far, I have setup the database and all that. Now I am trying to fix the view_topic.php and it is returning "Notice: Undefined index: id" on line 30 something.
This is line 30: $id=$_GET['id'];
You should make sure id is a valid index before accessing it.
$id = isset($_GET['id']) ? $_GET['id'] : null;
Notice: Undefined index:
This means that a piece of code attempts to access an element of an array that does not exist.
So, for example:
$myArray = Array(); // an empty array
echo $myArray['id']; // print 'id' element
// ^^ Oops! No such element yet!
The $_GET array contains the arguments from the querystring, which is the ?id=1 part of your request URL.
You will need to find out why this does not contain the element with key 'id' when id was provided in the URL, and then make your code decide what to do in the case that this value is missing, using a function such as isset or array_key_exists and an if statement.
Most likely you will present a more useful error message and terminate the script, if your code cannot continue without a valid id value.
Are you sure id= exists in the URL? Try adding the following near the top of your code.
var_dump($_GET);
var_dump dumps information about a variable to your screen. In this case, it will display all the GET data sent from your form. This is a debugging technique so you can see if your code is receiving what you expect. If the form uses method="post" and id is a field in the form, then you will need to use $_POST['id'] or $_REQUEST['id'] to access it.

Is there other ways of passing a php $_GET variable from url to a js variable? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get query string values in JavaScript
I know you could do this by echoing the variable $_GET['whatevervariable'] to a js variable.i was just wondering if there are other methods that can also do this?
This will provide the entire $_GET array to your JavaScript app:
<script type="text/javascript"><!--
_GET = <?php echo json_encode($_GET); ?>;
--></script>
The $_GET superglobal is just an array of the querystring.
You can fetch the querystring in javascript with window.location.search, but to use it like $_GET will need some sort of parsing and usually a few regular expressions to handle difficult characters etc.
Just try var qs = window.location.search; and then figure out exactly what you need, and how you will get it.
The easy solution if doing it inline is just echoing $_GET into a variable, like in danorton's answer.

Categories