2013-01-10 45 views
0

我試圖從jQuery的傳遞變量到PHP從jQuery的傳遞變量到PHP

我的jQuery代碼:

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

而我的 「as.php」 文件如下:

<?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>'; 
} 
?> 

我嘗試從jquery代碼傳遞'url'變量到我的「as.php」文件,但無法做到這一點。我必須在哪裏出錯?

+0

順便說一句,你不應該等待窗口負載,使用DOM就緒功能。 '$(document).ready(fn)' – gdoron

+0

你的數據庫代碼中有錯誤處理嗎?您應該嘗試返回'$ _GET ['url']'來查看問題出在javascript還是php。 – jeroen

+0

我的jquery是一點壞..請告訴我它是如何比窗口加載功能有用。 –

回答

4

你需要創建一個對象

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

應該是:

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

jQuery docs

+0

我想這一點,但沒有奏效 –

+0

@RobbieDc,我的PHP知識是關於......零,如果你試過已經那麼你的問題出在你的PHP代碼中。 – gdoron

+0

好的,Thanx幫助在jquery部分 –

1

我沒有看到你跟你貼$dbc代碼打開一個數據庫連接,所以將是NULL

這也導致mysqli_real_escape_string也返回NULL

由於您沒有做任何數據庫操作,您應該完全清除mysqli_real_escape_string

+0

嘗試打開數據庫連接,沒有工作。任何其他的選擇? –

+0

@Robbie Dc只要刪除它,你不需要它。 – jeroen

+0

刪除,現在該怎麼辦? –

0
在你的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> 

在你的PHP嘗試使用$_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>'; 
} 
?> 
+0

不會工作。 –

+0

這是爲什麼!你有什麼錯誤? –

+0

無法將jquery變量傳遞給php代碼 –