Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-12-2012, 06:08 AM   PM User | #1
kipal
New to the CF scene

 
Join Date: May 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
kipal is an unknown quantity at this point
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!
kipal is offline   Reply With Quote
Old 05-12-2012, 06:13 AM   PM User | #2
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,454
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 05-12-2012, 07:07 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 05-12-2012, 12:18 PM   PM User | #4
kipal
New to the CF scene

 
Join Date: May 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
kipal is an unknown quantity at this point
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
kipal is offline   Reply With Quote
Old 05-12-2012, 12:26 PM   PM User | #5
kipal
New to the CF scene

 
Join Date: May 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
kipal is an unknown quantity at this point
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
kipal is offline   Reply With Quote
Old 05-12-2012, 01:02 PM   PM User | #6
kipal
New to the CF scene

 
Join Date: May 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
kipal is an unknown quantity at this point
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....
kipal is offline   Reply With Quote
Reply

Bookmarks

Tags
checkbox, text

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting 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

Forum Jump


All times are GMT +1. The time now is 12:22 AM.


Advertisement
Log in to turn off these ads.