0

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

3
  • you should put the PHP code for solving your question , not javascript's . and the error is for that there is no index 5 in array username. Commented Jun 17, 2020 at 10:35
  • 1
    my php code in the ChangeInfo page is : $username = $_GET['username']; I wanted to get the value of username parameter from the URL i created on the javascript part Commented Jun 17, 2020 at 10:37
  • Does this answer your question? How to get the value from the GET parameters? Commented Jun 17, 2020 at 14:54

2 Answers 2

1

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 :)

Sign up to request clarification or add additional context in comments.

Comments

0

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');

2 Comments

I think you haven't understood OPs question fully. Their problem is that they don't receive the value of $_GET['username']
no the $_GET for page parameter is not working either . maybe the problem is the way I created the URL in the javascript part?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.