Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Checkbox array validation - location[]

bjorn_theartbjorn_theart Posts: 2Member
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
[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 "";
echo "";
echo "loc_id' />
$location->loc_descr";
echo "";
}
[/code]

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);
}
}
}
[/code]

My problem is with the checkbox name (name="location[]")
[code]
echo "loc_id' />
$location->loc_descr";
[/code]

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;
}
}
[/code]

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

Comments

  • CyGuyCyGuy Posts: 312Member
    : 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...
    [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 "";
    echo "";
    echo "loc_name' value='$location->loc_id' />
    $location->loc_descr";
    echo "";
    }
    ?>
    [/code]

    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.

    [code]
    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}";
    "; //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;
    }
    }
    [/code]

    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,

    [green]WHAT you;see_is_what=you.get;[/green]
    -Russ aka DangeRuss
  • bjorn_theartbjorn_theart Posts: 2Member
    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


  • CyGuyCyGuy Posts: 312Member
    : 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:
    [code] echo "loc_id' />
    $location->loc_descr";
    [/code]
    with this
    [code] echo "loc_id' />
    $location->loc_descr";
    [/code]
    Likewise; replace this:
    [code] for(var i=0; i < frm.location.length; i++){
    if(frm.location[i].checked) {
    [/code]
    with this
    [code] for(var i=0; i < document.forms[0].forms.length; i++){
    if(document.forms[0].forms[i].checked) {
    [/code]
    That assumes that the [u]first[/u] 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
  • nareshapnareshap Posts: 2Member
    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.

    [code]



    function check()
    {
    var a=document.some_form['graduate[]'];
    alert("Length:"+a.length);
    var p=0;
    for(i=0;i




    Post Graduate inHistory

    Telugu
    Computer Science
    Mathematics







    <?php
    if ($_POST[submitted])
    {
    $pg=$_POST[graduate];
    echo "<pre>";
    print_r(implode(",",$pg));
    echo "";

    }
    ?>
    [/code]
  • nareshapnareshap Posts: 2Member
    Hoorayyyyyyyyy!!

    I find the solution...

    [code]




    function check()
    {
    var a=new Array();
    a=document.getElementsByName("graduate[]");
    alert("Length:"+a.length);
    var p=0;
    for(i=0;i




    Post Graduate inHistory









    <?php
    if ($_POST[submitted])
    {
    $pg=$_POST[graduate];
    echo "<pre>";
    print_r(implode(",",$pg));
    echo "";

    }
    ?>
    [/code]
  • jeffery2k2610jeffery2k2610 Posts: 1Member
    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................
Sign In or Register to comment.