1

My code is there,

while($row = mysql_fetch_array($query2)) 
{
echo "<tr>";
$ids = $row['list_id'];
echo "<td><input type='checkbox' name='checkbox[]' value=" . $ids . "></td>";
echo "<td>" . $ids . "</td>";
echo "<td>" . $row['list_name'] . "</td>";
echo "<td>" . $row['campaign_id'] . "</td>";
echo "<td>" . $row['list_description'] . "</td>";
echo "<td>" . $row['list_changedate'] . "</td>";
echo "<td>" . $row['list_lastcalldate'] . "</td>";
echo "<td>" . $row['reset_time'] . "</td>";
echo "</tr>";
}

And

<a href="import.php"><input type="button" value="Import Leads to List"></a>

I wanna pass the checked checkbox value on the URL when button clicked. Like this,

Http://localhost/test/import.php?id=1234

if multiple multiple checkboxes checked, I wanna print error message or alert box like,

Please Select any one List ID!

Please help me.. thank you

Loki
  • 363
  • 4
  • 15
  • 5
    use radio button instead checkbox so user will able to select only one – NullPoiиteя May 29 '14 at 06:14
  • 2
    should note that all the rows generated have one checkbox having the same `id='checkbox'`, that's not valid, although your code can run OK in some cases. – King King May 29 '14 at 06:18
  • 1
    @EhsanSajjad well that's the HTML right there.. he's injecting it using php... – T J May 29 '14 at 06:20
  • 1
    and btw you are using old outdated mysql_* function ....and it wont work in new php versions check http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189 – NullPoiиteя May 29 '14 at 06:22

3 Answers3

5

1) change id='checkbox' to valid class='checkbox' EDIT: add class='checkbox';

2) Use jQuery function to check checked checkboxes cound and to get name:

$('button').click(function(){
    if ($('.checkbox:checked').length !== 1) {
       alert("Please Select any one List ID!");
    } else {
        *var url = window.location.href + '?id=' + $('.checkbox:checked').eq(0).attr('name');*
         **EDIT**:
         var url = window.location.href + '?id=' + $('.checkbox:checked').eq(0).val();
         $('a').attr('href', url);
    }
});

EDIT "What if i have multiple buttons":

Replace checkbox in loop with <a href='index.php?id=<?php echo $ids; ?>'><button>Import to List</button></a>, and there will be no need for js manipulation.

OR

Change checkbox: <td><input type='checkbox' onClick='changeUrl(this)' data-link-id='UniqueIdOfLink', and instead of $('button').click make function:

function changeUrl(e) {
    $('#' + $(e).data('link-id')).attr('href', 'index.php?id=' + $(e).val());
}
Justinas
  • 41,402
  • 5
  • 66
  • 96
2

First of all as others said, use radiobutton instead checkbox so users can select only one. Since you haven't posted your HTML code I can only provide you a sketchy scheme: (I use native JS, but you can make it in jQuery)

the PHP+HTML:

<form action="" name="name_of_form">
while($row = mysql_fetch_array($query2)) {
   echo "<tr>";
   $ids = $row['list_id'];
   echo "<td><input type='radio' name='radio[]' onclick='setURL(this.value)' value=".$ids."></td>";
   echo "<td>" . $ids . "</td>";
   echo "<td>" . $row['list_name'] . "</td>";
   echo "<td>" . $row['campaign_id'] . "</td>";
   echo "<td>" . $row['list_description'] . "</td>";
   echo "<td>" . $row['list_changedate'] . "</td>";
   echo "<td>" . $row['list_lastcalldate'] . "</td>";
   echo "<td>" . $row['reset_time'] . "</td>";
   echo "</tr>";
}
?>
<input type="submit" value='Import Leads to List'/>
</form>

The javascript:

function setURL(id){
     document.name_of_form.action="Http://localhost/test/import.php?id="+id;
}

Note that if you use forms you don't have to manually monkey around the url, you can just use $_POST.

ACs
  • 1,325
  • 2
  • 21
  • 39
0
$(document).ready( function () {

$('button').click(function(){

var dea = $("input[type='checkbox']").attr('value');
    window.location.href = 'http://localhost/test/import.php?id='+dea;
});

});

if you want to select only one option at a time you will use a radio button.

Tooba
  • 41
  • 8
  • You can check if user selected only one option, so it's possible to use checkbox too. – Justinas May 29 '14 at 06:34
  • yes but in checkbox have to set conditions while in radio box just the same name will work. Short and Easy – Tooba May 29 '14 at 06:37