JavaScript

Moderators: None (Apply to moderate this forum)
Number of threads: 2059
Number of posts: 5157

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Checkbox array validation - location[] Posted by bjorn_theart on 22 Jun 2007 at 1:08 AM
hi guys

I'm currently working on a project where registered uers can add a buyer or a seller specifications

On my buyerspec page I validate(with javascript) the contents of "Location"[url]http://www.lifestyleintroductions.co.za/prop/test/pic of location.jpg[/url] to see if the user has selected a location
by clicking on one of the other tabs.

CODE TO DISPLAY THE CHECKBOXES
  $sql = "select * from jos_li_location where loc_id <= '3' and active = '1' order by loc_id";
  $locations = $db->get_results($sql);
  foreach($locations as $location) {
    echo "<tr>";
    echo "<td width='5'></td>";
    echo "<td><input type='checkbox' name='location[]' value='$location->loc_id' />
$location->loc_descr</td>";
    echo "</tr>";
  }


If the validation is successfull, the user can click on the next tab and complete the rest of the form
On the form submission the values entered is posted to an action page members/buyer/addspecb.php and inserted into a mysql db
(if db queries look a bit strange, it's because I'm using ez_sql.php)
To add the location(s) (can add more than one location per buyer spec)
i call the following function InsertLocations($specid, $locations, $db) which receives and array of location values from the form

function InsertLocations($specid, $locations, $db) {

  if($locations) {
    foreach ($locations as $key => $value) {
      $sql = "insert into jos_li_locationlink(loc_id, spec_id) ".
             "values ('$value', '$specid')";
      $db->query($sql);
    }
  }
}


My problem is with the checkbox name (name="location[]")
  echo "<td><input type='checkbox' name='location[]' value='$location->loc_id' />
$location->loc_descr</td>";


My javascript validation(below) wich is called when the user clicks on the next tab wich toggles the various tabs to display or not,
likes the checkbox name="location", but not name="location[]". With the checkbox name attribute set to name="location" only the last selected checkbox value
gets passed to function InsertLocations($specid, $locations, $db).
If the checkbox attribute is set to name="location[]", then I get javascript erros, but without the validation check the values of the location[] checkbox
array is inserted perfectly in the db via function InsertLocations($specid, $locations, $db).

function countLocationb(form) {

  var total=0;
  frm = document.frmAddBuyerSpec;
  for(var i=0; i < frm.location.length; i++){
    if(frm.location[i].checked) {
      total++;
    }
  }
  if( total == 0 ) {
    return false;
  } else {
    return true;
  }
}


if false then user must enter at least one location

Is there some sort of workaround for this

I've been beating myself up for the last couple of days trying to workaround this, but nnnooooo solution yet

Thanx in advance for the help guys
Attachment: pic of location.jpg (18882 Bytes | downloaded 323 times)
Report
Re: Here's your problem Posted by CyGuy on 23 Jun 2007 at 10:37 PM
: hi guys
:
: I'm currently working on a project where registered uers can add a
: buyer or a seller specifications
:
: On my buyerspec page I validate(with javascript) the contents of
: "Location" http://www.lifestyleintroductions.co.za/prop/test/pic
: of location.jpg

Hey bjorn,

Every checkbox in the frmAddBuyerSpec HTML form has the name of "location[]" and that is why (when you remove the brackets) you get only the last checkbox. Besides [] aren't valid $tring data in js.

Your solution:
replace the validation script with this...
  $sql = "select * from jos_li_location where loc_id <= '3' and active = '1' order by loc_id";
  $locations = $db->get_results($sql);
  foreach($locations as $location) {
    echo "<tr>";
    echo "<td width='5'></td>";
    echo "<td><input type='checkbox' name='$location->loc_name' value='$location->loc_id' />
$location->loc_descr</td>";
    echo "</tr>";
  }
?>


Now you'll need another set in the database for the doc_name that should be unique, fairly short, and of type string, so you can name the elements.

function countLocationb(form) {
  
  var local=new Array();
<?
  $i=0;
  $sql = "select * from jos_li_location where loc_id <= '3' and active = '1' order by loc_id";
  $locations = $db->get_results($sql);
  foreach($locations as $location) {
    echo "local[{$i}]=\"{$location->loc_id}\"\;\n"; //not tested, but you get it
  $i++;
  }
?>
  var total=0;
  for(var i=0; i < local.length; i++){
    if(getElementByName(location[i]).checked) {
      total++;
    }
  }
  if( total == 0 ) {
    return false;
  } else {
    return true;
  }
}


Assuming the data doesn't change between calls it might work. You could load a couple of arrays and have js serve the page. There are other ways to do this, but this should point out where your problems are.

In the commented lines I went ahead and isolated the PHP variables in the string with the {}brackets and escaped the ;semicolon. Not sure about them though, becaus i am not a PHP guru. Play around with the HTML source to find the desired results wich would be a unique name.

Wish ya luck,

WHAT you;see_is_what=you.get;
-Russ aka DangeRuss
Report
Re: Checkbox array validation - location[] Posted by bjorn_theart on 25 Jun 2007 at 3:04 AM
Hey Russ

uh?????

Thanx for the reply, but I'm afraid you gonna have to put that in simpler english for. Not a code guru like you yet. Hoping to get there soon though.

