0

I am trying to pass a variable from javascript to PHP using a an AJAX request [$.get] but am getting nothing through the $_GET method. I am wondering why the variable email won't pass even though there is a value within it.

fetch.js

email = resp.emails[i].value; 
$url = 'login.php'; //this file is on the same directory as fetch.js
//AJAX
$.get($url, {name: email}); 

login.php

$googleid = $_GET['name'];
echo $googleid; //nothing showing

If you need any more details please don't hesitate to ask.

EDIT I know I am going to get flamed for this but this was a typo. In my script I didn't make this mistake.

11
  • 4
    You send GET variable named name. So in php you can access it by $_GET['name'], not $_GET['email'] Commented Aug 19, 2014 at 15:25
  • 2
    This is embarrassing Commented Aug 19, 2014 at 15:33
  • Also, login.php should be in the same directory as your HTML file, not necessarily the JS file. Are you getting a 404 when calling $.get ? Check your dev console. Try $.get($url, {name: email}).fail(function() {alert("An error happened")}) Commented Aug 19, 2014 at 15:37
  • Yes for now they are all in the same directory. Commented Aug 19, 2014 at 15:42
  • Did you try the fail method I suggested? Also, where are you trying to use the value that AJAX gives you? Show that code, what you have shown doesn't even have a callback for $.get Commented Aug 19, 2014 at 15:44

4 Answers 4

2
 $googleid = $_GET['email']; 

should be

 $googleid = $_GET['name'];

Because your GET variable was name and email was value that assign to name (eg, name = [email protected]). So you should access it by $_GET['name']

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

1 Comment

Consider deleting this answer now that the OP updated their question, it was a typo
1

It should be

$_GET['name'];

What is the line email = resp.emails[i].value;? Has that a value?

Comments

1

Since in JS you send GET variable named name then in PHP you have to access it by $_GET['name'], not $_GET['email']:

$googleid = $_GET['name'];

Comments

0

You have to use $_POST for a parameter request.

For get the parameter would be visible in the url what you probably dont want like [email protected]

2 Comments

I'l have to change $.get(method) to something else. Will I?
no you have to use $_POST[] instead of $_GET[] in your PHP script.

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.