calling javascript function once on body onload event - php

I am currently working on the project that have 2 php files category.php and product.php.
In category.php file i put a link shown below.
click here
I have the following js function called on the body onload in the product.php page.
<body onload="font_select(1760,'No',239)">
When users click on the link then they will be redirect to product.php file where the body onload function is called.this function is working fine.
My question is i need to call this function only once when the user enter in product.php page.currently this function is called on page reload also.
How would i call this function only once ? Thanx In Advance.

The most efficient way will be, use Cookies

(It's a little kludgy, but you could check document.referrer in font_select)
well that's wrong, turns out at least in firefox the referrer does not change apparently when the page is refreshed.
You may want to have a look at this question, where they handle detecting refresh using a cookie, however you should know that is really just a check if the page has been accessed before so it won't work unless you also clear that cookie on detecting a click on the link from your category.php page.

Go for javascript cookies
well to track whether the function is called or not, you could use javascript cookie. Like say you set a cookie with any name, say functioncalled with value 0 initially, when user goes to product page, check this cookie. If value is 0, then call a function then set the value of cookie to 1, next time when page is reloaded the function will not be called.

Related

Refresh only a div ("content") by universal button working on every page

To be clear - Ive already checked other Questions about refreshing div and the ideas I found were not exactly what I look for.
My site is made of plenty pages with the same header and footer (top, bottom, menu on both sides). I use smarty templates, and the Whole action of every page happens in one <div id="content">.
My users use to refresh most of those pages many times to do an action they've already done once again. With refreshing browser loads again header, footer, viewed page etc. I would like to bring them the button (instead of F5) which will refresh just a current content page (e.g. account.php) without refreshing whole site.
One of plenty structure:
<?php
$title = 'OneOfPlenty';
require_once("includes/head.php");
{
Whole action
}
require_once("includes/foot.php");
?>
header.tpl ends with <div id="content"> then comes
onofplenty.tpl and then in
footer.tpl I got </div> (close the content)
Here comes the question: Is it even possible? Am I able to create such a flexible button which will recognize which page is being displayed and will "know" to refresh just the content of this page?
Any ideas and help will be aprreciated.
Thank you
TTed
You could do an Ajax call with jQuery to get the output html of the tpl file of the page.
You could use an Ajax call, e.g. by using the jQuery get() function, e.g. like this
$.get("includes/account.php", function(data) {
$("#content").html(data);
alert("Load was performed.");
});
If you saved some kind of variable, either to session or to a data-content on your div. Just so you know which page you are on. Say if you are on account.php you set $('#content').attr("data-content", "account"). Then if you press the refresh button you could use an ajax get on $('#content').attr("data-content") + 'php' to re-import the data. Could be done with a SESSION variable as well.

How do I fully refresh the page in CodeIgniter?

I am loading a form in an overlay. The overlay has a separate controller and action to the page that invokes the overlay.
On form submit, if successful, I simply want to reload the referring page that the overlay was loaded from. I can get the referring page to load, but it places the content inside the overlay.
header("Location: www.example.com", true, 302);
does not work.
Using the URL helper like this:
$url = $_SERVER['HTTP_REFERER'];
redirect($url);
Also does not work. Every time it loads in the overlay. I am sad because of it.
You can use this code to refresh in codeigniter:
redirect($_SERVER['REQUEST_URI'], 'refresh');
it should work!
This one is more simple
redirect($this->uri->uri_string());
Unfortunately, using header to refresh/redirect will only reflect the changes in the container that is displaying the PHP page.
To refresh the parent page (i.e. the page that is displaying the overlay itself), you will need to do it on the client-side using Javascript. These questions should help you get on the right path:
Redirect parent window from an iframe action
PHP "header (location)" inside IFRAME, to load in _top location?
You can use this code to refresh in codeigniter:
redirect($_SERVER['REQUEST_URI'], 'refresh');
Hope this helps.
You do not have to supply additional two arguments for header. Just use this pattern:
header('Location: www.somewhere.com');
Also, check out this article about a similar matter:
Redirect with CodeIgniter
redirect(site_url(strtolower(__CLASS__)));
It's just
redirect('/some_url', 'refresh');
First load under constructor method like as:
function __construct()
{
parent::__construct();
$this->load->library('user_agent');
}
After inserting or update add this method it will work fine.
redirect($this->agent->referrer());
This one is more simple for everone :)
location.reload()

