Welcome to Dream.In.Code
Become an Expert!

Join 149,534 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,510 people online right now. Registration is fast and FREE... Join Now!




checkbox array validation - name="location" VS name="lo

 
Reply to this topicStart new topic

checkbox array validation - name="location" VS name="lo

bjorntheart
21 Jun, 2007 - 04:11 AM
Post #1

New D.I.C Head
*

Joined: 21 Jun, 2007
Posts: 1


My Contributions
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" (see pic of location.jpg) to see if the user has selected a location by clicking on one of the other tabs.
Attached Image

CODE TO DISPLAY THE CHECKBOXES
CODE

  $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

CODE

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[]")
CODE

  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).

CODE

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
User is offlineProfile CardPM
+Quote Post

ahmad_511
RE: Checkbox Array Validation - Name="location" VS Name="lo
21 Jun, 2007 - 08:42 PM
Post #2

D.I.C Regular
Group Icon

Joined: 28 Apr, 2007
Posts: 351



Thanked: 8 times
Dream Kudos: 400
My Contributions
Hi.
First of all "Yes ez_sql.php looks weird to me"
But if it works good !,that's enough.
Now.
I think the problem is in the way you echo "checkbox element"'s.
Javascript error will occurs when you set the name of checkbox to “location[]” // you can not define checkbox array this way
So. Remove the brackets from your echo and set the name to 'location'
CODE

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


Please let us know if you solved the problem
User is offlineProfile CardPM
+Quote Post

deepamanohar
RE: Checkbox Array Validation - Name="location" VS Name="lo
14 Mar, 2008 - 01:19 AM
Post #3

New D.I.C Head
Group Icon

Joined: 14 Mar, 2008
Posts: 9


Dream Kudos: 150
My Contributions
QUOTE

hi

To solve this array problem you have to just add an unique id to the locations and do a small javascript which i have place in the sample code

in your code you have to do as <input type='checkbox' id='location<? print $location->loc_id; ?>' name='location[]' value='<? print $location->loc_id;?>' /><? print $location->loc_descr; ?></td>


To be more clear i had Placed a sample code with the javascript to check the checkbox value
CODE

  <html>
<head>
<title>Checkbox</title>
<script type="text/javascript">
function CheckboxvalidateForm()
{
var locations = document.getElementsByName('location[]');
var TotalLocationChecked = 0;
for (var i = 0; i < locations.length; i++)
{
    if (locations[i].checked)
    {
        TotalLocationChecked = 1;
        break;
    }
}
if (TotalLocationChecked==0)
{
    alert("No Locations selected");
    locations[0].focus();
    return false;
}
return true;
}
</script>
<meta name="description" content="Validation for checkbox array">
</head>
<body>
<form action="checkbox.php" method="get" onsubmit="return CheckboxvalidateForm()">
<input id="location_1" name="location[]" type="checkbox" value="India">India<br>
<input id="location_2" name="location[]" type="checkbox" value="Japan">Japan<br>
<input id="location_3" name="location[]" type="checkbox" value="Italy">Italy</br>
<input type="submit" value="submit">
</form>
</body>
</html>


Once the validation is done submit the form to next page. You can get the array of posted checkbox values. smile.gif



User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 09:08PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month