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.

  • 4
    You send GET variable named `name`. So in php you can access it by `$_GET['name']`, not `$_GET['email']` – hindmost Aug 19 '14 at 15:25
  • 2
  • 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")}) ` – Ruan Mendes Aug 19 '14 at 15:37
  • Yes for now they are all in the same directory. – Apparently Im user2212428 Aug 19 '14 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` – Ruan Mendes Aug 19 '14 at 15:44
  • @JuanMendes My colleague just mentioned I needed a callback. I didn't know though it can make a difference. According to [this question](http://stackoverflow.com/questions/419240/how-to-get-javascript-function-data-into-a-php-variable) where I took my code from you do not need one. But his solution doesn't work for mine.. – Apparently Im user2212428 Aug 19 '14 at 15:50
  • @Apparently Im user2212428 What `var_dump($_GET);` tells? – hindmost Aug 19 '14 at 15:56
  • @hindmost array(0) { } – Apparently Im user2212428 Aug 19 '14 at 16:06
  • Apparently the OP didn't provided all relevant information about his issue. So I think this question should be closed as a sort of "cannot be reproduced". – hindmost Aug 19 '14 at 16:15
  • @ApparentlyImuser2212428 Once again, did you add the callback I mentioned in my comment to check that it's even getting to your PHP file? – Ruan Mendes Aug 19 '14 at 17:36
  • I changed $.get with $.ajax, swapped GET with POST and added a success function and it worked. However this was a completely different solution and I am still confused why this one doesn't work. I guess this question needs to be closed. I apologize. – Apparently Im user2212428 Aug 19 '14 at 18:10

4 Answers4

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 = test@example.com). So you should access it by $_GET['name']

MH2K9
  • 11,951
  • 7
  • 32
  • 49
1

It should be

$_GET['name'];

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

user32342534
  • 145
  • 6
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'];
hindmost
  • 7,125
  • 3
  • 27
  • 39
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 login.php?email=my_email@domain.tld

Steini
  • 2,753
  • 15
  • 24