Is it possible to automatically call a JS function after use header php function?

Is it possible to automatically call a JS function (or do any statement) after use header php function (or click on any link) to redirect location ? And how to do that? Thanks you so much.
If the PHP file which holds properly used header function redirect has any other HTML code, this HTML code will not be interpreted as it will never reach the browser. This is, by definition how header works and your PHP page isn't even supposed to send any output before header is called by definition of the function.
One way to call a JS function that should ONLY execute if the visitor came to the page with this header redirect, would be to add a parameter to the page and on the destination page detect if the parameter is present.
For example. If the www.example1.com/original.php is the page that redirects to www.example2.com/redirect.php by using header, you could have header redirect to http://www.example2.com/redirect.php?redirected. In redirect.php you check if redirected parameter exists and if so you define a JS function and make it do what you want.
Is it possible to automatically call a JS function (or do any statement) after use header php function to redirect location ?
Only by inserting a <script> element in the page being redirected to.
(or click on any link) to redirect location ?
You can bind an event handler to all the links on a given page. That will run when they are clicked (but before the browser leaves the page).
That doesn't make much sense. You're either redirecting or are outputting an HTML page with Javascript in it. Can't do both at the same time. You can output an HTML page with Javascript in it after the redirect has finished, i.e. on the next page you're redirecting to.
In php you can do:
header("Location: http://mydomain.com/test.php");
Or print some JS to the page:
print "<script type='text/javascript'>window.location = 'test.php';</script>";

Altering the back behaviour in the browser PHP

In my PHP site, when a user adds something to their cart I trigger a URL change to make some functions and triggers on the page:
Original URL
/category/product/10
New URL
/category/product/10#addtocartbutton
URL becomes
/category/product/10?x=1
The method:
/category/product/10
/category/product/10#addtocartbutton (user clicks an onclick href to submit form)
/category/product/10?x=1 (the form adds the item to the session and appends this URL querystring)
The problem is when they press the bac b
In Javascript, using location.replace instead of location.href = 'new location'
Take a look at: http://www.roseindia.net/javascript/javascript-location-replace.shtml
-> location.replace will replace the current address by the new one -> when user click back button -> error will not happen.
Or try something with pushState. You should be able to manipulate you're history according to your needs.
A good idea is to make the add function redirect you back to the product page without showing the query-string. Since I dont know how you wrote your add controller it's hard to give you an exact code example...
Write the PHP controller for adding something like:
function add_to_cart()
{
add $_GET['x'] to session;
redirect back to product page;
}

Calling a php function with the help of a button or a click

I have created a class named as "member" and inside the class I have a function named update(). Now I want to call this function when the user clicks 'UPDATE' button.
I know I can do this by simply creating an "UPDATE.php" file.
MY QUESTION IS :-
Can I call the "update() function" directly without creating an extra file? I have already created an object of the same class. I just want to call the update function when the user clicks on update button.
an action.php example:
<?
if (isset($_GET[update])){
$id=new myClass;
$id::update($params);
exit;}
//rest of your code here
?>
Your button is in your view. Your method is in your class . You need a controller sitting in the middle routing the requests. Whether you use 1 file or many files for your requests is up to you. But you'll need a PHP file sitting in the middle to capture the button click (a POST) and then call the method.
As far as I know you can't do this without reloading your page, checking if some set parameters exist which indicate the button is clicked and than execute the button. If this is what you are looking for... yes it is possible on page reload. No as far as I know it is not possible directly because your php-function has to parse the results again.
Remember that a Model-View-Controller way is better and that this will allow you to ajax (or regular) requests to the controller-class.
You do it on the same page and have an if statement which checks for the button submission (not completely event driven) like so
if (isset($_POST['btn_update']))
{
update();
}
<input type="submit" name="btn_update" value="Update" />
That will have to be wrapped in a form.
Or you could do it with AJAX so that a full page refresh isn't necessary. Check out the jQuery website for more details.

Categories