Solved

Jsonp retreive data problem!

Posted on 2015-01-24
6
148 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
  • 3
6 Comments
 
LVL 108

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 42

Expert Comment

by:Rob Jurd, EE MVE
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 42

Expert Comment

by:Rob Jurd, EE MVE
ID: 40568931
Your header should be "application/json" not JavaScript

Then follow that with

echo json_encode($arr);
0
 
LVL 42

Accepted Solution

by:
Rob Jurd, EE MVE 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

932 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now