Solved

how to grab selcted values from check boxes with Jquery, process array with php and make mysql query

Posted on 2011-02-17
5
268 Views
Last Modified: 2014-06-01
Hi All,
I have a web search for where user searches for plants at http://www.rootsnantucket.com/duplicate-of-the-catalog.html 

when user performs a search, jquery passes the search term to a page that then refreshes the table with the new values.  The new table has a new column labeled 'print me'.  User clicks on plants interested in printing and the hits "print these plants".  I want the values of the checkboxes to be passed to a second page attached below that will then run a select query and then create a new table with new plants.

I tried searching for "astil" which pulls up a bunch of plants.  I check some plants and then click print and an empty table gets returned to the viewer.  The values aren't being passed to the query, and are not being output.  

Any guidance or links to pages with a similar solution would be amazing.  I've spent way too long working on this simple problem.

//table row code
<tr sprite="" id="Astilbe "><td>Astilbe 'Sprite'</td><td>12-15</td><td>Part to full shade</td><td>Shell pink flowers turning to rust cloroed seed heads</td><td></td><td>Deer resistant.  </td><td><input type="checkbox" value="16" name="id"></td></tr>

//jquery code to pass variables
<script>
$(document).ready(function() { 
        $("#tablesorted").tablesorter( );
        $("#cfContact").click(function() {
                $("#tablesorted").remove();
                $.get('plant-search-page.html' , {'variety': $('#variety').val()},  function(data) {
                    $("#searchDiv").html(data);$("#tablesorted").tablesorter( );
                });
        });
});
$('#reset').click(function() {
                location.reload();
            });
$('#printMe').live('click', function() {
        var selectedItems = new Array();
           $("input[name='id']:checked").each(function() {selectedItems.push($(this).val());});

            $.get( 'print-plants.html', {'id':selectedItems}, function(data){
                    $("#tablesorted").remove();
                    $('#searchDiv').html(data);
            });
});
</script>



//prin plants page code
<?php
$id=$_GET['id'];
$id = implode(",",$id); 
$sql = "SELECT * FROM catalog WHERE id IN ($id)";
$resultArray = $modx->db->makeArray( $sql );//put it into an array
echo "<table id='tablesorted' class='tablesorter'><thead><tr>";
echo "<th class='sortable'>Variety</th><th class='sortable'>Height (in)</th><th class='sortable'>Exposure</th><th class='sortable'>Flower Color</th><th class='sortable'>Bloom Time</th><th class='sortable'>Points of Interest </th><th class='sortable'>Print Me</th>";
echo "</tr></thead><tbody>";

foreach($resultArray as $item)//go through each record in the array
{//start looping through the data array

echo "<tr id='".$item['variety']."'>";
echo"<td>".$item['variety']."</td>";
echo"<td>".$item['height']."</td>";
echo"<td>".$item['exposure']."</td>";
echo"<td>".$item['color']."</td>";
echo"<td>".$item['bloom_time']."</td>";
echo"<td>".$item['points_of_interest']."</td>";

echo "</tr>";
}//finish looping through the data array
echo"</tbody></table>";
?>

Open in new window

0
Comment
Question by:noahlearner
5 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 34919526
>how to grab selcted values from check boxes with Jquery

$("#myCheckbox_ID").attr("value");
0
 

Author Comment

by:noahlearner
ID: 34919601
thanks for your response, but I think that the values are already getting grabbed.  If I use firbug and watch what happens when I click "print these"  the parameters that are getting passed are  
id[]	12 
id[]	13

Open in new window


I think the problem is on the print-plant-page.html processor page because the params that are getting sent are not being out put back to the user.
0
 
LVL 4

Expert Comment

by:janicegannon
ID: 34926602
Try changing your code

I think it's the way you are searching for your input box...

Tested here and looks good.. If that's not it, then we need to look at your PHP..
// From This
var selectedItems = new Array();
           $("input[name='id']:checked").each(function() {selectedItems.push($(this).val());});
/. To This
var selectedItems = [];
$('#tablesorted').find('input:checkbox').each(function() {
    var plantID = $(this).val();
    selectedItems.push(plantID);
});

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question