duanpao4172 2012-05-07 10:39
浏览 18

构建一个字符串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

  • 写回答

1条回答 默认 最新

  • dqj5046 2012-05-07 11:36
    关注

    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>
    
    评论

报告相同问题?

悬赏问题

  • ¥30 自适应 LMS 算法实现 FIR 最佳维纳滤波器matlab方案
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像