Question : output is repeated

this code works

but

$_SESSION[sessionVar]
is written 2x


I can hide this by making iframe small height
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
----------------
mainpage.php
----------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript">
  function outputText(o) {
    document.getElementById("outputTextHidden").src = "saveToSession2010.php?sessionVar=" + o.options[o.selectedIndex].value;
  }
</script>
</head>
<body>
<select onchange="outputText(this);">
<option value="">Choose one...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br />
<iframe id="outputTextHidden" width="320" height="200" />
</body>
</html>
1:
2:
3:
4:
5:
6:
7:
8:
9:
---------------------------
saveToSession2010.php
---------------------------
<?PHP
   session_start();
   // now we can save sessionVar in session var 
   $_SESSION["sessionVar"] = $_GET["sessionVar"];
   echo $_SESSION["sessionVar"];
?>

Answer : output is repeated

Can you tell us from a "high-level" view what you're trying to achieve here?  It almost looks to me like you would be much better off using AJAX (perhaps jQuery) if you want to hide and show parts of a web page.  You mainuplate the DOM instead of the iFrame.  There are a lot of not-so-nice things about iFrames, enough so that I would prefer to avoid them when I can do so.

I am not sure of the source of the writing in this code snippet - I think it may be Rasmus Lerdorf of PHP fame.  It shows how to do an AJAX call, and that understanding may be helpful to you.

Best regards (and please tell us a little more about what you're trying to accomplish), ~Ray
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
I find a lot of this AJAX stuff a bit of a hype.  Lots of people have
been using similar things long before it became "AJAX".  And it really
isn't as complicated as a lot of people make it out to be.  Here is a
simple example from one of my apps.  

/* THE JAVASCRIPT TO CREATE THE AJAX INFRASTRUCTURE */
/* FUNCTION TO CREATE THE BROWSER-DEPENDENT REQUEST OBJECT */
function createRequestObject() 
{
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer")
    {
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else
    {
        ro = new XMLHttpRequest();
    }
    return ro;
}


/* FUNCTION TO SEND THE REQUEST */
function sendReq(action) 
{
    http.open('get', 'ajaxprocessor.php?action='+action);
    http.onreadystatechange = handleResponse;
    http.send(null);
}


/* FUNCTION TO HANDLE THE RESPONSE */
function handleResponse()
{
    if(http.readyState == 4)
    {
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1))
        {
            update = response.split('|');
            document.getElementById(update[0]).innerHTML = update[1];
        }
    }
}


/* CREATE THE OBJECT */
var http = createRequestObject();

/* END OF THE JAVASCRIPT */

This creates a request object along with a send request and handle
response function.  So to actually use it, you could include this js in
your page.  Then to make one of these backend requests you would tie it
to something.  Like an onclick event or a straight href like this:

<a href="javascript:sendReq('foo')">[Foo]</a>

That means that when someone clicks on that link what actually happens
is that a backend request to ajaxprocessor.php?action=foo will be sent.

In ajaxprocessor.php you might have something like this:

/* THE PHP TO HANDLE THE REQUEST FROM THE FRONT-END SCRIPT */
switch($_GET['action']) 
{
    case 'foo':
      // DO WHATEVER PROCESSING IS APPROPRIATE
      // THEN RETURN A RESPONSE STRING TO handleResponse()
      echo "foo|Foo done";
      break;
      // ETC...
}

Now, look at handleResponse.  It parses the "foo|Foo done" string and
splits it on the '|' and uses whatever is before the '|' as the dom
element id in your page and the part after as the new innerHTML of that
element.  That means if you have a div tag like this in your page:

<div id="foo">Waiting for Foo</div>

Once you click on that link, that will dynamically be changed to:

<div id="foo">Foo done</div>

That's all there is to it.  Everything else is just building on top of
this.  Replacing my simple response "id|text" syntax with a richer XML
format and making the request much more complicated as well.  Before you
blindly install large "AJAX" libraries, have a go at rolling your own
functionality so you know exactly how it works and you only make it as
complicated as you need.  Often you do not need much more than what I
have shown here.

Expanding this approach a bit to send multiple parameters in the
request, for example, would be really simple.  Something like:

function sendReqArg(action,arg) 
{
    http.open('get', 'ajaxprocessor.php?action='+action+'&arg='+arg);
    http.onreadystatechange = handleResponse;
    http.send(null);
}

And your handleResponse can easily be expanded to do more interesting 
things than just replacing the contents of a div.

-Rasmus (Lerdorf?)
Random Solutions  
 
programming4us programming4us