How to send two values per parameter with a single option selected? PHP

Asked

Viewed 1,405 times

4

I need to send two values per parameter to another page, however value select tag only sends one... How could I send two?

Follows the code:

<option value="<?php echo $data->format("d/m/Y"); ?>">
     <?php echo $data->format("d/m/Y").' às 14:15'; ?>
</option>
  • Maybe you need an attribute multiple no select. Can you explain better what you want to do? give examples pf.

  • For example, I am selecting that option there that is showing on the screen the date and time together, like '01/12/2014 at 14:15', but in the value field I can only send the date...

2 answers

4


An alternative is you do not specify the value, in this case the content of <option>, for example:

<option>
    <?php echo $data->format("d/m/Y") . ' às 14:15'; ?>
</option>
  • That’s no use to me either, because I need to separate him on arrival at destination...

  • 2

    You can separate the time date using explode('às', $select); where $select is the name of <select> in the <form>

  • But how would I put the two separate values on file reception? $Data = $_POST['slcDataHora']; and $Hora = $_POST['slcDataHora'];?

  • 2

    Probably so: $dados = explode('às', $_POST['slcDataHora']); then you’ll have the date on $dados[0] and the time in $dados[1]. But beware of SQL Injection, you are using the $_POST without any kind of sanitation.

3

You can do it this way:

<select name="">
    <option value="{'num_sequence':[0,1,2,3]}">Passando Por Array</option>
    <option value="{'foo':'bar','one':'two'}">Passando por Objeto</option>
</select>
  • I think this is the best solution for my case, but I don’t understand how it works... It would be <option value="{'teste':[<?php echo $data->format("d/m/Y"); ?>, 20:30]}">? so it didn’t work... As I call the value for a variable, in the case?

  • 3

    @Alceu in this case can use the object, value="{'dia':'<?php echo $data->format("d/m/Y"); ?>','hora':'20.30'}" and when you fetch the value from the DOM element you have to use JSON.parse(select.value) to retrieve the object. Note that you must use single quotes in HTML and double quotes in the string/value for JSON to be valid.

Browser other questions tagged

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