how to concatenate values in php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anis Nimi
    New Member
    • Oct 2012
    • 1

    how to concatenate values in php?

    how to concatenate multiple check box values in same fields


    Pls rply me fast........... ........
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I have no idea what you mean.

    Comment

    • johny10151981
      Top Contributor
      • Jan 2010
      • 1059

      #3
      First of never tell "Reply me fast". That is very rude.

      Secondly your question does not make any sense.

      if you want the checkbox to be selected then simply:

      do below:
      Code:
      <INPUT TYPE="Checkbox" CHECKED>
      if you want it to be judge by php then it can look like something below:

      Code:
      if(certain_condition_fulfilled()==true)
      {
       $sel="SELECTED";
      }
      else $set="";
      ?>
      <INPUT TYPE="Checkbox" <?php echo $set;?>>

      Comment

      • ATNC33
        New Member
        • Sep 2012
        • 7

        #4
        I can't understand your question. On checkbox you can only have one answer... Like, gender: masculin or feminin.

        A if you use a radio, then you can have more than one answer...

        I don't know if i'm helping, but i can't do better with your question!

        Comment

        • johny10151981
          Top Contributor
          • Jan 2010
          • 1059

          #5
          ATN, will you rethink your answer?

          Comment

          • johny10151981
            Top Contributor
            • Jan 2010
            • 1059

            #6
            did you edit the question? If you did then explain the reason Please

            Comment

            • jdstankosky
              New Member
              • Sep 2012
              • 30

              #7
              ATNC33 you have that backwards. Checkboxes are for multiple values. Radio buttons are for a single value from multiple choices.

              Anis Nimi, You literally want the values concatenated? Like:

              value1value2

              or you want them in an array?

              array[0]=value1
              array[1]=value2

              or do you want them as an array in PHP?

              You need to clarify your question.

              Comment

              • Rabbit
                Recognized Expert MVP
                • Jan 2007
                • 12517

                #8
                @jd, ATNC33 is not the original poster. I believe they were trying to answer the original poster.

                Comment

                • jdstankosky
                  New Member
                  • Sep 2012
                  • 30

                  #9
                  I addressed the original poster in my second paragraph.

                  Comment

                  • VanessaMeacham
                    New Member
                    • Sep 2012
                    • 31

                    #10
                    HI ! i think you could try this :

                    Code:
                    <html>
                    <head>
                    <title>Serializing Checkbox</title>
                    <script src="http://code.jquery.com/jquery-latest.js"></script>
                    <script>
                    $(document).ready(
                            function()
                            {
                                    $("#test").click(
                                    function() {
                                            serializeCheckbox();
                                    }
                                    );
                            }
                    );
                    
                    function serializeCheckbox()
                    {
                            var string = '&string=';
                            
                            var inputs = document.getElementsByTagName('input');
                            
                            for( var x = 0; x < inputs.length; x++ )
                            {
                                    if(inputs[x].type == "checkbox" && inputs[x].name == 'field') 
                                    {
                                            if(inputs[x].checked == true)
                                            {
                                                    string += inputs[x].value + ',';
                                            }
                                    }
                            }
                        if (/,$/.test(string)) {
                            string=string.replace(/,$/,"")
                        }
                            alert(string);
                    }
                    
                    </script>
                    </head>
                    <body>
                    <form>
                    <input type="button" id="test" value="Click me" />
                    <br />
                    <input name="field" type="checkbox" value="blue" />Blue<br />
                    <input name="field" type="checkbox" value="red" />Red<br />
                    <input name="field" type="checkbox" value="green" />Green<br />
                    </form>
                    </body>
                    </html>
                    Last edited by Dormilich; Oct 13 '12, 08:11 AM. Reason: Please use [CODE] [/CODE] tags when posting code.

                    Comment

                    • solutionwand
                      New Member
                      • Dec 2012
                      • 16

                      #11
                      Hi,
                      I hope this short code shall help you to understand the whole concept.

                      Code:
                      $concated_str = '';
                      if(isset($_POST['checkboxvalues'])){
                          $concated_str = implode(',',$_POST['checkboxvalues']);
                      }
                      //store $concated_str in the database
                      In short, use simply implode function. For more detail.

                      Comment

                      Working...