How to Post same data to 2 pages in PHP, I need to select data from database in index.php and load_page.php
I try to do this:
<a href="index.php?cat=Gadgets && pag/load_data.php?cat=Gadgets" title="Common">
thanks
If you want to send data to 2 different pages without redirecting the user through 2 pages you should have a look at AJAX with jQuery
If you want to open 2 pages with GET data you can easily do that with jQuery. Have jQuery listen to user clicks on the link and open a new tab.
Something like (not tested)
<script>
$(document).ready(function(){
$('#link').bind('click', function(){
window.open(url2, '_blank');
});
});
</script>
<a href='mypage.php?data=bla&data2=bleh' id='link'>GO</a>
It does not make a huge amount of sense to send the same data to 2 different pages, however if it must be done I can think of 2 immediate options.
Below is an "untested" cURL option
<?php
## Data - from form or other source.
$data = array(
"name" => "c.bavota",
"website" => "http://bavotasan.com",
"twitterID" => "bavotasan"
);
## Send data to 1 or more pages and collect responses.
$response_from_page1 = post_to_url("http://yoursite.com/page1.php", $data);
$response_from_page2 = post_to_url("http://yoursite.com/page2.php", $data);
## Function to make the POST requests.
function post_to_url($url, $data) {
$fields = '';
foreach($data as $key => $value) {
$fields .= $key . '=' . $value . '&';
}
rtrim($fields, '&');
$post = curl_init();
curl_setopt($post, CURLOPT_URL, $url);
curl_setopt($post, CURLOPT_POST, count($data));
curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($post);
curl_close($post);
return $result;
}
?>
Each of your pages would return a text string, maybe something like true or false, depending on the exact application.
This is a slightly amended code from another tutorial.
I did it this way
<script type="text/javascript">
$(document).ready(function(){
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (!results) { return 0; }
return results[1] || 0;
}
function loading_show(){
$('#loading').html("<img src='images/loading.gif'/>").fadeIn('fast');
}
function loading_hide(){
$('#loading').fadeOut('fast');
}
function loadData(page){
loading_show();
caty = $.urlParam('cat');
$.ajax
({
type: "POST",
url: "pagination/load_data.php",
data: {page: page, cat: caty},
success: function(msg)
{
$("#container").ajaxComplete(function(event, request, settings)
{
loading_hide();
$("#container").html(msg);
});
}
});
}
loadData(1); // For first time page load default results
$('#container .pagination li.active').live('click',function(){
var page = $(this).attr('p');
loadData(page);
});
$('#go_btn').live('click',function(){
var page = parseInt($('.goto').val());
var no_of_pages = parseInt($('.total').attr('a'));
if(page != 0 && page <= no_of_pages){
loadData(page);
}else{
alert('Enter a PAGE between 1 and '+no_of_pages);
$('.goto').val("").focus();
return false;
}
});
});
</script>
where cat: caty was gotten from URL caty = $.urlParam('cat');
using this function
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (!results) { return 0; }
return results[1] || 0;
}
and page
is the page
data of pagination sent from the same page index.php
both page
(gotten from index.php) and caty
gotten (from another page via link click), will be sent to script pagination in pagination/load_data.php
.
in this case I can filter data to make pagination based on category and number/page choose.
Said