Solved

Jsonp retreive data problem!

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
php mail headers 2 12
WooCommerce Sort by Date 4 9
Wordpress type image upload 10 29
PHP Sum Column in Table 3 27
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

705 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

14 Experts available now in Live!

Get 1:1 Help Now