php - How to get form data to an xml file and send it by email

one text

I have an html form, I have successfully send the form data to email, and I save the form data after each submission to an xml file. I have 2 issues: 1- the Xml file always get only 1 entry, so whenever someone submit the form, the file hold only the last submission, and i need that the file hold every submission, and never delete it. 2- I would like to attach this file to the email with the form data. Thank you for helping me.

html code

<form action="suchauftrag-gesendet-6.php" method="post" role="form" id="suchauftrag-form">
          <div class="row">
            <div class="col-md-6">
              <h5 class="text-center pb-3">Meine Daten</h5>
              <label for="name">Name, Vorname <span class="secondary">*</span></label>
              <input type="text" class="form-control" name="name" id="name" required>
              <label for="email">E-Mail-Adresse <span class="secondary">*</span></label>
              <input type="email" class="form-control" name="email" id="email" required>
              <label for="street">Stra??e, Nr. <span class="secondary">*</span></label>
              <input type="text" class="form-control" name="street" id="street">
              <label for="street">Stadt <span class="secondary">*</span></label>
              <input type="text" class="form-control" name="city" id="city">
              <label for="street">PLZ <span class="secondary">*</span></label>
              <input type="text" class="form-control" name="zipcode" id="city">
              <label for="phone">Telefonnummer <span class="text-muted"></span></label>
              <input type="text" class="form-control" name="phone" id="phone">
            </div>
            <div class="col-md-6">
                <h5 class="text-center pb-3 secondary" id="isuche">Ich suche</h5>
                <div class="form-group">
                     <select class="select form-control" id="wohnung" name="wohnung" required>
                        <option value="" disabled selected>W?�hlen</option>
      <option value="Wohnung">Wohnung</option>
      <option value="Haus">Haus</option>
    </select>
                </div>
                <div class="form-group">
             <select class="select form-control" id="mietekaufen" name="mietekaufen" required>
                <option value="" disabled selected>W?�hlen</option>
      <option value="Miete">Zur Miete</option>
      <option value="Kauf">Zum Kauf</option>
    </select>
                </div>
                <div class="form-group"><fieldset name="gewerbe" id="gewerbe"> 
                    <p class="mt-2 mb-1"><strong>Gewerbeimmobilie</strong></p>
                    <div class="custom-control custom-checkbox d-md-inline ml-0">
                      <input type="checkbox" class="custom-control-input" id="customCheck5" name="gewerbe" value="Buroflache">
                      <label class="custom-control-label" for="customCheck5">B??rofl?�che</label>
                    </div>
                    <div class="custom-control custom-checkbox d-md-inline ml-0 ml-md-3">
                      <input type="checkbox" class="custom-control-input" id="customCheck6" name="gewerbe" value="Gastronomie">
                      <label class="custom-control-label" for="customCheck6">Gastronomie</label>
                    </div>
                    <div class="custom-control custom-checkbox d-md-inline ml-0 ml-md-3">
                      <input type="checkbox" class="custom-control-input" id="customCheck7" name="gewerbe" value="Ladenlokal">
                      <label class="custom-control-label" for="customCheck7">Ladenlokal</label>
                    </div>
                    <br/>
                    <div class="custom-control custom-checkbox mt-1">
                      <input type="checkbox" class="custom-control-input" id="customCheck8" name="gewerbe" value="Sonstiges">
                      <label class="custom-control-label" for="SonstigeGewerbeBeschreibung">Sonstiges und zwar:</label>
                      <input type="text" class="form-control mt-2" name="sonstige-gewerbe" id="sonstige-gewerbe">
                    </div></fieldset> 
                </div>
                <div class="form-group mb-0">
                    <p><strong>Ungef?�hre Gr?�??e</strong></p>
                    <label class="d-inline" for="size_von">von</label><input type="number" max="9999" class="form-control d-inline mx-3" name="size_von" id="size_von" style="width:4rem" required><label class="d-inline" for="size_bis">bis</label><input type="number" max="9999" class="form-control d-inline mx-3" name="size_bis" id="size_bis" style="width:4rem" required><span>qm</span>
                </div>
                <div class="form-group">
                    <p><strong>Budget</strong></p>
                    <label class="d-inline-block" style="width:4rem" for="budget_miete">Miete:</label><input type="number" max="99999" class="form-control d-inline mx-3 mb-2" name="budget_miete" id="budger_miete" style="width:6rem" required><span>EUR/Monat</span><br/><label class="d-inline-block" for="budget_kauf" style="width:4rem">Kauf:</label><input type="number" max="9999999" class="form-control d-inline mx-3" name="budget_kauf" id="budget_kauf" style="width:6rem" required><span>EUR</span>
                </div>
            </div>
           </div>
            <div class="form-group row pt-4 bg-color2">
                <div class="col-12">
                    <p class="mt-2 mb-3"><strong>Ort</strong> (Mehrfachnennungen m?�glich)</p>
                </div>
                <div class="col-6">
                    <label for="dusseldorf">D??sseldorf</label>
                     <select class="select form-control" id="dusseldorf" name="Stadtbezirk">
                        <option value="" disabled selected>W?�hlen</option>
      <option id="stadtbezirk1" value="Stadtbezirk-1">Stadtbezirk 1</option>
 <option id="stadtbezirk2" value="Stadtbezirk-2">Stadtbezirk 2</option>
  <option id="stadtbezirk3" value="Stadtbezirk-3">Stadtbezirk 3</option>
   <option id="stadtbezirk4" value="Stadtbezirk-4">Stadtbezirk 4</option>
    <option id="stadtbezirk5" value="Stadtbezirk-5">Stadtbezirk 5</option>
     <option id="stadtbezirk6" value="Stadtbezirk-6">Stadtbezirk 6</option>
      <option id="stadtbezirk7" value="Stadtbezirk-7">Stadtbezirk 7</option>
       <option id="stadtbezirk8" value="Stadtbezirk-8">Stadtbezirk 8</option>
        <option id="stadtbezirk9" value="Stadtbezirk-9">Stadtbezirk 9</option>
         <option id="stadtbezirk10" value="Stadtbezirk-10">Stadtbezirk 10</option>
    </select>
                </div>
                <div class="col-6">
                    <label for="mettmann">Mettmann</label>
                     <select class="select form-control" id="mettmann" name="Mettmann">
                        <option value="" disabled selected>W?�hlen</option>
      <option id="erkrath" value="Erkrath">Erkrath</option>
 <option id="haan" value="Haan">Haan</option>
  <option id="heiligenhaus" value="Heiligenhaus">Heiligenhaus</option>
   <option id="hilden" value="Hilden">Hilden</option>
    <option id="langenfeld" value="Langenfeld">Langenfeld</option>
     <option id="mettman" value="Mettmann">Mettmann</option>
      <option id="monheim" value="Monheim am Rhein">Monheim am Rhein</option>
       <option id="ratingen" value="Ratingen">Ratingen</option>
        <option id="velbert" value="Velbert">Velbert</option>
         <option id="wulfrath" value="Wulfrath">W??lfrath</option>
    </select>
                </div>
                <div class="col-12 col-md-10">
                    <label for="andere">Andere</label>
                     <select class="select form-control" id="andere" name="AndereOrte">
                        <option value="" disabled selected>W?�hlen</option>
      <option id="duisburg" value="Duisburg">Duisburg</option>
 <option id="essen" value="Essen">Essen</option>
  <option id="mulheim" value="Mulheim an der Ruhr">M??lheim an der Ruhr</option>
   <option id="krefeld" value="Krefeld">Krefeld</option>
    <option id="meerbusch" value="Meerbusch">Meerbusch</option>
     <option id="neuss" value="Neuss">Neuss</option>
    </select>
                </div>
                <div class="col-12 col-md-2 d-flex">
                    <p class="text-right mt-auto ml-auto"><span class="secondary"><br/><br/>*</span> Pflichtfelder</p>
                </div>
            </div>          
           <div class="row mb-5">
                <div class="col">
                    <hr class="pt-4 pb-1">
                    <div class="custom-control custom-checkbox">
                      <input type="checkbox" class="custom-control-input" id="customCheck35" required>
                      <label class="custom-control-label" for="customCheck35">Ich habe die <a href="datenschutz" target="_blank">Datenschutzerkl?�rung</a> zur Kenntnis genommen. Ich stimme zu, dass meine Angaben und Daten zur Beantwortung meiner Anfrage elektronisch erhoben und gespeichert werden.</label>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col">
                    <button type="submit" class="btn btn-primary w-100" name="submit" value="submit">Suchauftrag senden</button>
                </div>
            </div>
        </form>

