yii2: update/refresh/reload a page using Pjax different from requesting page - javascript

I have asked this question, but I think that wasn't clear enough so I am putting it differently. I have a _form.php page and a grid-view(index.php)page.
I am sending a Pjax request from _form.php and want to update/refresh the grid-view(index.php)page.
on top my _form.php I am having this code.
<?php
$this->registerJs(
'$("document").ready(function(){
$("#new_medicine").on("pjax:end", function() {
$.pjax.reload({container:"#medicine"}); //Reload GridView
});
});'
);
?>
Now the container "#medicine" is not on _form.php page, but grid-view(index.php) page. So how can I modify the above code so that it updates/refresh the container "#medicine" in index.php page.
I think I have explained the situation correctly. please tell me, if more information is needed.
Thanks.

Try using $this::POS_READY instead of wrapping your code in $("document").ready(function(){})
$js = '$("#new_medicine").on("pjax:end", function() {
$.pjax.reload({container:"#medicine"}); //Reload GridView
});'
$this->registerJs($js, $this::POS_READY);
EDIT
Apparently you want to reload the gridview that is open on another client's index.php after data is inserted using _form.php.
It isn't possible to send jQuery commands to another client (browser) and have it executed.
You can for example reload the gridview on the index.php every x seconds or minutes.
$js = 'function refresh() {
$.pjax.reload({container:"#medicine"});
setTimeout(refresh, 5000); // restart the function every 5 seconds
}
refresh();';
$this->registerJs($js, $this::POS_READY);

Related

How to load a function only once with JS or PHP?

