0

There must be a kazillion topics and help related to this, but I cannot figure out how to implement anything to work with my setup.

HTML:

    <!-- SHOW/HIDE TEXT FIELD -->
    <!-- IF BITTEN ANIMAL="YES" or BITTEN "BOTH" Show Field -->
<div id="hidden_textfield10" style="display:none;">
    <p class="data_bold">What kind of animal</p><div class="data_small">What kind of animal has your pet bitten? Check all that apply:</div>
    <label for="Wild"><input class="form_style" type="checkbox" value="Wild" id="Wild" name="whatKindofAnimal[5]" />Wild</label><br />
    <label for="Bird"><input class="form_style" type="checkbox" value="Bird" id="Bird" name="whatKindofAnimal[4]" />Bird</label><br />
    <label for="Cat"><input class="form_style" type="checkbox" value="Cat" id="Cat" name="whatKindofAnimal[3]" />Cat</label><br />
    <label for="Dog"><input class="form_style" type="checkbox" value="Dog" id="Dog" name="whatKindofAnimal[2]" />Dog</label><br />
    <label for="Other-Animal"><input class="form_style" type="checkbox" id="Other-Animal" name="whatKindofAnimal[1]" onclick="if(!this.checked) document.getElementById('otherKindofAnimal').value='' " />Other</label>
    <!-- IF BITTEN ANIMAL="Other" Show Field -->
<div class="Other-Animal" style="display:none;">
    <input class="form_style" type="text" placeholder="Please Specify" id="otherKindofAnimal" name="whatKindofAnimal[1]" />
</div>
    <!-- END "Other" Field -->

PHP:

<strong>What Kind of Animal:</strong> <?php $name = 
$whatKindofAnimal = implode(", ", $_POST['whatKindofAnimal']); {echo 
htmlspecialchars($whatKindofAnimal);} ?></p>

I know the name is the same for the last radio button and the textbox, but this works for my setup and is irrelevant with my dilemma. Disregard the HTML.

How do I correct the php to remove that last comma?

Thanks!!! Tracy

3
  • 6
    Filter out the empty elements of the array before imploding: implode(', ', array_filter($_POST['whatKindofAnimal'])). Commented Aug 22, 2017 at 14:10
  • You Are Amazing! Thank you! Yes, I had read about the array_filter, along with a thousand other suggestions, but like I said, I had no idea of how to, or where to, add it; and add it correctly. Thank You! I'm hoping this was my last little dilemma with this form producing results correctly. Again, Thank You!!! Commented Aug 22, 2017 at 16:38
  • Not a complete duplicate of page linked above. I had read that post before and it did not help in my situation. The answer that deceze gave above fixed my problem easily w/2 words and a couple characters. Hope it helps someone else, too! Commented Aug 22, 2017 at 18:41

3 Answers 3

1

You can try and use rtrim to remove the last comma.

Man page found here: http://php.net/manual/en/function.rtrim.php

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of giving each animal a specific position in the whatKindofAnimal array, you can just get the elements to fill the array where needed. Note the use of whatKindofAnimal[] instead of whatKindofAnimal[5]

<label for="Wild"><input class="form_style" type="checkbox" value="Wild" id="Wild" name="whatKindofAnimal[]" />Wild</label><br />

Selecting Bird and Cat gives...

Array ( [0] => Bird [1] => Cat )

Comments

-1

You could use substrings:

 $FieldLength = StrLen($YourField);
 $YourField = SubStr($YourField, 0, $FieldLength-1);

2 Comments

You could also just use -1 as a parameter for substr().
I know its just for better understanding that i've made them separate

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.