1

So what I'm trying to do is add a name='' value to an option in a drop down list. This all is pretty simple using just HTML. But I would like to have the name value be from the database I'm queuing. But the database string has spaces in between the words, and this causes a problem. I need the string to be Eten & drinken and not the way it outputs now, see example below.

Right now, I've got this:

   <?php $q = $row["Categorie"];
        echo    "   
            <option value=".$q." name=".$q.">
               ".$row["Categorie"]."&nbsp;&nbsp;
                   <span style='font-size:1.2rem;color:#F8F8F8;'>(" . $row['x'] .  ")
                   </span>
            </option>
    ";?>

Which outputs the following if I look at the source:

<option value="eten-en-drinken" name="Eten" &amp;="" drinken="">
    Eten &amp; drinken&nbsp;&nbsp;(19)
</option>

I've tried searching google and str_replace, htmlspecialchars, utf8_encode and changed name to data-type but noting works. I just want it to be Eten en drinken. I need this exact value for use in a second drop down menu. Any thoughts, documentation or help?

4
  • close the php tag after $q = $row["Categorie"]; Commented Apr 19, 2017 at 12:42
  • Sorry, forgot the PHP tags... updated my question. The SELECT * etc is outside this file, but that all works correct. The $row's are shown correct and the data is queued, only the name='' part is the problem here. Commented Apr 19, 2017 at 12:47
  • this is weird. in your php, you do not have name and value attributes Commented Apr 19, 2017 at 12:49
  • @Akin What do you mean? (I just made an other update, forgot to change the name value back, oops). How about now? The $stub value was the same as the name value. So I changed that, hope this is better and clearer. Commented Apr 19, 2017 at 12:54

3 Answers 3

1

Your problem migh be solved by adding missing quotes for name attribute.

echo    "   
        <option value=".$q." name='".$q."'>..."
Sign up to request clarification or add additional context in comments.

2 Comments

Riiiiightttttt. Now I feel kind of stupid. But this was it. Thank man!
No problem, it can be confusing with all the quotes :)
0

Try this code

$q = $row["Categorie"];

echo "< option value=".$stub." name=".$q." > ".$row["Categorie"]." < span style='font-size:1.2rem;color:#F8F8F8;'>(" . $row['x'] . ")< /span>< /option>";

Comments

0

You should try using htmlspecialchars() when saving your data to mySQL and not when retrieving it-

<?php
  $new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
  echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
?>

When retrieving your data, try using htmlspecialchars_decode()-

<?php
  $str = "<p>this -&gt; &quot;</p>\n";
  echo htmlspecialchars_decode($str);

  // note that here the quotes aren't converted
  echo htmlspecialchars_decode($str, ENT_NOQUOTES);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.