Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Posting multiple checkbox values to <input type=text>

    print "<Font face=\"calibri, Arial\"><table id=\"rnatable\" border=2px width=100%>";
    print "<th></th>";
    print "<th>a</th>";
    print "<th>b</th>";
    print "<th>c</th>";
    print "<th>d</th>";
    print "<th>e</th>";
    print "<th>f</th>";
    print "<th>g</th>";

    while($array = mysql_fetch_array($sql_query))
    {
    $id=$array['a'];
    print "<tr id=\"newtr\">";
    print "<td><input id=\"check\" type=\"checkbox\" name=\"keyword[]\" value=\"$id\" ></td>";
    print "<td>".$array['a']."</td>";
    print "<td>".$array['b']."</td>";
    print "<td>".$array['c']."</td>";
    print "<td>".$array['d']."</td>";
    print "<td>".$array['e']."</td>";
    print "<td>".$array['f']."</td>";

    print "<td>
    <a href=\"http://localhost/rnasearch/retrieve.php?a=$id\">bla</a>
    </td></tr></font>";

    }

    print "</table>";
    }

    this is part of a php searchengine script that i wrote to retrieve data from a database and as you can see the last column that is g contains a link which when clicked takes the user to the second php script which retrieves additional information of that entry, but this is done for only single entries so as you can see i introduced checkboxes so that the user can check any number of checkboxes and retrieve information as per their wish

    now to do this i created a <input type=text where i want the value of these checkboxes i.e. $id to be posted in a delimited format so that the user can then click the corresponding button and retrieve the information this is the code

    <div id="floatMenu">
    <ul class="menu1">
    <center><li><form name="senddata" method="POST" action="http://localhost/retrieve.php" style="display:inline;"><input id="fasta" type="text" class="multitext"><input name="Fasta" type="Submit" value="Retrieve Fasta"></form>

    this is a css floating menu....so far so good....everything went fine....
    after this i had to write a javascript to do this and i've been stuck there searching forums for the last 4 days!

    this is the javascript i found from somewhere which came close to doing wht i wanted it to do

    window.onload = function () {
    var cb = document.getElementById('check');
    var fasta = document.getElementById('fasta');
    cb.onclick = function () {fasta.value = cb.value + ",";
    };};

    this script only sends the value of the first checkbox in the table, the others are not found by it, mind you its not the first checkbox selected its the first checkbox that is present in the table

    how can i resolve this problem so that even if i have n number of checkboxes in my table, if the user chooses to do so they can retrieve n number of information....please help i've almost lost hope in this!

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You need to pass all the checkboxes to the server and then build the extra code onto the query there. There's no point doing anything with JavaScript as anything that you do in JavaScript would just replicate what you need server side for it to work for those with JavaScript turned off. JavaScript can only provide enhanced functioning for those with it enabled and in this situation there is nothing to enhance.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,620
    Thanks
    78
    Thanked 4,388 Times in 4,353 Posts
    It's actually much easier to do this in the server in PHP code, anyway.

    You just check to see if you got any values in $_GET["keyword"] and, if so, just convert all the values into an IN( ) list for your SQL query.

    I don't use PHP, but I *believe* it would be something like this:
    Code:
    if ( isset($_GET["keyword"]) )
    {
        $list = "";
        for ( $i = 0; $i < count( $_GET["keyword"] ); ++$i )
        {
            if ( $i > 0 ) $list .= ",";
            $list .= "'" + mysql_real_esape_string($_GET["keyword"][$i]) + "'";
        }
        $sql = "SELECT * FROM table WHERE somefield IN (" . $list . ")";
        ...
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm actually a newbie, its been hardly 15 days i have been working on this, so the magnanimity of the problem is greater for me as i still haven't been have able to successfully construct url strings considering n number of possible combinations....i will incorporate a non javascript method in the future but right now javascripting seems the easiest way to go

  • #5
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    It's actually much easier to do this in the server in PHP code, anyway.

    You just check to see if you got any values in $_GET["keyword"] and, if so, just convert all the values into an IN( ) list for your SQL query.

    I don't use PHP, but I *believe* it would be something like this:
    Code:
    if ( isset($_GET["keyword"]) )
    {
        $list = "";
        for ( $i = 0; $i < count( $_GET["keyword"] ); ++$i )
        {
            if ( $i > 0 ) $list .= ",";
            $list .= "'" + mysql_real_esape_string($_GET["keyword"][$i]) + "'";
        }
        $sql = "SELECT * FROM table WHERE somefield IN (" . $list . ")";
        ...
    }
    i thought a form can only send its own value and not that of other forms....i'll incorporate a form and try this out and let you know

  • #6
    New to the CF scene
    Join Date
    May 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kipal View Post
    i thought a form can only send its own value and not that of other forms....i'll incorporate a form and try this out and let you know
    I tried this method but as i thought, a different form is unable to send the data of another form...i cannot as well incorporate buttons into this form as that will make the whole thing very messy and the idea redundant.....but if a javascript solution to this persistent problem is available it would be really helpful....


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •