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. Then return the resalt data to the first page(do not make an iframe).
I think jquery post and load may be can do that (but have no idea how to combine two functiton). Or maybe there have any other way. Can any one give me some simple example? Thanks.
update
products.php
<开发者_如何学Go;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>
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. And suppose that you have one other page whose name is 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.
With jQuery you can do that.
On yourPage.php you will have :
<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 :
<?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)
With JavaScript You cannot do AJAX calls to other domains. This is a browser "feature" that blocks this calls as prevention to corss-domain scripting...
But still You can do it with PHP. If You also want to get the response regarding to Your post, You can use cURL http://php.net/curl. If You want to use just get, it is much simpler, You only call from Your 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.
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['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
var_dump($_SESSION['response']);
精彩评论