2

Currently i have a HTML form that collects some metadata about some files, and each user as to fill some fields. I want to register some keywords about the data. I could ask them to write the keywords by hand on a normal text box, but i would prefer to have a list of checkboxes of about 10/15 values.

Then i need to pass only the checked values to a PHP file, using $_POST. My problem is that i assign a variable to those values, and then i call that variable in a DOM event. I´m generating a XML file, and currently i have the HTML ready to register these keywords in text input. I understand how to create the checkboxes, and pass them as an array to PHP., by reading other questions here. But i can´t understand how to pass this array to a $dom->createElement situation, and preferably separated by commas.

PHP

//Pull data from HTML form
$keywordsString = $_POST['keywords'];

// Creates xml document
$dom = new DOMDocument();
    $dom->encoding = 'utf-8';
    $dom->xmlVersion = '1.0';
    $dom->formatOutput = true;

$xmlFileName = 'example_example.xml';

// Adds metadata to xml
$metadata = $dom->createElement('MD_Metadata');

    $idInfo = $dom->createElement('identificationInfo');
        $descriptiveKeywords = $dom->createElement('descriptiveKeywords');
                $CharacterString = $dom->createElement('CharacterString', $keywordsString);
                $descriptiveKeywords->appendChild($CharacterString);
            $idInfo->appendChild($descriptiveKeywords);
    $metadata->appendChild($idInfo);

$dom->appendChild($metadata);

$dom->save($xmlFileName);

I can´t figure out how to pass checkbox values to that $keywordsString , but separated by commas. The rest i can understand and write, using the other questions about this kind of issue.

Thanks in advance for all help provided.

4
  • You'd need to use a foreach and the inputs need to be able to accept multiple arrays, I.e.: []. Commented Dec 16, 2019 at 17:05
  • Thank you. The foreach is a bit confusing, but i'll look into it with more attention. The multiple array was on my mind but wasn´t sure on how to proceed about it. Commented Dec 16, 2019 at 17:08
  • 1
    Welcome. Have a look at this Q&A here on Stack Overflow. I submitted an answer in there myself, but there are other answers in that same question that you can have a look at. It will / should give you a good idea as to how to go about this. Commented Dec 16, 2019 at 17:10
  • That Q&A is really enlightening, even with a quick read. I will focus on it, thanks again. Commented Dec 16, 2019 at 17:16

1 Answer 1

2

Your html should look like this:

<form action="" method="post">
  <input name="choice[]" type="checkbox" value="1" /> 
  <input name="choice[]" type="checkbox" value="2" /> 
  <input name="choice[]" type="checkbox" value="3" /> 
  <input name="choice[]" type="checkbox" value="4" /> 
  <input type="submit" value="order" />
</form>

In PHP, you can iterate over using foreach loop. Place the below code at the top of your html form:

A small note from basics, when posting data via post method, you need to apply Post redirect get design pattern to prevent form re-submissions.

If the data you are sending is not sensitive, you can do it via get request.

<?php
    if($_POST):
    foreach($_POST['choice'] as $val)
    {
    echo $val . "<br>";
    }
    endif;
?>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.