Input value does not print completely variable with space

Asked

Viewed 108 times

0

good morning. I have a problem here, when I will update a page with data coming from the database the select option shows the data correctly but the captured values are not correct, an example, if it is of cities comes in value only the first name of the city while in the display of the name comes complete, so when I do the update only records the first name, because this dividing and creating fields. what I need is that on the update page the select option comes with the data that is in the database.


    <select class="form-control" name="cidade">
  <option value="">-- Selecionar --</option>
  <?php
$curc4 = $pdo->query("SELECT * FROM comarca ORDER BY comarc asc");
while ($lic4 = $curc4->fetch(PDO::FETCH_ASSOC)) {
echo "<option value=".$lic4['comarc']." ".($cidade == $lic4['comarc'] ? "selected":"")." >".$lic4['comarc']."</option>";
} ?> 
</select>

//solução encontrada

<select class="form-control" name="cidag">
                        <option value="">-- Selecionar --</option>
                        <?php
                        $curc4 = $pdo->query("SELECT * FROM comarca ORDER BY comarc asc");
                        while ($lic4 = $curc4->fetch(PDO::FETCH_ASSOC)) {
                          $comarc=$lic4['comarc'];
                          if($cidag == $comarc){
                            $selected = "selected";
                          } else {
                            $selected = " ";
                          }
                           ?>
                    <option value="<?php echo $comarc;?>" <?php echo $selected;?> ><?php echo $comarc;?></option>
                       <?php } ?> 
                      </select>

console shows this way

<option value="ANGRA" dos="" reis="">ANGRA DOS REIS</option>

I thank you for your help

  • https://answall.com/help/someone-answers

1 answer

0


Put options values in quotes value=".... "which will work

If you prefer it can be in simple quotes value=' .... '

echo "<option value=\"".$lic4['comarc']."\" ".($cidade == $lic4['comarc'] ? "selected":"").">".$lic4['comarc']."</option>";

The browser interprets the value of the property value WITH SPACE as two properties. The solution is to hug with quotes.

OBS

In firefox, I noticed the attribute "selected" doesn’t work unless you put the select within a form, where the form has an attribute name.

To avoid any problem use selected='selected'

echo "<select class=\"form-control\" name=\"cidag\">
     <option value=\"\">-- Selecionar --</option>";
                    
        $curc4 = $pdo->query("SELECT * FROM comarca ORDER BY comarc asc");
        while ($lic4 = $curc4->fetch(PDO::FETCH_ASSOC)) {
                      
      /*######### com aspas simples ficaria assim ###########################################################################
       echo "<option value='".$lic4['comarc'']."' ".($cidag == $lic4['comarc''] ? "selected='selected'":"").">".$lic4['comarc'']."</option>";
      ###################################################################### */
                      
       echo "<option value=\"".$lic4['comarc'']."\" ".($cidag == $lic4['comarc''] ? "selected='selected'":"").">".$lic4['comarc'']."</option>";
       }
       echo "</select>";
  • almost perfect Leo, only problem is that the select option is coming empty on the update page, it should come with the data that are in the database. on the console the error that was before disappeared, was solved.

  • in fact the log is coming in this way <option value="ANGRA DOS REIS Selected">ANGRA DOS REIS</option>

  • @Gerson, it’s just a question of quotation marks

  • I edited the answer

  • hahaha, I took a beating to put the quotes in the right place

  • I made other changes here, but select option keeps coming empty, if I work with id instead the name works as done in the first code when I posted the doubt, but with names I’m not getting.

  • i did not express myself right, select options come normal with all data for choice, only that it does not appear the city Selected that this set in the database when it opens the page for editing, this comes empty, hence I would have to select again.

  • I will check if there is another error that is disturbing and return if you find.

  • @Gerson just copy what’s in my answer and replace it in his code. If it doesn’t work right the error is in another part of your code that is not posted in the question.

  • thanks for your help, but I only managed to make it work as above edition . Thank you very much.

  • Right, but notice that you did exactly what I suggested, embrace VALUE with quotes <option value="<? php echo $comarc;? >" just differently, segmented PHP. I’ll edit the answer

Show 6 more comments

Browser other questions tagged

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