We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29201
    • 239 Posts
    I’m simply trying to get my contact form working inside Modx so that I can add it to my menu, keep it easy to maintain, etc.

    Here’s my URL - http://fairview-condo.com/index.php?id=26. Check it out and try to submit the form. Ok so you see what’s happening?

    I’ve placed the following form code into a chunk on my contact form page (although I’m not really sure this is necc.):

    <form id="contactForm" action="contact-us.php" method="post">
    <p><span class="form-col1">Your Name:<span class="red">*</span></span> <input name="cname" /></p>
    <p><span class="form-col1">Your Email:<span class="red">*</span></span> <input id="email" name="cemail" /></p>
    <p><span class="form-col1">Phone:<span class="red">*</span></span> <input id="cphone" name="cphone" /></p>
    <p><span class="form-col1">Website:</span> <input id="cwebsite" name="cwebsite" /></p>
    <p><label class="form-col1">Message:</label> <textarea id="message" cols="20" rows="5" name="cmessage"></textarea></p>
    <p> <input class="submit" name="submit" src="assets/templates/revival/images/submit.png" type="image" value="Send Message" /></p>
    </form>

    As you can see it posts to a response file that is outside of Modx called contact-us.php. The contents of the PHP file are as follows:

    <?php
    
    switch($_REQUEST['cemail']) {
    	default: 
    // replace the bellow email with your email id
    $sendto = '[email protected]';  break;	
    }
    
    $subject = 'Contact from Fairview Condo Contact Form';
    $errormessage = 'Oops! There seems to have been a problem. May be these...';
    $thanks = "Thanks for contacting us! We'll get back to you as soon as possible!";
    $emptyname =  'Enter your name?';
    $emptyemail = 'Enter your email address?';
    $emptytele = 'Enter your telephone number?';
    $emptymessage = 'Enter a message?';
    $alertname =  'Enter your name using only the standard alphabet?';
    $alertemail = 'Enter your email in this format: <i>[email protected]</i>?';
    $alerttele = 'Enter your telephone number in this format: <i>xxx-xxx-xxxx</i>?';
    $alertmessage = "Making sure you aren't using any parenthesis or other escaping characters in the message? Most URLS are fine though!";
    
    $alert = '';
    $pass = 0;
    
    function clean_var($variable) {
        $variable = strip_tags(stripslashes(trim(rtrim($variable))));
      return $variable;
    }
    
    if ( empty($_REQUEST['cname']) ) {
    	$pass = 1;
    	$alert .= "<li>" . $emptyname . "</li>";
    } elseif ( ereg( "[][{}()*+?.\\^$|]", $_REQUEST['cname'] ) ) {
    	$pass = 1;
    	$alert .= "<li>" . $alertname . "</li>";
    }
    if ( empty($_REQUEST['cemail']) ) {
    	$pass = 1;
    	$alert .= "<li>" . $emptyemail . "</li>";
    } elseif ( !eregi("^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$", $_REQUEST['cemail']) ) {
    	$pass = 1;
    	$alert .= "<li>" . $alertemail . "</li>";
    }
    if ( empty($_REQUEST['cphone']) ) {
    	$pass = 1;
    	$alert .= "<li>" . $emptytele . "</li>";
    } elseif ( !ereg( "\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}", $_REQUEST['cphone'] ) ) {
    	$pass = 1;
    	$alert .= "<li>" . $alerttele . "</li>";
    }
    if ( empty($_REQUEST['cmessage']) ) {
    	$pass = 1;
    	$alert .= "<li>" . $emptymessage . "</li>";
    } elseif ( ereg( "[][{}()*+?\\^$|]", $_REQUEST['cmessage'] ) ) {
    	$pass = 1;
    	$alert .= "<li>" . $alertmessage . "</li>";
    }
    
    	if ( $pass==1 ) {
    
    	echo "<script>$(\".message\").hide(\"slow\").show(\"slow\"); </script>";
    	echo "<b>" . $errormessage . "</b>";
    	echo "<ul>";
    	echo $alert;
    	echo "</ul>";
    
    	} elseif (isset($_REQUEST['cmessage'])) {
    
    	    $message = "From: " . clean_var($_REQUEST['cname']) . "\n";
    		$message .= "Email: " . clean_var($_REQUEST['cemail']) . "\n";
    	    $message .= "Telephone: " . clean_var($_REQUEST['cphone']) . "\n";
    		 $message .= "Website: " . clean_var($_REQUEST['cwebsite']) . "\n";
    	    $message .= "Message: \n" . clean_var($_REQUEST['cmessage']);
    	    $header = 'From:'. clean_var($_REQUEST['cemail']);
    
    		mail($sendto, $subject, $message, $header);
    
    		echo "<script>$(\".message\").hide(\"slow\").show(\"slow\").animate({opacity: 1.0}, 4000).hide(\"slow\"); $(':input').clearForm() </script>";
    		echo $thanks;
    		die();
    		echo "<br/><br/>" . $message;
    
    	}
    	
    
    ?>
    
    


    As you have seen.....the error messages don’t display on my form page. Instead they show up on the contact-us.php page.

    I have tried using a snippet and posting the form data to itself and this keeps the error messages on the correct page.....however they don’t work properly. Incidentally this works fine in PRD and locally if I bring everything out of MODX.

    Could someone lend a hand???

    Many thanks,

    eliot
      • 1343 ☆ A M B ☆
      • 2,213 Posts
      Does the contact-us.php page exist in the same location as index.php? Or are you trying to use eform to run this which might make things easier?
        Patrick | Server Wrangler
        About Me: Website | Tweets |  MODX Hosting
        • 29201
        • 239 Posts
        The contact-us.html and contact-us.php page are both in the root of the site. I was working with eForm but the Captcha wasn’t displaying.
          • 3749
          • 24,544 Posts
          I’m assuming that you want the form to post to itself. In that case you want:

          <form id="contactForm" action="[~[*id*]~]" method="post">


          If you want it to post to another page that is a document on the MODx site, just replace [*id*] with that document’s ID number:

          <form id="contactForm" action="[~[12]~]" method="post">



            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 29201
            • 239 Posts
            i ended up getting this to work by posting to another page. Once the validation finished I redirected to a Thank You page. Not the tidiest of solutions.

            Thanks for your help.