构建一个字符串json_encode

I am working with a php file. I want to present 8 css buttons.

   echo "<div class='num_outer'>right ";

   <button type="num-btn" onclick="do_some_code()">"1"</button>
   <button type="num-btn" onclick="do_some_code()">"2"</button>
   <button type="num-btn" onclick="do_some_code()">"3"</button>
   <button type="num-btn" onclick="do_some_code()">"4"</button>

   echo $colors;
   echo " </div>";// end num_outer

Okay so I want them to choose one or more colors and have them concatenate into the variable $colors. So $colors would look like "142", if 3 where chosen. After hours of researching, it appears json_encode may be the best. But I am open to any suggestions.

I also want it to work on all browsers. Thank you for your help.

edit ------------------------------------------ Suggested code -----------

  echo "<div class='num_outer'>right ";
  echo "<script language=\"JavaScript\">
";
  echo "var str = ''; 
";
  echo " function do_some_code(val)";
  echo "{";
echo "   str+=val;";
echo "}";
  echo "</script> ";
  echo " <button type="num-btn" onclick="do_some_code('1')">"1"</button>";
  echo " <button type="num-btn" onclick="do_some_code('2')">"2"</button>";
  echo "  <button type="num-btn" onclick="do_some_code('3')">"3"</button>";
  echo "  <button type="num-btn" onclick="do_some_code('4')">"4"</button>";

  echo "   <button type="num-btn" onclick="window.location.href='get_tst1a.php?        str='+str">Show me value</button>";
   $str = $_GET['str']; 
   echo " </div>";// end num_outer

My file name for this is get_tst1a.php

Why are you using echo for all html tags. Your php string concatination is breaking. Try below.

<div class='num_outer'>right 
<script language="JavaScript">
var str = ''; 
function do_some_code(val)
{str+=val;}
</script>
<button type="num-btn" onclick="do_some_code('1')">"1"</button>
<button type="num-btn" onclick="do_some_code('2')">"2"</button>
<button type="num-btn" onclick="do_some_code('3')">"3"</button>
<button type="num-btn" onclick="do_some_code('4')">"4"</button>
<button type="num-btn" onclick="window.location.href='get_tst1a.php?str='+str">Show me value</button>"
<? 
    echo $str = $_GET['str']; 
?>
</div>