1

I am using AJAX to get the value of the element chosen in a select input and to launch a PHP script that returns some input checkboxes fields.

Here is what it looks like :

HTML

<form method="post" action="liens_chra.php" name="Form" id="Form">

    <label for="id_turbo">Turbo</label>
    <select name="id_turbo" size="1" id="id_turbo">
        <option value="10970">TM1761178</option> 
        <!-- and more -->
    </select>

    <div id="choix_reffab">
        <!-- checkboxes appear here -->
    </div>
    <p class="form ">
        <input type="submit" name="valider" value="Enregistrer">
    </p>

    <!--    something I tried too
    <input  type="button" id="submitevent" value="Enregister">

    <script type="text/javascript" >
        $('#submitevent').click(function() {
             $("#Form").submit();
        });
    </script> -->
</form>

JQuery/AJAX

$("#id_turbo").change(function(){
    var id_turbo = $("#id_turbo").val();
    $.ajax({type: "POST",
        url: "<?=URLSITEWEB;?>admin/outils/ajax/liste_Reffab.php",
        data: "id_turbo="+id_turbo+"",
        error: function(){
            /*alert(id_famille+" \n ne passent pas.");*/
        },
        success: function(data){
            $("#choix_reffab").html(data);
        }
    });
});

PHP

/* things */
foreach ($fabTab as $fab) {
    $chaine .= '<input type="checkbox" "name=tabreffab[]" id="'.$fab.'" value="'.$fab.'" /><label for="'.$fab.'">'.$fab.'</label>';
}
echo $chaine;

So, when the user selects a value, some checkboxes appears. My problem is that the data I want is not transfered to $_POST, and here is the result :

var_dump($_POST['tabreffab']) // is NULL, others values are ok

I'm fairly new to AJAX & JQuery, so I have no idea what to do. I tried submitting the form using JQuery, made no changes.

Relkco
  • 35
  • 5
  • 4
    Remember, now you are sending data using AJAX, you only get in PHP what you send with the AJAX request. i.e. All you are sending is `data: "id_turbo="+id_turbo+"",` which would be better written as `{ id_turbo: $("#id_turbo").val()}` So now you need to process all your checkboxes in js and pass all the `:checked` ones along with the `id_turbo` value – RiggsFolly May 04 '16 at 10:15
  • 1
    This will probably help you amend your code http://stackoverflow.com/questions/2155622/get-a-list-of-checked-checkboxes-in-a-div-using-jquery – RiggsFolly May 04 '16 at 10:22

3 Answers3

1

I got your issues when you return your checkbox your mistake is at name property of element.

In your code "name=tabreffab[]" Replace with name = "tabreffab[]"

$chaine .= ''.$fab.'';

Insted of above write like below code

$chaine .= '<input type="checkbox" name = "tabreffab[]" id="'.$fab.'" value="'.$fab.'" /><label for="'.$fab.'">'.$fab.'</label>';
Denis Bhojvani
  • 817
  • 1
  • 9
  • 18
0

checkboxes value will display in $_POST[] only on selecting a value

try in a editor

 <?php
    echo "<pre>";
    print_r($_POST);
?>

<form id="sampleName" name ="sampleName" method="post" action="">
   <textarea id="testID" name="textAreaName"></textarea>   
   <input type="text" name="text1" />
   <input type="checkbox" name="check" value="1"/>
   <input type="checkbox" name="check" value="2"/>
   <input type="checkbox" name="check" value="3"/>
   <input type="submit" value="submit">
</form>
-1

The problem is in your Ajax call, specifically in the data part. It takes a json formatted object. The right way to write this is data:{id_turbo:"id_turbo_value"} not as what you used - data: "id_turbo="+id_turbo+""

Velimir Tchatchevsky
  • 2,812
  • 1
  • 16
  • 21
  • why the quotes around `id_turbo_value`, I mean that would just pass the string `id_turbo_value` as the value of `id_turbo`...? – benomatis May 04 '16 at 10:19
  • 1
    And that is not his problem at all. – RiggsFolly May 04 '16 at 10:19
  • 1
    never knew you could pass a string to `data`, I'm leaving the answer for people like me in the future. The quotes are to show the value must be quoted - something that is not true for the key (id_turbo_VALUE) is to represent the actual value not a variable name – Velimir Tchatchevsky May 04 '16 at 11:00