pass checkbox values to the url

Asked

Viewed 646 times

0

Good afternoon, Personal I am with a doubt in how to make to pass the selected items of a page for a URL to send a report all the code already working nicely only missing that part.

example

item     codigo
1        11
2        12
3        13
4        14
5        15

how do I select the checkbox of items 1,2,5 to pass a url in the following way www.teste.com/realatorio.php? id=1,2,5

<?php
                while ($row = mysql_fetch_object($saldo_financeiro2)) {
                    $cliente = $row->nome_cliente;/* dentro do $row[] vai o nome da coluna da sua consulta */
                    echo '<tr>
                    <td>
                        <input class="input-field" type="checkbox" name="age" value="'.$row->item.'"/>
                        <label></label>
                    </td>';
                    echo "<td>".date('d-m-Y', strtotime($row->codigo))."</td>
                    </tr>";
                }
                ?>

inserir a descrição da imagem aqui

  • Why not forward via POST? via GET is easier to manipulate results, confusing, I think

  • and how would it be via post? because with various data so I have not yet been able to do

  • Can you help me @Andrébaill

3 answers

2


You can do something like this

<input type="checkbox" name="age" <?php if (isset($age) && $age=="1") echo "checked";?> value="1">1-9
<input type="checkbox" name="age" <?php if (isset($age) && $age=="10") echo "checked";?> value="10">10-19
<input type="checkbox" name="age" <?php if (isset($age) && $age=="20") echo "checked";?> value="20">20-29
<input type="checkbox" name="age" <?php if (isset($age) && $age=="30") echo "checked";?> value="30">30-39

Server side receives as a $_POST['age']

 $ages = $_POST['age'];
 print($ages[0]);
  • I’ll post my code on how he mounts this extrusion there

  • because when I put yours if I select one I can no longer select nor another and while trying to add the <?php if (isset($age) && $age=="10") echo "checked";?> creates error

  • Sorry pus type=radio and is type=checkbox

  • I’m having trouble inserting this input into an echo <?php&#xA; while ($row = mysql_fetch_object($saldo_financeiro2)) {&#xA; echo '<tr>&#xA; <td>&#xA; <input class="input-field" type="checkbox" name="age" <?php if (isset($age) && $age=="30") echo "checked";?> value="'.$row->item.'" />&#xA; <label></label>&#xA; </td>';&#xA; echo "&#xA; <td>$row->codigo</td>&#xA; </tr>";&#xA; }&#xA; ?>

  • Remove <?php if (isset($age) && $age=="30") echo "checked";? > and try again for the rest it looks good to me

  • I can’t select anything

  • it must have the input type checkbox tag misspelled make sure it is doing well http://www.w3schools.com/tags/att_input_checked.asp

  • I had to format the result as follows $id_trabalho = implode( ',', $_POST['age'] );

Show 4 more comments

0

window.history.pushState(data, null, 'url_target');

You will have to do a function, for example, with jquery.

$('checkbox').on('click', function(){
   window.history.pushState(null,null,'index.php?id'+$(this).val());
});

NOTE: It is an HTML5 feature, depending on the broswer may not work.

0

How do I select the checkbox of items 1,2,5 to pass a url in the following way www.teste.com/realatorio.php? id=1,2,5.
That was your question to my answer, created just your example url which is www.teste.com/realatorio.php? id=1,2,5.

in HTML you create the inputs I created this class to guide what is happening

<input type="checkbox" valor="1">
<input type="checkbox" valor="2">
<input type="checkbox" valor="3">
<input type="checkbox" valor="4">
<p class="url"></p>

In Jquery you create your URL

jQuery(function($) {
    // www.teste.com/realatorio.php?id=1,2,5
    var url = Array();
    $('input[type="checkbox"]').change(function(){
        if($(this).is(':checked')){
            url.push($(this).attr('valor'));
        }else{
            url.pop($(this).attr('valor'));
        }
        var terminar = url.toString();
        $('#url').html('<p>www.teste.com/realatorio.php?id='+terminar+'</p>');
    })
})

In the example you will create a url similar to what you want
"www.teste.com/realatorio.php? id=1,2,5" marking the checked inputs will fetch the values . I hope it helped.

This is an example of how you can do your url .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.