Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've been researching this for 2 days but have yet to find an adequate solution to my issue. Essentially I am trying to "link" database rows by ID based on multiple check boxes using jQuery, but here is where it gets a little tricky:

Background:

  • I have a database table (positions) displayed as an HTML table (#ct) with check boxes next to each row.
  • Up to four (4) rows can be checked at a time and "linked" to one another (but don't worry about the limit of number of boxes that can be checked)
  • Linking the rows from positions SHOULD create one new row in covered_positions with columns id(PK), linked_id1, linked_id2, linked_id3, linked_id4

What is already done:

  • Each checkbox already has its value assigned to the respective row id in positions
  • A dropdown select box already triggers the Javascript function ctActions()

What I think is needed:

  • Create an array in ctActions() of the checkbox values using something like:

    var linked_ids = [];
    
    $("#ct input[type='checkbox']:checked").each(function() {
        linked_ids.push(this.value);
    });
    
  • Encode and send the data to PHP:

    var linked_ids_json = JSON.stringify(linked_ids);
    
    $.post('link-positions.php', { linked_ids_json: linked_ids_json } );
    

  • In my link-positions.php:

    // Get & explode array
    $array = json_decode($_POST['linked_ids_json']);
    
    // Test whether each value is set -- Not sure if needed
    $linked_id1 = (isset($array[0]) ? $array[0] : "");
    $linked_id2 = (isset($array[1]) ? $array[1] : "");
    $linked_id3 = (isset($array[2]) ? $array[2] : "");
    $linked_id4 = (isset($array[3]) ? $array[3] : "");
    
    include('../mysqli-connect.php');
    $conn = dbConnect();
    
    $sql = "INSERT INTO `covered_positions` (
        linked_id1,
        linked_id2,
        linked_id3,
        linked_id4)
        VALUES (?, ?, ?, ?)";
    
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('iiii',
                $linked_id1,
                $linked_id2,
                $linked_id3,
                $linked_id4);
    
    $stmt->execute();
    
    $conn->close();
    

But after all this, nothing happens. I'm sure there are some blatant errors in my code so I thought I would post it for some more experienced eyes.

UPDATES

  • Fixed $stmt->execute to $stmt->execute() but still does not solve issue.
share|improve this question
+1 for the very detailed question, though "nothing happens" is not really a valid problem. A good start is by opening Chrome dev tools' Network tab or Firebug's Net tab before sending the ajax and it will display the response from your php. – Fabrício Matté Feb 15 at 18:15
1  
Shouldn't $stmt->execute; be $stmt->execute(); in link-positions.php? – sbeliv01 Feb 15 at 18:21
on top of the ->execute stuff, you should also add in proper error handling on the DB code. you're simply assuming that each of those calls (connect, prepare, bind, execute) has succeeded. DB operations have exactly ONE way to succeed, and a near infinite number of ways to fail. ALWAYS check return values or try/catch db operations. – Marc B Feb 15 at 18:24
have you var_dump'ed the input data on link-positions.php? – Manatax Feb 15 at 18:49
Just used console to discover a 404 for link-positions.php so I simply moved the file to the correct directory and the script works! – Tomanow Feb 15 at 19:01
show 1 more comment

3 Answers

Check in the FireBug if the AJAX request gets completed succesfully, or any error comes.

Next, check what data you are getting on the server by print_r($_POST);

Considering your HTML is like

<form id="form1">
    <input class="cb" type="checkbox" value="a" name="cbTest[]" />
    <input class="cb" type="checkbox" value="b" name="cbTest[]" />
    <input class="cb" type="checkbox" value="c" name="cbTest[]" />
    <input class="cb" type="checkbox" value="d" name="cbTest[]" />
</form>

I would suggest you to get the checked values by using

// if the form only contains checkboxes
var linked_ids_json = $('#form1').serialize();

// Or
var linked_ids_json = $('.cb').serialize();
$.ajax({
    type: 'post',
    url: 'link-positions.php',
    data: linked_ids_json,
    success: function () {
        // callback
    }
});

Use in PHP like,

$array = $_POST['linked_ids_json'];

You'll be able to access the values like $linked_id[0];

share|improve this answer
Thank you for your input. I unfortunately cannot use the form method because the table is already in a form. I managed to solve the problem however, it was a simply 404 for my link-positions.php. – Tomanow Feb 15 at 19:03

Solved by the OP. This was originally edited into the question body:

I found link-positions.php file to be in the wrong directory! The code below works just fine.

share|improve this answer
up vote 0 down vote accepted

I managed to solve the problem. The code works fine, just the link-positions.php was in the wrong directory. Hope the code helps others with similar tasks.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.