Will not setting location = 'home.php' on button click lead to website issues or be a savvy way to reduce bandwidth?

StackOverflow https://stackoverflow.com/questions/18908073

سؤال

I am working on a site in which the user (after logging in) is sent to the home page of the site (home.php). On this home page are 4 separate iframes, each within a separate div and each populated by a different php file (not home.php). The site is not finished but I am trying to build the site with bandwidth minimization in mind. In one of the containers is an iframe which is populated by users.php as follows:

<div id="SomeDiv">
<iframe name="frame3" width="1050" height="175" src="users.php" frameborder="0" ></iframe>
</div>

What I have currently done is put a status-setting button within users.php itself, which when pressed changes the user's 'status'....works great. After pressing the button, the location is re-set to users.php as follows:

if  (($_POST['hidden']) == 'On') {

        $login_id=mysql_real_escape_string($_SESSION['login_id']);

        $Update1=mysql_query("UPDATE login SET Status = 'On' WHERE   
login_id = '$login_id'");               
$message = 'You have set your status to: ON';               
echo "<SCRIPT>
alert ('$message');
location='users.php';
</SCRIPT>"; 

}

else if  (($_POST['hidden']) == 'Off') {

        $login_id=mysql_real_escape_string($_SESSION['login_id']);

        $Update2=mysql_query("UPDATE login SET status = 'off' WHERE    
login_id = '$login_id'");               
$message1 = 'You have set your status to: OFF';                 
echo "<SCRIPT>
alert ('$message1');
location='users.php';
</SCRIPT>"; 
}

I have intentionally coded this way instead of putting the button within 'home.php' and setting location='home.php' after the button is clicked; my reasoning was this was a quick and easy way to save on bandwidth (i.e., only the iframe reloads by setting the location = 'users.php', instead of the whole page reloading by setting location = 'home.php' by placing the button within the code of 'home.php'). As a beginner, this seems like a great bandwidth-reducing strategy, plus it's much faster (about half a second) than a whole page reload (about 1.5 seconds). As a beginner, are there any potential pitfalls of this method of which I am unaware?

هل كانت مفيدة؟

المحلول

Using iframes to optimize anything is counter-productive like @Dagon says. It takes a longer time to re-render the window on a full refresh than to load content in via AJAX

What you would actually want to do is have markup like this:

<div id="container">
  <button id="updatePane" value="Update" />

  <div id="changeMe">
    Some content on load
  </div>
</div>

You'd want different elements like changeMe to replace each iframe.

Then you can use AJAX, most likely with a library like jQuery, to load content into changeMe and other DIVs. This way you are going to your server, getting the least amount of information possible, without actually refreshing the window.

EDIT:

And also since I see you are using PHP you can actually do something like:

<div id="container">
  <button id="updatePane" value="Update" />

  <div id="users">
     <?php include("users.php"); ?>
  </div>
</div>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top