Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Changes to list box using JS not sticking when submiting form

Changes to list box using JS not sticking when submiting form
Thread Tools
timmerk
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Oct 9, 2003, 03:48 AM
 
As some of you may have seen in my other posts, I had needed code that adds IPs to a listbox in real time. However, even though I have that working now, when I go and submit the form, the listbox with the added values from JS are not sent though the POST variables. Any idea how to get the dynamically added items in the listbox to get sent with the other variables?

Thanks!
     
mzllr
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Oct 9, 2003, 04:48 AM
 
here's the whole thing...

[php]<html>

<head>

<script language="JavaScript" type="text/javascript">
function removeIP(v)
{
// remove IP address from list
document.forms[0].ipList[v] = null;
}

function checkIP(v)
{
var check = 0;

// creates an array from the sections of an IP address
// 192.168.254.2 would be ipSub(192, 168, 254, 2)
var ipSub = v.split(".");

// checks to make sure a valid IP address is entered
for (i = 0; i < ipSub.length; ++i)
{
// checks to ensure that only numbers were entered for the IP address
for (j = 0; j < ipSub[i].length; ++j)
{
// verifies that each character is a number
var abc = parseFloat(ipSub[i].charAt(j));

// if something other than a number is found, an alert is posted
if (isNaN(abc)) { alert("Character " + ++j + " in section " + ++i + " is not a number."); ++check; }
}

// if an element of the array ipSub has more than 3 characters, an alert is posted
if (ipSub[i].length > 3) { alert("Invalid IP address, section " + ++i + " is too long."); ++check; }

// if an elembent of the array ipSub is greater than 255, an alert is posted
if (ipSub[i] > 255) { alert("Invalid IP address, section " + ++i + " is out of range."); ++check; }
}

// adds to list if valid
if (check == 0)
{
// increase the number of options by 1
++document.forms[0].ipList.length;

// add text to the new option
document.forms[0].ipList[(document.forms[0].ipList.length - 1)].text = v;
}
}

function submitIP(v)
{
// selects all values in list box
for (i = 0; i < v.length; ++i)
{
v[i].selected = "true";
}

// changes the name of the <select> tag to match an array for PHP processing
document.forms[0].ipList.name = "ipList[]";
}
</script>

</head>

<body>

<form name="ipForm" method="post" action="<? echo $PHP_SELF ?>">
<select name="ipList" size="10" multiple>
<option>192.168.254.2</option>
<option>192.168.254.3</option>
<option>192.168.254.4</option>
</select>
<a href="javascript:removeIP(document.forms[0].ipList.selectedIndex)">Remove</a>
<br>&nbsp;<br>
<input type="text" name="add_ip">
<a href="javascript:checkIP(document.forms[0].add_ip.value)">Add</a>
<br>
<input type="submit" name="submit" value="Submit" onClick="javascript:submitIP(document.forms[0].ipList);">
</form>

<?

if (!$_POST['submit'])

{
echo "form not submitted";
}

else

{
echo "<b>IP Addresses from list:</b> <br>";

for ($i=0; $i < count($ipList); $i++)
{
echo "$ipList[$i] <br>";
}
}

?>

</body>

</html>[/php]

see it here.
     
timmerk  (op)
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Oct 9, 2003, 12:03 PM
 
Thanks! Seems to work perfect!
     
timmerk  (op)
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Oct 10, 2003, 02:40 AM
 
delete. im doing something wrong.
( Last edited by timmerk; Oct 10, 2003 at 03:16 AM. )
     
timmerk  (op)
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Oct 10, 2003, 02:45 PM
 
I found out why it wasn't working for me - register globals needs to be turned on in PHP. Any ideas how to make it work without globals on? I am using the $_POST variable to access the ipList, so I don't know why it needs globals.

Any ideas?
     
varcos
Fresh-Faced Recruit
Join Date: Apr 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
Oct 10, 2003, 03:03 PM
 
Hey, I'm not sure what your problem is but you can always put this in your code to see what variables are being passed around.

print_r(get_defined_vars());

Hope that helps,

varcos
     
clam2000
Dedicated MacNNer
Join Date: Aug 2002
Status: Offline
Reply With Quote
Oct 10, 2003, 03:40 PM
 
Try the sample code with

'$ipList=$HTTP_POST_VARS['ipList'];'

just after the else.

this should make sure that the iplist variable has the array of ip's.

--will
     
timmerk  (op)
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Oct 10, 2003, 05:07 PM
 
Thanks, but I already am using the $_POST variable which does the same thing. But I'm going to keep looking around where I might have forgotten it.

On another subject - the above javascript code that selects all of the list doesn't work when I try using the code in my code - it just selects the last item in the listbox and therefore, submits just that.

I'll try to post my code later. Thanks
( Last edited by timmerk; Oct 11, 2003 at 12:37 AM. )
     
timmerk  (op)
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Oct 11, 2003, 12:38 AM
 
bump (i edited the post above, but it didn't put the thread on top)
     
mzllr
Registered User
Join Date: Jul 2003
Location: San Jose
Status: Offline
Reply With Quote
Oct 11, 2003, 01:24 AM
 
Originally posted by timmerk:
On another subject - the above javascript code that selects all of the list doesn't work when I try using the code in my code - it just selects the last item in the listbox and therefore, submits just that.
does your select tag allow for multiple selections?

[php]
<select name="ipList" size="10" multiple>
[/php]
     
timmerk  (op)
Mac Elite
Join Date: Jan 2001
Status: Offline
Reply With Quote
Oct 11, 2003, 01:30 AM
 
bingo! man, i got to give you credit with my program/page. is this ok?

thanks again for all your help!
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 09:05 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,