简体   繁体   English

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

[英]how to post value to another page and get back the data not via an iframe(php)

I have many tags.我有很多标签。 I want click each tag, then post/get the tag's value to another page.我想单击每个标签,然后将标签的值发布/获取到另一个页面。

In another page, received the values and make a mysql query.在另一页中,接收到值并进行 mysql 查询。 Then return the resalt data to the first page(do not make an iframe).然后将 resalt 数据返回到第一页(不要制作 iframe)。

I think jquery post and load may be can do that (but have no idea how to combine two functiton).我认为 jquery 发布和加载可能可以做到这一点(但不知道如何结合两个功能)。 Or maybe there have any other way.或者也许还有其他方法。 Can any one give me some simple example?谁能给我一些简单的例子? Thanks.谢谢。

update更新

products.php产品.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数据.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>

With JavaScript You cannot do AJAX calls to other domains.使用 JavaScript 您不能对其他域进行 AJAX 调用。 This is a browser "feature" that blocks this calls as prevention to corss-domain scripting...这是一个浏览器“功能”,可阻止此调用以防止跨域脚本编写...

But still You can do it with PHP.但是您仍然可以使用 PHP 来做到这一点。 If You also want to get the response regarding to Your post, You can use cURL http://php.net/curl .如果您还想获得关于您的帖子的回复,您可以使用 cURL http://php.net/curl If You want to use just get, it is much simpler, You only call from Your PHP:如果您想使用 get,它会简单得多,您只需从您的 PHP 调用:

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

Depending to the response data format You can output them directly or parse it first.根据响应数据格式,可以直接 output 或者先解析。

If You need them to be outputed on another page (that You are now), You can save the data to the $_SESSION and do a redirect...如果您需要将它们输出到另一个页面(您现在是),您可以将数据保存到 $_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');

and on Your my_first_page.php You can do和你的 my_first_page.php 你可以做

var_dump($_SESSION['response']);

If your pages are ALL on the same domain then you can do the following:如果您的页面都在同一个域上,那么您可以执行以下操作:

Suppose that you have the first page, in which you do not want any iframe, the name of the page is yourPage.php.假设您有第一页,其中不想要任何 iframe,该页的名称是 yourPage.php。 And suppose that you have one other page whose name is yourService.php并假设您还有另一个页面,其名称为 yourService.php

What I understand from your question is that you just want to perform a simple AJAX request in order to POST/GET some data to a page, and retrieve the response.我从您的问题中了解到,您只想执行一个简单的 AJAX 请求,以便将一些数据发布/获取到页面并检索响应。

With jQuery you can do that.使用 jQuery 您可以做到这一点。

On yourPage.php you will have:在 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>

And on yourService.php:在 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
?>

I hope my answer will help you.希望我的回答对你有所帮助。 This solution does not work if yourService.php is not on the same domain name that yourPage.php (the javascript engine of your browser will block that)如果 yourService.php 与 yourPage.php 不在同一个域名上,则此解决方案不起作用(浏览器的 javascript 引擎将阻止该域名)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM