Solved

Jsonp retreive data problem!

Posted on 2015-01-24
6
155 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
php56-php-mcrypt for rhel7 php56 1 98
php error 27 55
restriction of entering a a page 5 29
Get data from two MySQL tables 6 18
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …

685 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