We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7019
    • 135 Posts
    hello,

    i know a little bit of e-form, but i want to make a contactform that i use in websites that aren’t made with modx

    this is my snippet:


    <?php
    if(!empty($_POST['submit']))
    { 
    if(strlen($_POST['naam']) == 0)
      { $error_msg .="- Vul aub uw naam in.<br>"; } 
    if(strlen($_POST['voornaam']) == 0)
      { $error_msg .="- Vul aub uw voornaam in.<br>"; }   
    if(strlen($_POST['telefoon']) == 0)
      { $error_msg .="- Vul aub uw telefoon- of GSM-nummer in.<br>"; }
    if(!ereg("^[_a-zA-Z0-9-]+(\.[*@([a-zA-Z0-9-]+)*@([a-zA-Z0-9-]+\.)+([a-zA-Z]{2,4})$", $_POST['emailadres']))
      { $error_msg .="- Vul aub een geldig email adres in.<br>"; } 
    if(strlen($_POST['vraag']) == 0)
      { $error_msg .="- Laat aub weten waar u meer info over wenst.<br>"; }
      
    if(!empty($error_msg))
      { 
        //Een van de velden werd niet goed ingevuld 
        echo "<div style='font-weight:bold; border:1px dashed #C13605; color:#C13605;padding-left:5px'>Contactformulier kon niet worden uitgevoerd door volgende redenen:<br><br>"; 
        echo $error_msg; 
        echo "<br>Klik aub op <a href=javascript:history.back(1)>Ga terug</a> en vul alle velden correct in.</div><br><br>"; 
      }
    else 
      { 
        $ontvanger = "[email protected]"; //Het email adres van de persoon die vragen moet ontvangen. 
        $naamvoornaam = $_POST['naam'] . " " . $_POST['voornaam']; //naam en voornaam van de verzender
        $emailadres = $_POST['emailadres']; //het emailadres van de verzender
        $subject = "Contact via website Lili-Wood.be"; //onderwerp van de mail. 
        $header = "From: " . $naamvoornaam . " <".$emailadres.">\r\n"; 
        $mail_body = "Contact via website Lili-Wood.be: " . date("d-m-Y") . " om " . date("H:i") . " uur uitgevoerd.\n"; 
        $mail_body .= "De volgende gegevens werden ingevuld:\n\n"; 
        $mail_body .= "Naam: " . $_POST['naam'] . "\n";
        $mail_body .= "Voornaam: " . $_POST['voornaam'] . "\n";
        $mail_body .= "Straat: " . $_POST['straat'] . "\n";
        $mail_body .= "Postcode - Gemeente: " . $_POST['postcode'] . " " . $_POST['gemeente'] . "\n";
        $mail_body .= "Telefoon/GSM: " . $_POST['telefoon'] . "\n";
    	$mail_body .= "E-mailadres: " . $_POST['emailadres'] . "\n";
    	$mail_body .= "Vraag: " . $_POST['info'] . "\n";
        $mail_body .= "\n\n -- Einde contactpagina --"; 
        mail($ontvanger, $subject, $mail_body, $header); 
        echo "<b>Uw bericht is succesvol verzonden.</b><br><b>Er zal zo spoedig mogelijk contact met u opgenomen worden.</b></font><br>";  
      }
    }  
    else 
    { 
    ?>
            <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="contact">
              <div class="formentry"> 
                <label class="label" for="naam">Naam: *</label>
                <input name="naam" type="text" size="30" id="naam" />
              </div>
              <div class="formentry"> 
                <label class="label" for="voornaam">Voornaam: *</label>
                <input name="voornaam" type="text" size="30" id="voornaam" />
              </div>
              <div class="formentry"> 
                <label class="label" for="straat">Straat + nr:</label>
                <input name="straat" type="text" size="30" id="straat" />
              </div>
              <div class="formentry"> 
                <label class="label" for="postcode">Postcode:</label>
                <input name="postcode" type="text" size="5" id="postcode" />
              </div>
              <div class="formentry"> 
                <label class="label" for="gemeente">Gemeente:</label>
                <input name="gemeente" type="text" size="30" id="gemeente" />
              </div>
              <div class="formentry"> 
                <label class="label" for="telefoon">Telefoon/GSM: *</label>
                <input name="telefoon" type="text" size="15" id="telefoon" />
              </div>
              <div class="formentry"> 
                <label class="label" for="emailadres">E-mailadres: *</label>
                <input name="emailadres" type="text" size="30" id="emailadres" />
              </div>
              <div class="formentry"> 
                <label class="label" for="info">Vraag: *</label>
                <textarea name="info" cols="45" rows="6" id="info"></textarea>
              </div>
              <div> 
                <input class="button" type="submit" name="submit" value="Verzenden" />
              </div>
              <div id="veldenverplicht"> (Velden met een * zijn verplicht in te vullen) 
              </div>
            </form>
            <?php
    					}
    ?>
    


    everything looks fine, but when i click on the send-button, then i go straight to the index-page and further nothing. it doesn’t send the information in the contactform. Even when an required field is empty, the error-call doesn’t show. it also go to the indexpage

    has this something to do with the following code that doesn’t work in modx?:

    <form action="<? echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="contact">

    or do i have to put the 2 parts of php in a 2 snippets and the html in a chunk and do a call to this 2snippets and 1 chunk in the documenten content?
      • 3496
      • 101 Posts
      I found the following in the MODx documentation, perhaps it’s relevant:

      * Don’t try to mix PHP and HTML in a Snippet. Snippets execute PHP code. They should always begin with a <?php and end with a ?> You cannot mix PHP and HTML in a Snippet! For example, the following code won’t work:

      view plaincopy to clipboardprint?

      1. <p>This is a horrible mixture of HTML and PHP</p>
      2. <?php
      3. return "<p>and PHP!&nbsp; Don’t try it!&nbsp; It’s bad architecture and it won’t work!!</p>";
      4. ?>

      <p>This is a horrible mixture of HTML and PHP</p>
      <?php
      return "<p>and PHP!&nbsp; Don’t try it!&nbsp; It’s bad architecture and it won’t work!!</p>";
      ?>

      You’ll find that MODx will append PHP tags to beginning and end of the snippet, creating an invalid syntax, e.g.
      view plaincopy to clipboardprint?

      1. <?php <?php //something here ?> ?>

      <?php <?php //something here ?> ?>

      If you need to do something like this, use a Chunk - separate the PHP into a Snippet, load its output into a placeholder with the modx API placeholder functions, and include the Snippet’s placeholders in the Chunk.
        • 7019
        • 135 Posts
        thanks for your help

        i wil try to do something like you’ve said