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

    I like to use my own simple subscription writing emails into a text-file -- but just got an ’irrational’ problem with ModX.


    The Chunk {{subscribe}} implemented in the website:

    <!-- checks input before submitting -->
           <script language="JavaScript">
    	var find_result;
    	function form_email(){
    		var str=document.validation.email.value;
    		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    		if (filter.test(str))
    			find_result=true;
    		else{
    			alert("Bitte geben Sie eine gültige Email-Adresse an!")
    			find_result=false;
    		}
    		return (find_result);
    	}
    	function checkbae(){
    		if (document.layers||document.getElementById||document.all)
    			return form_email();
    		else
    			return true;
    	}
    	</script>
    
    <!-- the field where the user enters the email -->
    <center>
    	<form name="validation" action="index.php?id=100" onSubmit="return checkbae()" method="post">
    		<p>Inform me if a new version is released:</p>
    		<p>Email: <input type="text" name="email" style="width:120px;" /></p>
    		<input id="submitButton" type="image" src="assets/images/button.png" alt="Register" class="subbutton" />
    	</form>
    </center>
    



    The referring document (see form: action=index.php?id=100) has the snippet "[[subscriber]]" included:

    The Snippet:

    <?php
           // transferred EMAIL
    	$email = $_POST['email'];
    
           // IP FUNCTION
    	function getIp(){
    	      if( isset( $_SERVER ['HTTP_X_FORWARDED_FOR'] ) ){
    		      $ip = $_SERVER ['HTTP_X_FORWARDED_FOR'];
    	      }elseif( isset( $_SERVER ['HTTP_VIA'] ) ){
    		      $ip = $_SERVER ['HTTP_VIA'];
    	      }elseif( isset( $_SERVER ['REMOTE_ADDR'] ) ){
    		      $ip = $_SERVER ['REMOTE_ADDR'];
    	      }else{
    		      $ip = "Anonima" ;
    	      }
    	      return $ip;
    	}
            $ipadresse = getIp();
    
           // DATE formatting 
    	$heute = getdate();
    	if($heute[mon]<10) { $monat = "0".$heute[mon]; }
    		else { $monat = $heute[mon]; }
    	if($heute[mday]<10) {  $tag = "0".$heute[mday]; }
    		else { $tag = $heute[mday]; }
    	if($heute[minutes]<10) {  $minute = "0".$heute[minutes]; }
    		else { $minute = $heute[minutes]; }
    
           // WRITE FILE
            $fileURL = (string) ($modx->config['base_path'].'assets/subscribers.txt');
            $file = fopen($fileURL, 'a');
    	// alternatively I tried: $file = fopen("assets/subscribers.txt", 'a');
    	$writeThat = $email.";".$heute[year]."-".$monat."-".$tag." ".$heute[hours].":".$minute." - IP: ".$ipadresse."\n";
    	fwrite($file, $writeThat);
    	fclose($file);
    
    	// inform me via email
    	$mail_to = '[email protected]';
    	$subject = 'New Subscriber';
    	$text = $heute[year]."-".$monat."-".$tag." ".$heute[hours].":".$minute." - IP: ".$ipadresse."\n".$email."\n";
    	$header = "From: [email protected]";
    	mail($mail_to, $subject, $text, $header);
    ?>



    When I tried it first everything worked fine. Some days passed, then I got no new subscribers so I tried the subscription myself again... and nothing happened! Changing the code again, clearing the cache, trying it once more... it worked again. Days later, the same: doesn’t work...

    Please help me. What am I doing wrong? Has the behaviour something to do with the cache? Can’t I use php this way in modx? (I am no php-developer, sorry.)
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      The problem is not with MODx being irrational wink

      Any time you have an interactive snippet, or a snippet that will get changeable data, you need to call the snippet in its uncached form: [!...!]. Or else set the document it’s on to be uncached.

      From the wiki documentation:

      If a document is cacheable then the snippets on that document need to be called as unchacheable [! !]. This is true of any interactive snippets, or snippets that will fetch dynamic content, such as forms or feed readers.

      If the page with the snippet output is cached, the page is served next time without processing the snippet, which means there’s nobody listening to the POST from the form.

      Calling the snippet uncached means that even if the page is cached, any uncached snippets will be processed, and then it will pick up the POST and generate the appropriate output.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 15270
        • 4 Posts
        Now it works like a charm! many thx!

        a newbie & the cache wink