WordPress.org

Ready to get started?Download WordPress

Forums

wordpress contact form don't work correctly (2 posts)

  1. rerraw
    Member
    Posted 6 months ago #

    hi i created modal contact form by twitter bootstrap and converted it by php to wordpress but when click on the send button i go to homepage
    i don't why occure this error

    my file

    <?php
    	// Function for email address validation
    	function isEmail($verify_email) {
    			return(preg_match("/^[-_.[:alnum:]]+@(((<a href="http://codex.wordpress.org/:alnum:">:alnum:</a>|<a href="http://codex.wordpress.org/:alnum:">:alnum:</a>[[:alnum:]-]*<a href="http://codex.wordpress.org/:alnum:">:alnum:</a>)\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",
    			$verify_email)
    		);
    	}
    
    	$error_name = false;
    	$error_email = false;
    	$error_message = false;
    
    	if (isset($_POST['contact-submit'])) {
    		// Initialize variables for the form fields
    		$name = '';
    		$email = '';
    		$website = '';
    		$message = '';
    		$receiver_email = '';
    
    		// Get the name
    		if (trim($_POST['contact-author']) == '') {
    			$error_name = true;
    		} else {
    			$name = trim($_POST['contact-author']);
    		}
    
    		// Get the email
    		if (trim($_POST['contact-email']) == '' || !isEmail(trim($_POST['contact-email']))) {
    			$error_email = true;
    		} else {
    			$email = trim($_POST['contact-email']);
    		}	
    
    		// Get the website
    			$website = trim($_POST['contact-url']);	
    
    		// Get the message
    		if (trim($_POST['contact-message']) == '') {
    			$error_message = true;
    		} else {
    			$message = stripcslashes(trim($_POST['contact-message']));
    		}	
    
    		// Check if we have errors
    		if (!$error_name && !$error_email && !$error_message) {
    			// Get the received email
    			$receiver_email = 'rerraw@gmail.com';
    
    			$subject = 'You have been contacted by' . $name;
    			$body = "You have been contacted by $name. Their Message is:". PHP_EOL . PHP_EOL;
    			$body .= $message . PHP_EOL . PHP_EOL;
    			$body .= "You can contact $name via email at $email.";
    
    			if ($website != '') {
    				$body .= "and visit their website at $website";
    			}
    			$body .= PHP_EOL . PHP_EOL;
    
    			$headers = "From $email" . PHP_EOL;
    			$headers .= "Reply-To: $email" . PHP_EOL;
    			$headers .= "MIME-Version: 1.0" . PHP_EOL;
    			$headers .= "Contetnt-type text/plain; charset=utf-8" . PHP_EOL;
    			$headers .= "Content-Transfer-Encoding: qouted-printable" . PHP_EOL;
    
    			if (mail($receiver_email, $subject, $body, $headers)) {
    				$email_sent = true;
    			} else {
    				$email_sent_error = true;
    			}
    
    		}
    	}
    
    ?>
    
    <?php if (isset($email_sent) && $email_sent = true) : ?>
    	<h3><?php _e('Success', 'Yallanpe-Theme'); ?></h3>
    	<p><?php _e('You have successfully sent the message.', 'Yallanpe-Theme'); ?></p>
    <?php elseif (isset($email_sent_error) && $email_sent_error = true) : ?>
    	<h3><?php _e('Error!', 'Yallanpe-Theme'); ?></h3>
    	<p><?php _e('We coudnt send the message at this time please try again later.', 'Yallanpe-Theme'); ?></p>
    <?php else : ?>
    
    <div class="modal fade" id="contactus" tabindex="-1" role="dialog" aria-labelledby="modal-contactus" aria-hidden="true">
    <div class="modal-dialog">
    	<div class="modal-content">
    		<div class="modal-header">
    			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    			<h4 class="modal-title" id="modal-contactus">Contact With Us</h4>
    		</div>
    		<div class="modal-body">
    			<form action="" method="POST" id="contact-form" role="form">
    				<div class="form-group">
    					<div class="input-group">
    						<span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
    						<input value="<?php if (isset($_POST['contact-author'])) echo $_POST['contact-author']; ?>" type="text" class="form-control" name="contact_author" id="contact_author" placeholder="First Name" />
    					</div>
    				</div>
    				<div class="form-group">
    					<div class="input-group">
    						<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
    						<input value="<?php if (isset($_POST['contact-email'])) echo $_POST['contact-eamil']; ?>" type="text" class="form-control" name="contact_email" id="contact_email" placeholder="Email Address" />
    					</div>
    				</div>
    				<div class="form-group">
    					<div class="input-group">
    						<span class="input-group-addon"><span class="glyphicon glyphicon-envelope"></span></span>
    						<input value="<?php if (isset($_POST['contact-url'])) echo $_POST['contact-url']; ?>" type="text" class="form-control" name="contact_url" id="contact_url" placeholder="Wbsite URL" />
    					</div>
    				</div>
    				<div class="form-group">
    					<div class="input-group">
    					 <span class="input-group-addon"><span class="glyphicon glyphicon-comment"></span></span>
    					 <textarea class="form-control" name="contact_message" id="contact_message" cols="60" rows="10" placeholder="Write Your Message Here"><?php if (isset($_POST['contact-message'])) echo $_POST['contact-message']; ?></textarea>
    					</div>
    				</div>
    				<div class="modal-footer">
    					<input type="hidden" name="contact-submit" id="contact-submit" value="true" />
        				<input type="submit" class="btn btn-default" data-dismiss="modal" value="Go Back" />
    					<input type="submit" class="btn btn-primary" value="Send" />
        			</div>
    			</form>
    		</div>
    	</div>
    </div>
    </div>
    
    <?php endif; ?>

    [Moderator Note: Please post code or markup between backticks or use the code button. Or better still - use a pastebin. Your posted code may now have been permanently damaged by the forum's parser.]

  2. bcworkz
    Member
    Posted 5 months ago #

    I'm not sure how modals actually work in regards to how the DOM object is structured, but it seems some information is missing from the resulting POST request that WP needs to get back to this page. You could try explicitly providing a full path as the action attribute instead of "". A more efficient and reliable approach would be to submit the form using AJAX, but some significant reorganization of your code would be in order. Not much re-coding, it just needs to reside elsewhere.

Reply

You must log in to post.

About this Topic