I passed a parameter through an URL using javascript. Here's the code:
<script> window.onload = function() { // Creating a cookie after the document is ready var cookies = document.cookie.split(";") var cookiePair = cookies[0].split("="); var cookie_user=cookiePair[1]; // remove ending parenthesis here window.location.replace("http://192.168.206.1/foodblog/?page=http://192.168.206.1/test/ChangeInfo.php&username="+cookie_user); }; </script>
The page that received the parameter is called ChangeInfo This is what I see in the URL when I get to the ChangeInfo page: http://192.168.206.1/foodblog/?page=http://192.168.206.1/test/ChangeInfo.php&username=nitzan
When I'm trying to get the parameter username from the URL, I get this error: Notice: Undefined index: username in C:\xampp\htdocs\test\ChangeInfo.php on line 5
The way I'm trying to get this parameter is to use $_GET like that: $username = $_GET['username'];
Does anyone know why this makes me a problem? Thanks in advance
I just solve the problem
I deleted the Page parameter from the URL I created in javascript part. this is the updated Javascript part:
<script> window.onload = function() { // Creating a cookie after the document is ready var cookies = document.cookie.split(";") var cookiePair = cookies[0].split("="); var cookie_user=cookiePair[1]; // remove ending parenthesis here window.location.replace("http://192.168.206.1/test/ChangeInfo.php?username="+cookie_user); }; </script>
thank you:)
Ignoring the javascript part, needing to focus on PHP.
You are on this page: http://192.168.206.1/foodblog/?page=http://192.168.206.1/test/ChangeInfo.php&username=nitzan
And when you use $_GET['username'] you get the error, that it is not assigned. It seems that your $_GET is not working at all, probably Apache settings.
Also, it is safer to get GET parameters with isset first.
if(isset($_GET['username']) && $_GET['username']] {
$username = $_GET['username'];
}
else {
$username = '';
}
Then you can compare, if username is set or not in your php code:
if($username) {
//Do something
}
Final thought. Is your first parameter page= http://192.168.206.1/test/ChangeInfo.php working? Can you get it through $_GET?
The problem seems to be just in the way you set and get the url parameter though $_GET. If you use some framework, it might be disabled to use $_GET directly and for example in Symfony you need to use:
$request->get('username');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.