0

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\">\n";
  echo "var str = ''; \n";
  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

3
  • 2
    You cannot mix JavaScript and PHP that way. PHP runs on the server, JavaScript in the browser. Or I don't understand your question... May 7, 2012 at 10:44
  • 1
    It sounds like you want check boxes, not buttons. May 7, 2012 at 10:45
  • Well I want the click on the button to be the action. I don't want a submit button. Thats why I though json-encode .
    – Joe
    May 7, 2012 at 10:58

1 Answer 1

0

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>
4
  • Well I want to store the string. How do I get it from "alert(str);" to php to store?
    – Joe
    May 7, 2012 at 11:42
  • What do you want to do in php side? I think you need form. Do you aware of form?
    – VibhaJ
    May 7, 2012 at 11:50
  • I am aware of form. But I want a click on each button, and then store the string.
    – Joe
    May 7, 2012 at 12:10
  • No, I posted the changes above. Looks like many problems to me.
    – Joe
    May 8, 2012 at 9:48

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.