Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Jsonp retreive data problem!

Posted on 2015-01-24
6
Medium Priority
?
177 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 111

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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
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 …

722 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