0

I am trying to pass a variable from jquery to php

My jquery code:

<script>
$(window).load(function(){
$.get("as.php",url="http://www.wired.co.uk/news/archive/2013-01/10/chinese-desert-mystery", function(data,status){
$('#lax').html(data);
});
});
</script>
<div id="lax" >
</div>

And my "as.php" file is as follows:

<?php
include('phpQuery.php');
$file = mysqli_real_escape_string($dbc, strip_tags(trim($_GET['url'])));  
phpQuery::newDocumentFileHTML($file);
$titleElement = pq('title'); 
$title = $titleElement->html();
echo '<a href="" >' . htmlentities( $title) . '</a><br/>';

foreach(pq('meta') as $li)

  if ((pq($li)->attr('name')=='description')||(pq($li)->attr('name')=='Description')){
   echo '<p>'.pq($li)->attr('content').'</p>';
}
?>

I am tryin to pass 'url' variable from jquery code to my "as.php" file , but not able to do so. Where must be I going wrong?

6
  • btw you shouldn't wait for window load, use DOM ready function. $(document).ready(fn) Commented Jan 10, 2013 at 21:25
  • Do you have error handling in your database code? You should try to return just $_GET['url'] to see if the problem is in javascript or php. Commented Jan 10, 2013 at 21:28
  • My jquery is little bad.. please tell me how it is useful than window load function. Commented Jan 10, 2013 at 21:29
  • api.jquery.com/ready Commented Jan 10, 2013 at 21:30
  • Thnx for the info gdoron. Commented Jan 10, 2013 at 21:31

3 Answers 3

4

You need to create an object

url="http://www.wired.co.uk/news/archive/2013-01/10/chinese-desert-mystery"

Should be:

{url :"http://www.wired.co.uk/news/archive/2013-01/10/chinese-desert-mystery"}

jQuery docs

Sign up to request clarification or add additional context in comments.

2 Comments

@RobbieDc, My PHP knowledge is about... zero and if you tried that already then your problem is in your PHP code.
Ok, Thanx for helping in the jquery part
1

I don't see you opening a database connection, so with the code you posted $dbc will be NULL.

That causes mysqli_real_escape_string to return NULL as well.

As you are not doing any database operations, you should get rid of mysqli_real_escape_string completely.

5 Comments

Tried opening db connection, did not work. Any other alternative?
@Robbie Dc Just remove it, you don't need it.
removed, now what to do next?
@Robbie Dc That depends on the problem you are having :-) I would just open as.php in the browser with the required query string and debug line by line.
My problem is am not able to pass the 'url' variable to php code, do i use $.ajax instead of $.get ?
0

in you jquery:

<script>
$(window).load(function(){
$.get("as.php",
{url:"http://www.wired.co.uk/news/archive/2013-01/10/chinese-desert-mystery"}, function(data){
$('#lax').html(data);
});
});
</script>

in your php try using $_REQUEST

<?php
include('phpQuery.php');
$file = mysqli_real_escape_string($dbc, strip_tags(trim($_REQUEST['url'])));  
phpQuery::newDocumentFileHTML($file);
$titleElement = pq('title'); 
$title = $titleElement->html();
echo '<a href="" >' . htmlentities( $title) . '</a><br/>';

foreach(pq('meta') as $li)

  if ((pq($li)->attr('name')=='description')||(pq($li)->attr('name')=='Description')){
   echo '<p>'.pq($li)->attr('content').'</p>';
}
?>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.