如何将值发布到另一个页面并通过iframe(php)获取数据

时间:2011-05-05 09:14:14

标签: php post get

我有很多标签。我想点击每个标签,然后将标签的值发布/获取到另一个页面。

在另一个页面中,收到值并进行mysql查询。然后将重新调整数据返回到第一页(不要制作iframe)。

我认为jquery post和load可能会这样做(但不知道如何组合两个functiton)。或许还有其他办法。任何人都可以给我一些简单的例子吗?感谢。

更新

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>

2 个答案:

答案 0 :(得分:0)

使用JavaScript您无法对其他域执行AJAX调用。这是一个浏览器“功能”,它阻止这些调用作为对corss-domain scripting的预防......

但你仍然可以用PHP做到这一点。如果您还想获得有关您帖子的回复,可以使用cURL http://php.net/curl。如果你想使用get,它会更简单,你只能从你的PHP调用:

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

根据响应数据格式您可以直接输出或首先解析它。

如果您需要将它们输出到另一个页面(现在是您),您可以将数据保存到$ _SESSION并进行重定向......

$_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']);

答案 1 :(得分:0)

如果您的网页在同一个网域上是全部的,那么您可以执行以下操作:

假设您有第一页,您不想要任何iframe,页面名称是yourPage.php。 并假设您有另一个页面,其名称为yourService.php

我从您的问题中理解的是,您只是想执行一个简单的AJAX请求,以便将一些数据发布/获取到页面,并检索响应。

使用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与yourPage.php(浏览器的javascript引擎将阻止该域名)不在同一域名上,则此解决方案无效。