Solved

Jsonp retreive data problem!

Posted on 2015-01-24
6
164 Views
Last Modified: 2015-01-27
Hello,
I have set everything to sent asynchronous request to php file where it retrieve a json encoded details and things works fine but i get this error when i edit this


      This works fine !!
      
      $arr = array ('item1'=>"1",'item2'=>"alo",'item3'=>"We love jQuery4u");

      but when i tried to assign a variable
      for example

//details from database
$user = $row['details'];
$arr = array ('item1'=>"1",'item2'=>$user,'item3'=>"We love jQuery4u");

i got in the jquery response null related to item2!

so i figure out when assigning to the array a variable instead of constant it give me error so how can this be fixed
0
Comment
Question by:jaylab2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
6 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40568718
Please post the complete text of the JSON string that is returned from the PHP service.  Once we see the data it should be easy to show you how to process it.  Thanks.
0
 
LVL 43

Expert Comment

by:Rob
ID: 40568883
Your error is that your php $user variable hasn't been set.  It's sent that way to the browser (JavaScript)

Will need to see more of your php code, specifically the score between where you sty the user variable and the point you send the data back to the browser
0
 

Author Comment

by:jaylab2
ID: 40568929
Here it is

function login($user,$password){

	$u = $user;

	$first = "SELECT * FROM customer_entity where email='".$user."'";
	
	$sql = mysql_query($first);

	//$row = mysql_fetch_array($sql);
	
	$check1 = mysql_num_rows($sql);
	
	//get the customer id
	if ($check1 > 0 ){
	
		
		$code = mysql_fetch_array($sql);
		
		$userid = $code['entity_id'];
		
		//GET password hash
		$second = "SELECT * FROM customer_entity_varchar where entity_id='".$userid."' ORDER BY value_id ASC LIMIT 2,4";
		
		$sq = mysql_query($second);
		
		$test = mysql_fetch_array($sq);
		
		$pswhash = $test['value'];
		
		//now split 
		$hash = explode(":",$pswhash);
		
		$salt= $hash[1];
		
		$pass = md5($salt.$password);
		
		//return $pass."<br>".$hash[0];
		
		//GET NOW THE USERNAME 
		$third = "SELECT * FROM customer_entity_varchar where entity_id='".$userid."' ORDER BY value_id ASC LIMIT 0,4";
		
		$sq1 = mysql_query($third);
		
		$fname1 = mysql_fetch_array($sq1);
		
		$fname = "'".$fname1['value']."'";
		
		//GET NOW THE LASTNAME
		$fourth = "SELECT * FROM customer_entity_varchar where entity_id='".$userid."' ORDER BY value_id ASC LIMIT 1,4";
		
		$sq2 = mysql_query($fourth);
		
		$lname1 = mysql_fetch_array($sq2);
		
		$lname = $lname1['value'];
		
		if ($pass = $hash[0]) $auth =  "1";
		else $auth = "2";
		
	
	
	}else $auth = "3";

//return array ('item1'=>$auth,'item2'=>$user,'item3'=>$fname,'item4'=>$lname);
return array ('item1'=>'1','item2'=>$u,'item3'=>$fname,'item4'=>'slim');
}

Open in new window



//HERE GOES THE INCLUDES FOR CONNECTION 
include 'includes/dbconnect.php'; 
include 'includes/sql.php';
//END DATAS CONNECT

// where $_GET['callback'] = 'randomFn123'
$cb = $_GET['callback'];

//$arr = array ('item1'=>"I love jquery4u",'item2'=>"You love jQuery4u",'item3'=>"We love jQuery4u");

//START LOGIN
if (isset($_REQUEST['user'])){

	$arr = login($_REQUEST['user'],$_REQUEST['psw']);
	
	//$arr = array ('item1'=>"1",'item2'=>"medialablebanon@gmail.com",'item3'=>"We love jQuery4u");
	
}


if (preg_match('/\W/', $cb)) {
  // if $_GET['callback'] contains a non-word character,
  // this could be an XSS attack.
  header('HTTP/1.1 400 Bad Request');
  exit();
}


header('Content-type: application/javascript; charset=utf-8');
echo "/**/typeof ".$cb."==='function' && ".$cb."(".json_encode($arr).")";

Open in new window

0
 
LVL 43

Expert Comment

by:Rob
ID: 40568931
Your header should be "application/json" not JavaScript

Then follow that with

echo json_encode($arr);
0
 
LVL 43

Accepted Solution

by:
Rob earned 500 total points
ID: 40571958
http://www.egnyt.co/alain/?user=medialablebanon@gmail.com&psw=12345678 shows null.

As i've said previously, please change your code from:
header('Content-type: application/javascript; charset=utf-8');
echo "/**/typeof ".$cb."==='function' && ".$cb."(".json_encode($arr).")";

Open in new window

to:
header('Content-type: application/json; charset=utf-8');
echo json_encode($arr);

Open in new window

0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question