1

I am getting this error on var data =<?php echo serialize($msg);?>; line of the code below. console also raises data not defined error. I f put quotes around data, then this error goes but 1st error stays.

EDITED

//Raw xml
    $result = curl_exec($ch);
    curl_close($ch);
    $xml = simplexml_load_string($result);
     return $xml;
    } 
?>    
   <head>
   <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">
   </script>
   </head>
   <body>
   <script type="text/javascript"> <?php $msg = searchResults('windows');?>;
   var data ="<?php echo serialize($msg);?>";
   </script>
   <script type="text/javascript">
     $(document).ready(function() 
    {
       $.ajax({
       url: "script.php",
       type: "POST",
       data: data,
       success: function(){
       alert("success");
       }
      });
      return false;
     });
    </script>
    </body>
    </html>

I tried to see but couldn't figure out any problem

this is script.php

<?php
var_dump($_POST);
?>

this is xml from twitter

var data =O:16:"SimpleXMLElement":5:{s:2:"id";s:43:"tag:search.twitter.com,2005:search/#DIYSe_D";s:4:"link";a:4:{i:0;O:16:"SimpleXMLElement":1:{s:11:"@attributes";a:3:{s:4:"type";s:9:"text/html";s:4:"href";s:45:"http://search.twitter.com/search?q=%23DIYSe_D";s:3:"rel";s:9:"alternate";}}i:1;O:16:"SimpleXMLElement":1:{s:11:"@attributes";a:3:{s:4:"type";s:20:"application/atom+xml";s:4:"href";s:58:"http://search.twitter.com/search.atom?q=%23DIYSe_D&rpp=100";s:3:"rel";s:4:"self";}}i:2;O:16:"SimpleXMLElement":1:{s:11:"@attributes";a:3:{s:4:"type";s:37:"application/opensearchdescription+xml";s:4:"href";s:40:"http://search.twitter.com/opensearch.xml";s:3:"rel";s:6:"search";}}i:3;O:16:"SimpleXMLElement":1:{s:11:"@attributes";a:3:{s:4:"type";s:20:"application/atom+xml";s:4:"href";s:84:"http://search.twitter.com/search.atom?q=%23DIYSe_D&rpp=100&since_id=7856019371724800";s:3:"rel";s:7:"refresh";}}}s:5:"title";s:25:"#DIYSe_D - Twitter Search";s:7:"updated";s:20:"2010-11-24T21:53:28Z";s:5:"entry";a:3:{i:0;O:16:"SimpleXMLElement":7:{s:2:"id";s:44:"tag:search.twitter.com,2005:7552404006371328";s:9:"published";s:20:"2010-11-24T21:53:28Z";s:4:"link";a:2:{i:0;O:16:"SimpleXMLElement":1:{s:11:"@attributes";a:3:{s:4:"type";s:9:"text/html";s:4:"href";s:50:"http://twitter.com/_smir/statuses/7552404006371328";s:3:"rel";s:9:"alternate";}}i:1;O:16:"SimpleXMLElement":1:{s:11:"@attributes";a:3:{s:4:"type";s:9:"image/png";s:4:"href";s:67:"http://s.twimg.com/a/1289849896/images/default_profile_5_normal.png";s:3:"rel";s:5:"image";}}}s:5:"title";s:59:"#DIYse_D DELIVERAB: twitter messages 2_inc 1, 19th OCT 2010";s:7:"content";s:248:"<a href="http://search.twitter.com/search?q=%23DIYse_D" 
onclick="pageTracker._setCustomVar(2, 'result_type', 'recent', 3);pageTracker._trackPageview('/intra/hashtag/#DIYse_D');"><b>#DIYse_D</b></a> DELIVERAB: twitter messages 2_inc 1, 19th OCT 2010";s:7:"updated";s:20:"2010-11-24T21:53:28Z";s:6:"author";O:16:"SimpleXMLElement":2:{s:4:"name";s:13:"_smir (Smeer)";s:3:"uri";s:24:"http://twitter.com/_smir";}}i:1;O:16:"SimpleXMLElement":7:{s:2:"id";s:44:"tag:search.twitter.com,2005:7551711354822656";s:9:"published";s:20:"2010-11-24T21:50:42Z";s:4:"link";a:2:{i:0;O:16:"SimpleXMLElement":1:{s:11:"@attributes";a:3:{s:4:"type";s:9:"text/html";s:4:"href";s:58:"http://twitter.com/Babar_Shahzad/statuses/7551711354822656";s:3:"rel";s:9:"alternate";}}i:1;O:16:"SimpleXMLElement":1:{s:11:"@attributes";a:3:{s:4:"type";s:9:"image/png";s:4:"href";s:103:"http://a1.twimg.com/profile_images/1090185625/29465_391454998679_533808679_3864564_6071800_n_normal.jpg";s:3:"rel";s:5:"image";}}}s:5:"title";s:58:"#DIYse_D DELIVERAB: twitter messages 2_inc2, 24th OCT 2010";s:7:"content";s:247:"<a href="http://search.twitter.com/search?q=%23DIYse_D" onclick="pageTracker._setCustomVar(2, 'result_type', 'recent', 3);pageTracker._trackPageview('/intra/hashtag/#DIYse_D');"><b>#DIYse_D</b></a> DELIVERAB: twitter messages 2_inc2, 24th OCT 
2010";s:7:"updated";s:20:"2010-11-24T21:50:42Z";s:6:"author";O:16:"SimpleXMLElement":2:{s:4:"name";s:32:"Babar_Shahzad (Babar Shahzad Ch)";s:3:"uri";s:32:"http://twitter.com/Babar_Shahzad";}}i:2;O:16:"SimpleXMLElement":7:{s:2:"id";s:44:"tag:search.twitter.com,2005:7550668919283712";s:9:"published";s:20:"2010-11-24T21:46:34Z";s:4:"link";a:2:{i:0;O:16:"SimpleXMLElement":1:{s:11:"@attributes";a:3:{s:4:"type";s:9:"text/html";s:4:"href";s:58:"http://twitter.com/Babar_Shahzad/statuses/7550668919283712";s:3:"rel";s:9:"alternate";}}i:1;O:16:"SimpleXMLElement":1:{s:11:"@attributes";a:3:{s:4:"type";s:9:"image/png";s:4:"href";s:103:"http://a1.twimg.com/profile_images/1090185625/29465_391454998679_533808679_3864564_6071800_n_normal.jpg";s:3:"rel";s:5:"image";}}}s:5:"title";s:53:"#DIYse_D  DELIVERAB: twitter messages 1, 9th OCT 2010";s:7:"content";s:242:"<a href="http://search.twitter.com/search?q=%23DIYse_D" onclick="pageTracker._setCustomVar(2, 'result_type', 'recent', 3);pageTracker._trackPageview('/intra/hashtag/#DIYse_D');"><b>#DIYse_D</b></a>  DELIVERAB: twitter messages 1, 9th OCT 2010";s:7:"updated";s:20:"2010-11-24T21:46:34Z";s:6:"author";O:16:"SimpleXMLElement":2:{s:4:"name";s:32:"Babar_Shahzad (Babar Shahzad Ch)";s:3:"uri";s:32:"http://twitter.com/Babar_Shahzad";}}}};

This is my json equivalent code for same but its not working. I put it here because many if not all people are referring me add json to this code i.e OP, actually what I was wanting here but OP to send xml to php as json did not work for me earlier' *This code raises no console errors, but failure alert is ouput * curl is sending results, I have tested that in a test php block just before ajax in HTML.

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">

 <html>
 <?php
 function searchResults($q) {

  $host = "http://search.twitter.com/search.atom?q=" . urlencode( $q ) . "&rpp=100";
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $host);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

//Raw xml
  $result = curl_exec($ch);
  curl_close($ch);
  $xml = simplexml_load_string($result);
  return json_encode($xml);
  } 
 ?>
 <head>
 <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js">
 </script>
 </head>
 <body>
 <script type="text/javascript"> var msg_top = <?php echo searchResults('windows');?>;
 </script>
 <script type="text/javascript">
$(document).ready(function() 
{
   $.ajax({
   url: "script.php",
   type: "POST",
   dataType: "json",
   data: msg_top,
   success: function(msg){
   alert("success");
  }
 });
alert("failure");
});
</script>
</body>
</html>
XCeptable
  • 1,247
  • 6
  • 25
  • 49
  • 1
    Maybe try quoting it like var data =""; ?? – ameer Nov 25 '10 at 21:00
  • Shouldn't data: "data", be data: data, – Malachi Nov 25 '10 at 21:01
  • [Haven’t I told you just to use `echo searchResults('windows')`?](http://stackoverflow.com/questions/4276432/whats-wrong-with-this-assignment-of-json-object-from-php-to-javascript/4277246#4277246) – Gumbo Nov 25 '10 at 21:02
  • @ameer, no, it do not make any affect – XCeptable Nov 25 '10 at 21:03
  • @Malachi, if I use data, then it raises error, 'data not defined' – XCeptable Nov 25 '10 at 21:05
  • @ Gumbo, I put full code. I am trying to send xml to php. I am not using json for this as json seem to not working after a lot of effort & time by help of you all people – XCeptable Nov 25 '10 at 21:15
  • @ Gumbo, I added json code too in OP bec all people were advising to use json, as you can see, its like what you referred earlier. Console shows no error but now I added a failure block & it outputs failue – XCeptable Nov 26 '10 at 07:46

4 Answers4

6

Try

var data="<?php echo json_encode(serialize($msg));?>";

EDIT: I'm sorry, the json_encode() function automatically adds the quotes to strings, so the code should look like:

var data=<?php echo json_encode(serialize($msg));?>;


And there's some redundant semicolon (;) after the first ?>, you should remove it.

rhino
  • 13,543
  • 9
  • 37
  • 39
  • the data from searchResult method i.e. contained by php object is xml object. So should I encode it to json? I remove that but affect that have. – XCeptable Nov 25 '10 at 21:23
  • Yea, I think so - the XML code may contain some special characters and `json_encode()` will help you get rid of them by escaping them with backslashes. I'm sorry if I didn't understand you well, but I hope I did. ;) – rhino Nov 25 '10 at 21:29
  • no sorry, thank you very much for ur help. Actually I had had spent around 3 days with sending json but that did not work. Now I am trying to send xml object to php. If I have to use json, then I could from searchResult function i.e. php function sending results could send json – XCeptable Nov 25 '10 at 21:34
  • I added to OP json code too that I used, you can see it if you figure out any problem – XCeptable Nov 25 '10 at 21:53
5

serialize does not output JavaScript.

Try json_encode instead.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

<?php $msg = searchResults('windows');?>; Why is ; here? (the last one)
Edit:
Is something that i dont understand here <script type="text/javascript"> <?php $msg = searchResults('windows');?>;
var data =<?php echo serialize($msg);?>;
</script>

Why do you call <?php $msg = searchResults('windows');?>; inside <script>...</script> and why is there a ; after

cristian
  • 8,676
  • 3
  • 38
  • 44
  • yes, I remove that, actually it left after an edit. I am calling it inside script bec I want to assign js var value of php var – XCeptable Nov 25 '10 at 21:30
0

You're emitting Javascript code that contains the raw return value of PHP's serialize method.

This return value is not valid Javascript syntax, so you're getting a syntax error.

You need to wrap it in a string, and hope and pray that it never includes quotes or newlines.

Also, you probably want to pass the data variable to $.ajax, not the literal string "data".

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • actually what I am trying here to send xml to php. I have had almost 3 days work on json for same problem but json did not work. I have had probably a clean code for that but its not working json way. I could put it in OP, you could see & may figure out some problem of that. I added quotes to data as If I remove those, console errors 'data not defined' – XCeptable Nov 25 '10 at 21:41
  • @XCeptable: That's because the variable isn't defined. (because of the syntax error) – SLaks Nov 25 '10 at 21:51
  • if you mean I should put quotes around php assignment statements the I did that & it do not have ant affect. Would be very grate of you if you could see my json code that I added in OP, that is showing no error at all but var_dump prints array(0) { } – XCeptable Nov 25 '10 at 22:12