How to treat the value field?

Asked

Viewed 103 times

0

I have in my html a form, in it I have a field described this way:

<option value="sao-gabriel-da-palha">São Gabriel da Palha</option>

In my database I have to put the value equal to value, so I wanted the value to come different, come as São Gabriel da Palha and not sao-gabriel-da-palha. Can anyone tell me how to do this?

Here is my form:

<h3>O que você esta procurando? Digite aqui:</h3>
        <form class="form-inline" action="busca.php" method="post">
            <div class="form-group">
                <input type="text" class="form-control" id="palavra" placeholder="Digite aqui..." name="palavra">
            </div>
            <div class="form-group">
                <label for="cidade">Selecione a cidade:</label>
                <select name="cidade" class="form-control" id="cidade">
                    <option value="sao-gabriel-da-palha">São Gabriel da Palha</option>
                    <option value="São Domingos do Norte">São Domingos do Norte</option>
                    <option value="Vila Valério">Vila Valério</option>
                </select>
            </div>
            <button type="submit" class="btn btn-default">Buscar</button>
        </form>
  • normally value is the primary key, id, code, the information that will be displayed, name, description, etc...are two columns in the database table. Ps If you are from São Gabriel da Palha, greetings capixabas, I’m from Colatina =]

  • You are using php?

  • Yes, I am using PHP.. And yes @Rovann Linhalis from São Gabriel da Palha

3 answers

2


I recommend doing what Rovann Linhalis mentioned in his comment, but if you want to continue with the idea, you can do it as follows:

//Vamos supor que você colocou o valor do option na variável $cidade
$cidade = ucwords(str_replace('-', ' ', $cidade));

The code is replacing the hyphens by blanks and capitalizing each word’s initial.

Now just save the value of $cidade in the database.

Some tests:

Entrada: "sao-gabriel-da-palha"    Saída: "Sao Gabriel Da Palha"

Entrada: "rio-de-janeiro"          Saída: "Rio De Janeiro"

See working on Ideone.

1

<?php
// Troca 'fromPerson' pelo nome do seu campo html
if( isset($_POST['fromPerson']) && $_POST['fromPerson'] === 'sao-gabriel-da-palha' ) {
    $fromPerson = 'São Gabriel da Palha';
    echo $fromPerson;
}
  • Dude, I didn’t know that function

  • I see a redundancy in your code, in case it validates $_POST['fromPerson'] === 'sao-gabriel-da-palha', he wouldn’t need to validate the isset($_POST['fromPerson']).

0

Your form is generated through PHP or you have it written on a static HTML page?

If you are generating the form through PHP it is better to generate it with the values the way they should go to the database...

<select>
<?php 
// imaginando que o nome das cidades venham de algum array parecido com este
$cidades = array( 'São Paulo', 'Rio de Janeiro', 'Goiania');
foreach( $cidades as $cidade ){
    echo '<option value"'. $cidade .'">'. $cidade .'</option>
}
?>
</select>
  • my form is generated in a PHP file

  • Try following the example I gave there, it should not be very different, you will only need to change the line that shows the name of the city in the 'value' parameter of select.

  • I’ll put my form here in the question and you show me how I should do, it can be?

Browser other questions tagged

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