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

4
  • 5
    use radio button instead checkbox so user will able to select only one Commented May 29, 2014 at 6: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. Commented May 29, 2014 at 6:18
  • 1
    @EhsanSajjad well that's the HTML right there.. he's injecting it using php... Commented May 29, 2014 at 6:20
  • 1
    and btw you are using old outdated mysql_* function ....and it wont work in new php versions check stackoverflow.com/questions/12859942/… Commented May 29, 2014 at 6:22

3 Answers 3

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());
}
Sign up to request clarification or add additional context in comments.

2 Comments

You might want to actually assign the new value to the href… i think that's what the OP want..
If i use multiple buttons on a page?
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.

Comments

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.

2 Comments

You can check if user selected only one option, so it's possible to use checkbox too.
yes but in checkbox have to set conditions while in radio box just the same name will work. Short and Easy

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.