Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to give listbox values to the function

Status
Not open for further replies.

JackTheRussel

Programmer
Aug 22, 2006
110
FI
Hi.

I have multiselect listbox on my web-page.

Now I try to give selected listbox values to the javascript function (GetValues) when button is pushed.

I found two function on web, which print values to the alert-box when button is pushed (getSelected and outputSelected)

I changed alert-function to GetValues function, which call php-script (test.php) which print values to the screen.

Now when I push the button test.php always print 0 to the screen. I don't know how I can get this work.

Here is my functions
Code:
function getSelected(opt) {
            var selected = new Array();
            var index = 0;
            for (var intLoop = 0; intLoop < opt.length; intLoop++) {
               if ((opt[intLoop].selected) ||
                   (opt[intLoop].checked)) {
                  index = selected.length;
                  selected[index] = new Object;
                  selected[index].value = opt[intLoop].value;
                  selected[index].index = intLoop;
               }
            }
            return selected;
         }

         function outputSelected(opt) {
            var sel = getSelected(opt);
            var strSel = "";
            for (var item in sel)      
               strSel += sel[item].value + "\n";
            Hae(strSel);
         }


    function GetValues(strSel) {        
        var strURL="test.php?Res="+strSel;
        var req = getXMLHTTP();
    
        if (req) {
            
            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {                        
                        document.getElementById('testdiv').innerHTML=req.responseText;                        
                    } else {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }                
            }            
            req.open("GET", strURL, true);
            req.send(null);
        }
                
    }

And here are test.php
Code:
<?php

$Res=intval($_GET['strSel']);

echo "<h1>$Res</h1>"; //PRINTS ALWAYS 0 TO SCREEN

?>

And here is listbox and button code
Code:
<SELECT NAME="multistore" SIZE=3 MULTIPLE>
            <OPTION VALUE="Computer" SELECTED>Computer</OPTION>
            <OPTION VALUE="Bookstore">Book Store</OPTION>
            <OPTION VALUE="MailOrder" SELECTED>Mail Order</OPTION>
         </SELECT>
         <INPUT TYPE=BUTTON VALUE="Selected List Items"
            ONCLICK="outputSelected(this.form.multistore.options)">

If you have better solution I am more than glad to hear it.

The main purpose is to get values to the php-script.
Not to print values to the screen.

Help are needed.
 
It sounds like the page that Ajax is calling is being cached, so it is always returning the same output. Make the following change to ensure that the page is not cached:
Code:
    function GetValues(strSel) {
        [!]var now = new Date();[/!]
        var strURL="test.php?Res=" + strSel[!] + "&antiCache=" + now.getTime();[/!]
        var req = getXMLHTTP();
    
        if (req) {
            
            req.onreadystatechange = function() {
                if (req.readyState == 4) {
                    // only if "OK"
                    if (req.status == 200) {                        
                        document.getElementById('testdiv').innerHTML=req.responseText;                        
                    } else {
                        alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                    }
                }                
            }            
            req.open("GET", strURL, true);
            req.send(null);
        }
                
    }

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
[1] This is the major error of the whole setup. It is first and above all in the php page.
[1.1] The querystring value is named "Res", _not_ "strSel".
[1.2] intval(): you are trying to get some integer. What for? the strSel value so passed is a string of selected option values (separated by \n which is not very html significant, but that is another minor thing.)
[1.3] Combining [1.1] and [1.2], change this line.

>$Res=intval($_GET['strSel']);
[tt]$Res=$_GET['Res'];[/tt]

[2] Now the client-side script.
[2.1] What is Hae? I suppose it is some other function calling GetValues(). But since it is not shown, I can't be certain.
[2.2] In any case, when you pass strSel, you have to escape \n which is not safe. Hence. at least it should be this.
>Hae(strSel);
[tt]Hae(escape(strSel));[/tt]
[2.3] In any case, \n is not showing up well in html innerHTML. But you can revise it later.
[2.4] getXMLHTTP() is not shown. I suppose it is correct and it should something boring that everybody copying everybody else somehow.
[2.5] Based on your saying that you at least seeing "0" printed, implementing the above change, you should get the functionality in the large correct. The rest is detail.
 
Thanks for your valuable information!

I'm terrible sorry, that there is Function called Hae. It should be GetValues.

I will fix all these errors that you showed to me.

Thanks !
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top