Jump to content

Variable Update


Ads

Recommended Posts

What I want to do is When you Click a link in an Iframe it Updates a Variable being displayed in the main page. here is the Code for the page in.

 

<?php
session_start();
include("include/db.php");
$username = $_SESSION['username'];
echo $username;
$energy = $row['energy'];
$newenergy =$energy-1;
$sql="update players set energy='$newenergy' where username='$username'";
$result= mysql_query($sql);
echo $newenergy;
?>
<html>
<head>
<title>.::Cyrus Lite::.</title>

        <link href="style.css" rel="stylesheet" type="text/css">

</head>
<body>

 

The Variable in the Main page is the $energy Variable also.

Link to comment
Share on other sites

so, let me get this straight, you want to click a link and have the sql requery?

 

once the php is done with a page the vars are scrubbed, so by the time your are clicking on the browser $energy variable is a forgotten memory to PHP.  ::).

 

Now if you want to click on a link and have something dynamic happen in parent document you will need to use AJAX

 

Link to comment
Share on other sites

as i said before. $energy is gone. php resides on the server and the link you click is in your local browser, there is no connection and no persistence of state, between server and browser. so now that we have that clear.  8)

 

Ok, to dynamically alter what the output that $energy is possible thru Javascript.

but remember that Javascript is local browsers and any changes that transpire will not reflect on server db, unless you use AJAX to send the update to the database.

 

so lets say you have a this

 

parent window

<span id="energy"><? echo $energy ?></span>

 

iframe

<a href="javascript:void();" onclick="window.parent.getElementById('energy').value = 10;">set parent frame energy displayed value</a>

 

note the when php ran the script it used $energy to place a value in parent output. you can see with the javascript I can only effect in the diplayed value span.energy object

 

hope this gives you an understanding of the distance between the browser and the php server  ;D

 

Link to comment
Share on other sites

ok

 

parent.php with iframe code

<?php 
ob_start();
session_start();
$energy = 10;
?>
<span id="energy"><? echo $energy ?></span>
<hr>
<IFRAME SRC="child.php" WIDTH=450 HEIGHT=100>
If you can see this, your browser doesn't 
understand IFRAME.  
</IFRAME>

 

child.php (contents of iframe

<?php 
ob_start();
session_start();
?>

<a href="javascript:void(0);" onclick="reduceValue();">reduce parent value</a>

<SCRIPT language="JavaScript">
<!-- Begin JavaScript
var energy = 10

function reduceValue()
{
energy--;	
parent.window.document.getElementById('energy').innerHTML = energy;

}

// -- End JavaScript -->
</SCRIPT>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.