Got a bit confused as to really what and where to put code and update database.

P.S Please bear with me

Cheers


Report
Re: Checkbox array validation - location[] Posted by CyGuy on 25 Jun 2007 at 3:45 AM
: Hey Russ
:
: uh?????
:
: Thanx for the reply, but I'm afraid you gonna have to put that in
: simpler english for. Not a code guru like you yet. Hoping to get
: there soon though.
:
: Got a bit confused as to really what and where to put code and
: update database.
:
: P.S Please bear with me
:
: Cheers
:
:
:

In the line where you create the checkboxes, every checkbox is named location or at least that is what the name attribute is given. That should explain why you can only retrieve the last checkbox at best.

There is no easy fix. The name= attribute should probably be replaced by the id= attribute for best results or entirely replaced all togethar.

The HTML DOM has no checkbox array. AND it will most certainly NOT suppurt a user defined array. but javascript will allow you to access certain elements in the HTML DOM by array like links images applet and forms.

If you modify each checkbox to be enclosed in a form tag you could then use the array method like so:

Repace this:
  echo "<td><input type='checkbox' name='location[]' value='$location->loc_id' />
$location->loc_descr</td>";

with this
  echo "<td><form><input type='checkbox' value='$location->loc_id' />
$location->loc_descr</form></td>";

Likewise; replace this:
 for(var i=0; i < frm.location.length; i++){
    if(frm.location[i].checked) {

with this
 for(var i=0; i < document.forms[0].forms.length; i++){
    if(document.forms[0].forms[i].checked) {

That assumes that the first form in your page contains that clunky table with all the other little checkbox forms. There's another way to parse the HTML DOM, but it's far too much to show here. I guess this is a little simpler than previously described and follows your original scheme a bit better. I hope this answers your question
Report
Re: Checkbox array validation - location[] Posted by nareshap on 26 Aug 2008 at 4:37 AM
Hi,

The method above said works well for javascript validation but we can't get those check boxes' values into $_POST or $_GET arrays in PHP. But with the following code I can validate check boxes and get data too into $_POST unless there are more than one check boxes.

<html>
<head>
<script language="JavaScript">
function check()
{
	var a=document.some_form['graduate[]'];
	alert("Length:"+a.length);
	var p=0;
	for(i=0;i<a.length;i++){
		if(a[i].checked){
			alert(a[i].value);
			p=1;
		}
	}
	if (p==0){
		alert('please select at least one check box');
		return false;
	}
			
	document.some_form.submitted.value='yes';
	return true;
}
</script>
</head>
<body>
<form name="some_form" onsubmit="return check();" method="POST" action="">
<table>
<tr><td>Post Graduate in</td><td><input type="checkbox" name="graduate[]" value="history">History
<!-- plz comment the following three input checkboxes and run the program again -->
<input type="checkbox" name="graduate[]" value="telugu">Telugu
<input type="checkbox" name="graduate[]" value="Computer sceince">Computer Science
<input type="checkbox" name="graduate[]" value="Mathematics">Mathematics

</td></tr>
<tr><td><input type="submit" value="Submit"></td></tr>
<input type="hidden" name="submitted">
</form>
</body>
</html>
<?php
if ($_POST[submitted])
{
$pg=$_POST[graduate];
echo "<pre>";
print_r(implode(",",$pg));
echo "</pre>";

}
?>

Report
Re: Checkbox array validation - location[] Posted by nareshap on 27 Aug 2008 at 12:19 AM
Hoorayyyyyyyyy!!

I find the solution...


<html>
<head>
<script language="JavaScript">
function check()
{
	var a=new Array();
	a=document.getElementsByName("graduate[]");
	alert("Length:"+a.length);
	var p=0;
	for(i=0;i<a.length;i++){
		if(a[i].checked){
			alert(a[i].value);
			p=1;
		}
	}
	if (p==0){
		alert('please select at least one check box');
		return false;
	}
			
	document.some_form.submitted.value='yes';
	return true;
}
</script>
</head>
<body>
<form name="some_form" onsubmit="return check();" method="POST" action="">
<table>
<tr><td>Post Graduate in</td><td><input type="checkbox" name="graduate[]" value="history">History

<!--<input type="checkbox" name="graduate[]" value="telugu">Telugu
<input type="checkbox" name="graduate[]" value="Computer sceince">Computer Science
<input type="checkbox" name="graduate[]" value="Mathematics">Mathematics -->

</td></tr>
<tr><td><input type="submit" value="Submit"></td></tr>
<input type="hidden" name="submitted">
</form>
</body>
</html>
<?php
if ($_POST[submitted])
{
$pg=$_POST[graduate];
echo "<pre>";
print_r(implode(",",$pg));
echo "</pre>";

}
?>

Report
Re: Checkbox array validation - location[] Posted by jeffery2k2610 on 21 Aug 2009 at 2:06 AM
hai GUYS,

THIS WAS A GREAT STUFF....

I TOO WAS STUCK WITH VALIDATING CHECKBOXES WITH ARRAYNAMES

THE SOLUTION HERE IS COOL AND WORKED RIGHT FOR ME......

THANZ A LOT GUYS.....

WE ROCK................



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.