I am creating a php page that allows users to add a new letter to a letter archive that is saved to an XML file. One of the form elements is recipient with a text box. I would like to add a button that when clicked it will bring up another text box incase there are two recipients, I'm not sure how to do this( I know how to add a button to the page, just the method for when the button is cliked?) and if I need to change the XML file as each letter record only has one recipient tag?

Dani AI

Generated

Two practical routes, both used in production: keep one input and parse multiple values on submit, or add inputs dynamically with JavaScript and submit them as an array. and pointed at those options; below are concise, actionable examples and a few cautions so the XML and server code remain robust.

A minimal client-side pattern (works with plain JS): have the initial input named recipient[], add a button that clones/creates additional input elements also named recipient[], and submit the form normally. On the server PHP side read $_POST['recipient'] as an array, trim/filter empty items, then write each item as its own XML element. Example server-side construction using DOMDocument (creates safe text nodes):

<div id="recipients">
  <label>Recipient <input type="text" name="recipient[]" /></label>
</div>
<button type="button" id="addRecipient">Add recipient</button>

<script>
document.getElementById('addRecipient').addEventListener('click', function(){
  var container = document.getElementById('recipients');
  var label = document.createElement('label');
  label.textContent = 'Recipient ';
  var input = document.createElement('input');
  input.type = 'text';
  input.name = 'recipient[]';
  label.appendChild(input);
  container.appendChild(label);
});
</script>
$recipients = array_filter(array_map('trim', $_POST['recipient'] ?? []));
$dom = new DOMDocument('1.0','UTF-8');
$letter = $dom->createElement('letter');
$dom->appendChild($letter);
$recipsNode = $dom->createElement('recipients');
foreach ($recipients as $r) {
  $rNode = $dom->createElement('recipient');
  $rNode->appendChild($dom->createTextNode($r));
  $recipsNode->appendChild($rNode);
}
$letter->appendChild($recipsNode);
$dom->save('letters.xml');

If you prefer a single control, split on a delimiter server-side (for example commas/semicolons or one-per-line) with preg_split and then normalize as above. If you change the XML schema to allow multiple <recipient> elements, update any readers and consider a one-time migration script for older single-recipient records. Always validate and sanitize inputs, escape XML via DOM methods (avoid manual string concatenation), provide accessible labels, and include a server-side fallback so the form works correctly if JavaScript is disabled.

Recommended Answers

All 2 Replies

Instead of this route, you could allow the user to enter two recipients separated by either a comma or semi-colon - similar to how many email clients work.

I agree with paulkd. You could write in the JS to add more text boxes dynamically, but it would probably be much easier to just have them separate them by a delimiter. You could use a textarea and have them put a new address on each line.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.