2011-05-05 28 views
0

我有很多標籤。我想單擊每個標籤,然後將標籤的值發佈/獲取到另一個頁面。如何將值發佈到另一頁並通過iframe取回數據(php)

在另一頁中,收到值並進行mysql查詢。然後將resalt數據返回到第一頁(不要創建iframe)。

我認爲jquery的帖子和加載可能可以做到這一點(但不知道如何結合兩個功能)。或者也許有其他方法。任何人都可以給我一個簡單的例子嗎?謝謝。

更新

products.php

<a href="data.php?get=hml03">model:hml03</a> 
<a href="data.php?get=hml04">model:hml04</a> 
<a href="data.php?get=hml05">model:hml05</a><!--post value from products.php--> 
...<!--many other tags --> 
<div class="show_data"></div><!-- get the data back show in here without refresh the page. 

data.php

<div id="data"> 
<?php 
$result = mysql_query("SELECT id,name,details,add_date,model FROM ctw_products WHERE (MATCH (name,details,model) AGAINST ('+$_GET['get']' IN BOOLEAN MODE) Order By add_date DESC LIMIT 20 "); // get value with a mysql query 
while ($row = mysql_fetch_array($result)) 
{ 
echo '<div class="name">'.$row['name'].'</div>'; 
echo '<div class="model">'.$row['model'].'</div>'; 
echo '<div class="details">'.$row['details'].'</div>';// how to return this part of datas back to products.php 
} 
?> 
</div> 

回答

0

如果你的頁面都在同一個域,那麼你可以做到以下幾點:

假設你擁有的第一頁,在其中您不希望任何的iframe的名稱,該頁面是yourPage.php。 假設你有一個其他頁面的名字是yourService.php

我從你的問題中瞭解到,你只是想執行一個簡單的AJAX請求,以POST/GET一些數據到頁面,並檢索響應。

有了jQuery,你可以做到這一點。

在yourPage.php你將有:

<html> 
[...] 
<script type="text/javascript"> 
function callService() { 
$.ajax({ 
    type: "GET", // or "POST", 
    data: "myLogin=login&myPassword=password", 
    url: "./yourService.php", 
    success: function(data){ 
    alert("The service told me : " + data); // data is the response, make something with  that like putting your data on your HTML page 
    } 
}); 
} 
</script> 
[...] 
<a href="#" onclick="callService(); return false;">Call the service</a> 
[...] 
</html> 

而且在yourService.php:

<?php 
// retrieve the data by looking at $_GET['login'] and $_GET['password'] (or $_POST) 
// clean-up the variables if needed 
// perform the MySQL query (by using mysql_query() for example) 
// retrieve the data from the database 
echo 'You are now connected'; // send something to yourPage.php by writing some data with the echo() function for example 
?> 

我希望我的回答可以幫助你。 如果yourService.php與您的Page.php(您的瀏覽器的javascript引擎將阻止該域名)不在相同的域名上,此解決方案不起作用

+0

hot將href值發佈到'ajax - > data'部分?在'yourPage.php'中,我的意思是在'data:「myLogin =調用服務''時寫入'調用服務'',如果我有很多'href'鏈接,謝謝。 – 2011-05-05 09:46:40

+0

'1st link 2nd link 3td link ' 喜歡這個? (當然修改Javascript函數callService()=>添加一個參數=> callService(myParam)) – mnflz 2011-05-05 12:18:57

0

使用JavaScript你不能做AJAX調用到其他域。這是一個瀏覽器「功能」,阻止這種呼籲作爲預防corss域腳本...

但仍然可以用PHP來做到這一點。如果您還想獲得有關您的帖子的回覆,您可以使用cURL http://php.net/curl。如果你想用剛剛得到的,它是非常簡單,你只從你的PHP撥打:

$response = file_get_contents('http://www.domain.com/some_page.php?data=' . $my_get_data); 

根據該響應的數據格式可以輸出它們直接或先解析它。

如果你需要他們被另一個頁面(你現在),你可以將數據保存到$ _SESSION,做一個重定向outputed ...

$_SESSION['response'] = file_get_contents('http://www.domain.com/some_page.php?data=' . $my_get_data); 
header('Location: http://www.my_domain.com/my_first_page.php'); 

和你my_first_page.php你可以做

var_dump($_SESSION['response']); 
+0

php可以先從第二頁加載日期而不刷新?我認爲如果沒有js,php就無法工作。 – 2011-05-05 09:42:36

+0

是的,沒有刷新你必須從JS AJAX調用你的域上的PHP腳本,這個PHP腳本將得到並返回其他域的數據... – shadyyx 2011-05-05 09:55:39

+0

那麼,如何使用js,判斷wheater第二頁已完成mysql查詢,然後做一個'$ response'?對不起,我是一個新手。再次感謝。 – 2011-05-05 10:00:54

相關問題