I've been making some research these hours and without any success. I will explain my situation and I hope you help me in some way.
I'm making a project for my university, on my site I use the database on a local server with PHP my admin, PHP coding js functions, and html/css. My problem is that when I load the "dashboard site" I want to popup a modal from bootstrap which is a form to complete the second phase of registration, and that popup I do it with :
<script type="text/javascript">
$(window).on('load',function(){
$('#exampleModal3').modal('show');
});
</script>
I've tried to use "include_once" PHP function , or with JS use some tricks like: if(!stuffdone){doSomething ... stuffdone=true;}, but it's not working, when I load the dashboard the popup appears again and I cant make it show only once. My idea is to make it appear only once on the first login of the user and then it won't appear anymore and instead, it will be a button to change the user data which will call the modal popup. Also sorry for my English, is terrible.
Thank you
You could try localstorage to get the registration modal going. But you will have to store it in db and write a function in php that checks users registration progress. and keep in mind that localstorage is just clientside on that particular browser. Something like this pseudocode:
var hasRegged = localstorage.getItem('hasreg');
function checkReg(){
if (hasRegged===null){
$('#exampleModal3').modal('show');
localstorage.setItem('hasreg','1');
}
}
$(window).on('load',function(){
checkReg();
});
$(document).on('click','.finishedregbutton', function(){
//Save the clickdata to db
});
you could also do this with ajax:
function checkReg(){
$.ajax({
"url":"regcheck.php",
"data":"uuid=<?php echo $_SESSION['uuid'];?>",
"type":"POST",
"dataType":"JSON",
"success": function(data){
if(data.status=='completed'){
$('#exampleModal3').modal('show');
}
});
}
$(window).on('load',function(){
checkReg();
});

Refresh Page with URL Hash after Submitting Form with jQuery Tabs

I'm a long time self taught user, just never had an issue that wasn't already answered in some form or another. Awesome community thank you all!
So I am dumbfounded as to why javascript will not properly refresh my page.
I have a very large page that I use with jquery and vertical tabs tagged with url hashes. This is a new function I recently added after updating to the newest version.
Upon clicking submit the form refreshes and if $_POST[update] is set it will run a set of queries. Then I have a javascript timeout that refreshes the page (ideally to the correct tab ex: home/database.php?file=101#tab3)
an excerpt of my messy code:
if(isset($_POST['update']))
{ do a bunch of sql stuff
....
<pre>
<?PHP
//unset update variable
$_POST = array();
//print_r(get_defined_vars());
?>
</pre>
</br>
<script type="text/javascript">
function Redirect() {
$('#redirect').trigger('click');
}
var currenturl = window.location.href;
document.write("<form action=" + currenturl +"><input type=submit name=redirect value=Redirect></form>");
document.write("</br>You will be redirected back to the file in 2 seconds.");
setTimeout('Redirect()', 2000);
</script>
<?php
mysql_close($conn);
}
else
{ display usual data....check boxes etc
Now I've tried refreshing in every conceivable way. I clear out the $_POST array so the page should reload displaying information, but it doesn't. However my confirmation of queries being ran show that 0 rows updated, so its not triggering the sql but also isn't properly refreshing still.
I found a function that stored the last clicked tab in the browsers session, however when you switch files, it would skip to that tab. Instead I would want to show the default tab.
I even tried made a button that on click it goes to the proper url which is shown above. It works when I click it manually but does not work when I trigger it via javascript. Even goes to the appropriate tab.
I am updating the url in the address bar using the below function. Also using the latest version of chrome.
<script>
$( "#tabs" ).tabs({
activate: function(event, ui) {
window.location.replace('#' + $(ui.newPanel).attr('id'));
var curTab = $(ui.newPanel).attr('id');
console.log(curTab);
}
});
</script>
Why don't you instead of sending that number after <form_url>?<params>#3 send that number as a request param in the url, for example like this <form_url>?tab=3?

PHP DOM function file_get_html call after Complete Page load

We are trying to fetch url from PHP DOM but webapge that we try to fetch has functionality (change price after page load) so now we need that PHP Dom function file_get_html get HTML after this onload functionality of remote page has been completed.
$html = file_get_html('https://www.example.com');
// Find all article blocks
foreach($html->find('div.pad15 h4 a') as $article) {
echo $article->innertext.'<br>';
}
foreach($html->find('p.sprc span.fb') as $pr){
echo $pr->innertext.'<br>';
}
$html->clear();
unset($html);
if I right undestend question you problem is get html after loading js. It's impossible. Becouse php open connection and get data at once
answer about you question is "NO"
php can not receive javascript rendered result. javascript is running on browser side and it not possible with php.
alternative is to do task with
http://phantomjs.org/
var page = require('webpage').create();
page.open('http://google.com', function () {
console.log(page.content);
phantom.exit();
});

How to refresh the WordPress home page without user noticing?

I would like to refresh the home page every X sec. I found this solution:
Install Auto Refresh Single Page plugin
Insert to header.php the lines:
if (is_front_page()) {
echo '<META HTTP-EQUIV="REFRESH" CONTENT="5">' ;
}
It refreshes the page but I notice that the page is reloaded. I would like it to happen smoothly. I don't want the refresh to make the page disappear for milliseconds. I want to refresh without the user noticing.
I have a page build with Wordpress. This page have few divs. Part of them list post type - and I need i to updated in case a new post submitted. Some divs need to change according to time of the day or the week and display different data. The page need to show all the time like a TV and information should be update automatically with no human touch. I tried to use ajax, but I don't know how to point it to reload specific div that built from Wordpress - I have no URL for specific div.
Check this out
if (is_front_page()) {
?>
<script>
setTimeout(function() { window.location = ""; }, 1000); //1 sec = 1000
</script>
<?php
}
I read your question, And as i thinking, you want update your posts on users homepage without any reloading. This can be done using "AJAX PHP". You should find plugins that uses "AJAX PHP" or "AJAX JAVASCRIPT".I hope this would be useful for you.

Is there any way to redirect to a new page and write some content with jquery on that page?

I am making a function which searches the database for flight information. I want user to be redirected on "ticket.php" if the response is true. I want to update the content of the page "ticket.php" using jquery. But jquery doesn't work. Am I doing something wrong here ? Here's part of javascript. It's an external javascript. It's included in both "search.php" and "ticket.php" page. This function gets invoked by clicking on search button in "search.php" page.
if(data){
setTimeout("location.href = 'ticket.php';",3000); // Redirect to new page
//Change content of "ticket.php" using jquery.
$("#source").text(x);
$("#destination").text(y);
$("#date").text(z);
$("#no_person").text(person);
$("#trip_type").text(type);
}
else
$("#alert").text("Oops! No Flight matches your criteria ! ");
When you set location.href to ticket.php, the user is then redirected to that new page (ticket.php). Then the browser will load that new page and will no longer use the javascript you have on your current page.
You will have to make the data appear on ticket.php, using e.g. url parameters taken from what they searched like this:
window.location.href = 'ticket.php?from=norway&to=sweden';
The reason this is not working for you is because when you redirect to ticket.php the page is reloaded and all of the javascript on the page is refreshed, losing whatever value they had before. To keep the data you have to send the information to ticket.php
There are a couple of ways to accomplish this, ordered in preferred method.
all assuming you are requesting the page like this: ticket.php?from=norway&to=sweden
php on the page ticket.php
<?php
echo $_GET['from'];
echo $_GET['to'];
?>
javascript on the page ticket.php
solution for getting params from this post:https://stackoverflow.com/a/1404100/2033671
alert(getURLParameter('from'));
alert(getURLParameter('to'));
jQuery from a different page on the same domain
$.get('ticket.php',function(response){
$html = $(response);
$html.find("#destination").text(y);
$html.find("#source").text(x);
$("html").html($html);
});

Categories

Resources