I have a page where a user creates an item to auction it. If he submits the item creation form, the browser automatically redirects him to the newly created page via the following line of php code:
header('Location: item.php?itemid='.$itemid);
I would like to display a notification right after the item creation (on the newly created page) saying that the item has been created. This is the (working) code I use to call the notifications:
<script type="text/javascript">
$(function(){
$container = $("#container").notify();
notifyItemCreation();
});
</script>
EDIT: I think people are misunderstanding the problem.
So my sellitem.php page contains a form to sell an item, if this is submitted it gets send to createitem.php, this is where I do the validation checks.
If everything is okay this is where I redirect to the newly created item.php?item='$itemid' page. I want on this (item.php?item='$itemid') page the notification to be displayed.
EDIT2: I do know how to create the item.php?itemid='$itemid' page, this gets done perfectly, it is the notification to appear that is the problem. I am using the notifications from http://www.erichynds.com/blog/a-jquery-ui-growl-ubuntu-notification-widget
Can the people who are downvoting also explain why they are.
You can use session variables. In the next request if a certain variable exists or indicates that the current item is new, you can echo the script tag and unset the variable.
You could just wrap the JS in a condition
<?php if(is_numeric($itemid)){ ?>
<script type="text/javascript">
$(function(){
$container = $("#container").notify();
notifyItemCreation();
});
</script>
<?php } ?>
You could use ISSET() as well
Related
How to submit the cart request-quote form after page load?
Below code is worked fine -
<?php if(!isset($_GET['updated'])) { ?>
<script>
jQuery(document).ready(function($) {
jQuery("#yith-ywraq-form").submit();
});
</script>
<?php } ?>
But avoid submitting forms, again and again, I reload to this action
site.com/request-quote/?updated=data
But if someone refreshes the same page site.com/request-quote/?updated=data
then I have no option to submit the form again on page load.
do you have a solution for this problem?
My requirement is I want to submit the cart request-quote form 1 time after page load?
You may use localStorage for this.
Take a flag in set some initial value of it.
When the form submits update it's value.
By checking the same you can avoid getting submit.
<?php if(!isset($_GET['updated'])) { ?>
<script>
if(!localStorage.getItem('isFormAlreadySubmit')){
jQuery(document).ready(function($) {
jQuery("#yith-ywraq-form").submit();
localStorage.setItem('isFormAlreadySubmit', true);
});
}
</script>
<?php } ?>
You can move $_GET['updated'] to localStorage or sessionStorage, then you don't need to reload the page to ?updated=data page anymore.
localStorage will save updated value even when the browser is closed and reopened.
sessionStorage will save updated value but it will be cleared if the browser is closed.
That depends on you to choose which one. So I make a demo with sessionStorage which I prefer because when the user closes the browser, it will clear the updated value, maybe the next user in that device is another (public device). I also remove the check if(!isset($_GET['updated'])) condition.
<script>
jQuery(document).ready(function($) {
if (!sessionStorage.getItem('updated')) {
jQuery("#yith-ywraq-form").submit();
sessionStorage.setItem('updated', true);
}
});
</script>
Update: add live demo https://codepen.io/tuandaodev/pen/jOwvrRg?editors=1111
You need to use a server side session variable to check (duplicate)
Answer
I have a problem. It is my full honor if anyone helps.
First, let me explain the workflow I want. My CMS is Wordpress. I have a webpage (views.php). In this page, I want to show a download button (id=” download-button”) just to users who has the role subscriber. In default, no one has the role subscriber. So, the button is hidden in default. When a user buys a specific product he gains the role subscriber. Now, suppose a user has opened views.php page as a tab in his browser. In this step, the button is hidden. After that, he opens another tab and buys that specific product and he gains the role subscriber. Now, if he refresh the view.php page, the download button is seen. But, I want the user to see the download button without refreshing the page. In this regard, I wrote button.php file to be called in ajax. However, it does not work.
My codes:
html code (written in view.php which is the place of download button):
<div id="div1"></div>
my javascript code (which is put inside view.php file):
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("button.php");
});
});
</script>
my button.php code:
<?php
if (check_user_role(array('subscriber'))) {
echo ('<button id="download-button">Download</button>');
}
?>
I should note that I have written check_user_role php function in views.php.
It would be my honor if you help.
Thanks in advance.
Milad
As stated by smartdroid in one of the answers above, you can subscribe an event listener function to the window.onfocus event. Try following:
document.addEventListener("DOMContentLoaded", function (event) {
window.onfocus = function () {
$("#div1").load("button.php");
}
});
I highly recomment you to read further into javascript events.
For plain javascript:
https://www.w3schools.com/js/js_events.asp
For jQuery:
https://api.jquery.com/category/events/
Hey you have to use Window setInterval() Method, what this method does it will fire in background at your time interval set.
You can call your ajax code to set/show your button
setInterval(function(){
$("#div1").load("button.php");
}, 3000);
Make sure once you do add this button put return false so it wont execute again and again not to increase load on webpage.
$(document).ready event runs only once after the DOM is loaded. So this event will not fire unless page is reloaded.
If the user is buying a subscription in another browser tab and then returns to the original tab, windows.onfocus event will fire.
So you can use window.onfocus event to check for subscription every time view.php tab becomes active, and then show the button when necessary. So you can use something like the following, in your view.php
$(document).ready(function(){
window.onfocus = function () {
$("#div1").load("button.php");
}
});
Add an iframe to your view.php that doesn't need to contain anyting nor be visible.
<iframe name="download" id="if_download" src="blank.html"></iframe>
Target the download-action to the iframe. With some JS:
function download(href) {
window.frames['download'].location = 'download.php?file=' + href;
return false;
}
You may need to wrap the download-action through a php-file to modify its header
download.php:
$file_name = $_GET['file'];
//validate file exists and *remove any ../ - simple:
if (strpos($file_name, '/') !== false) die('yeah right..');
header("Content-Disposition: attachment;filename=\"$file_name\"");
echo file_get_contents($file_name);
die();
This function works on loading an external page inside of a div that passes a $_POST variable and click works on the first record on loading the same external page with $_POST variable from query.
$(document).ready(function(){
// load index page when the page loads
$("#form").load("tm-reserves-form.php");
$("#record").click(function(){
// load home page on click
var ContactID = document.getElementById('ContactID').value;
$("#form").load("tm-reserves-form.php", {"ContactID": ContactID});
});
});
What is not functioning is this seems to only be passing the first record variable and no others. I can click on the first record and the external page with data loads. I click on other records and the div does not load with new data from click. I have a page that creates a customer list from a mysql query, inside the record loop is a hidden input field with the primary key. Any thoughts on what I am missing? I thought maybe to unbind click, but that did not work either.
HTML Code
<div id="record"><?php do { ?><input type="hidden" name="ContactID" id="ContactID" value="<?php echo $row_tm_reserves['ContactID']; ?>"><!-- Other strings --><?php } while ($row_tm_reserves = mysql_fetch_assoc($tm_reserves)); ?></div>
Help! and Thanks in advance!
The problem is that #form content gets replaced on every load.
My solution:
You can create a new element inside #form, for example a <p>, and append to it, i.e. :
$(document).ready(function(){
$("#form").load("tm-reserves-form.php");
$(document).on("click","#record",function(){
var ContactID = document.getElementById('ContactID').value;
$("#form").append($(document.createElement("p")).load("tm-reserves-form.php", {"ContactID": ContactID}));
});
});
This is the problem with event delegation, change
$("#record").click(function(){
});
to
$(document).on("click","#record",function(){
});
Mainly make sure you have only one id as record in your page.
You should post your html code, to help us help you...
if new dom elements are added, the script that attaches actions on them will not work, because they didnt exist at the load of that script.. switch to :
$(document).on("action","element",function()
I have 3 php files: view.php, edit.php and edit2.php.
view.php is where I view the content of my database. I use edit.php to enter new data and it's a small window that in which I type in a content I want to include in my db.
Then, I pass all of the data to edit2.php which doesn't display anything but only has implemented MySQL queries.
Now I would like to have something like this:
I'm on view.php. I click a button, edit.php opens as a new, small window. I enter the data, click SUBMIT button, everything is sent to edit2.php but at the same time the window is closed and view.php is refreshed.
I would be thankful if you could help. Thanks in advance.
You can use the js function window.opener.location.reload().There is no need of ajax for refreshing the page..Simple js is enough.
You may use
header('Location: view.php');
at the end of your edit2.php
Add following code to your edit.php
<script>
window.onunload = refreshParent;
function refreshParent() {
window.opener.location.reload();
}
</script>
I have a problem. I have a page that when you click a button, a popup with a form is shown. So, I complete some data and I submit. What I want to do is, to submit the form, close the form and refresh the parent page. I don't want to do it with AJAX.
The problem is that in my parent page I have to refresh content with the input information of the form.
So when I refresh, sometimes the data is shown and sometimes not. Do you know why this could happen?
I just use onsubmit="refreshParent()" in my form. The info is stored always in my database, so I think the problem may be that sometimes the refresh catches the new info and sometimes not.
function refreshParent() {
window.opener.location.reload();
window.close();
}
I use this to reload the page that opened a popup window:
<script language="JavaScript">
<!--
function reloadParentPage() {
window.opener.location.href = window.opener.location.href;
if (window.opener.progressWindow) {
window.opener.progressWindow.close()
}
window.close();
}
//-->
</script>
By the way, the code above is called by a link or button in the popup page.
You have a race condition between the script doing the insert and the script reloading the parent.
The solution is to call refreshParent on the page after the submit - that way you know the data is in the database. You don't even have to do it on document ready - return a stub page that just defines and calls refreshParent in the head tag.
In PHP when you run post script, at the end, include this code :
echo '<html><script language="javascript">
parent.location.href="http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"].'"; // or any other url
</script></html>';
This will output a javascript that will reload the windows.