php code


$subject = 'Formular - Suchauftrag';

// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Name', 'email' => 'Email', 'street' => 'Street', 'city' => 'City', 'zipcode' => 'Zipcode', 'wohnung' => 'Wohnung', 'mietekaufen' => 'Miete/kaufen', 'gewerbe' => 'Gewerbeimmobilie', 'sonstige-gewerbe' => 'Sonstige Gewerbe', 'size_von' => 'Size von(qm)', 'size_bis' => 'Size bis(qm)', 'budget_miete' => 'Budget Miete(?��/Monat)', 'budget_kauf' => 'Budget Kauf(?��)', 'Stadtbezirk' => 'Stadtbezirk', 'Mettmann' => 'Mettmann', 'AndereOrte' => 'Andere Orte'); 

error_reporting(E_ALL & ~E_NOTICE);

try
{

    if(count($_POST) == 0) throw new \Exception('Das Formular ist leer');
            
    $emailText = "Sie haben eine neue Nachricht von Ihrem Terminvereinbarungsformular \n=============================\n";

    foreach ($_POST as $key => $value) {
        // If the field exists in the $fields array, include it in the email 
        if (isset($fields[$key])) {
            $emailText .= "$fields[$key]: $value\n";
        }
    }

    // All the necessary headers for the email.
    $headers = array('Content-Type: text/plain; charset="UTF-8";',
        'From: ' . $from,
        'Reply-To: ' . $_POST['email'],
        'Return-Path: ' . $from,
    );
    
    // Send email
    mail($sendTo, $subject, $emailText, implode("\n", $headers));

    $responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
    $responseArray = array('type' => 'danger', 'message' => $errorMessage);
}


// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
    $encoded = json_encode($responseArray);

    header('Content-Type: application/json');

    echo $encoded;
}
// else just display the message
else {
    echo $responseArray['message'];
}


if(isset($_POST['submit']))
{
$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?><miles></miles>");

    $xml->appendChild('name', $_POST['name']);
    $xml->appendChild('email', $_POST['email']);
    $xml->appendChild('street', $_POST['street']);
    $xml->appendChild('city', $_POST['city']);
    $xml->appendChild('zipcode', $_POST['zipcode']);
    $xml->appendChild('phone', $_POST['phone']);
    $xml->appendChild('Wohnung-Haus', $_POST['Wohnung']);
    $xml->appendChild('Miete-kaufen', $_POST['mietekaufen']);
    $xml->appendChild('Gewerbe', $_POST['Gewerbe']);
   $xml->appendChild('Sonstige-Gewerbe', $_POST['sonstige-gewerbe']);
    $xml->appendChild('size_von', $_POST['size_von']);
    $xml->appendChild('size_bis', $_POST['size_bis']);
    $xml->appendChild('budget_miete', $_POST['budget_miete']);
    $xml->appendChild('budget_kauf', $_POST['budget_kauf']);
    $xml->appendChild('Stadtbezirk', $_POST['Stadtbezirk']);
    $xml->appendChild('Mettmann', $_POST['Mettmann']);
    $xml->appendChild('AndereOrte', $_POST['AndereOrte']);

    $asXML = $xml->asXML();
    $file = fopen("data.xml","w+");
    fwrite($file,$asXML);
    fclose($file); 
    print_r(error_get_last());

    if(file_exists('./data.xml'))
    {
        $myXML = file_get_contents('./data.xml');
        $xml = new SimpleXMLElement($myXML);
        $xmlpretty = $xml->asXML();

        // pretty print the XML in browser
        header('content-type: text/xml');
        echo $xmlpretty;
    }

}
?>

Source