WordPress.org

Ready to get started?Download WordPress

Forums

AJAX and Permalinks (1 post)

  1. st421
    Member
    Posted 1 year ago #

    Hi! I have found some posts where users had similar issues but never found a good answer. Here is my problem: I wrote a plugin that provides shortcodes for email forms (that are submitted with AJAX), but it only sometimes works with pretty permalinks.

    Here is the code:

    <?php
    /*
        Plugin Name: Contact Form
        Plugin URI: http://susanltyler.com/contact-form-plugin/
        Description: Put contact forms on your WordPress site.
        Author: S. Tyler
        Version: 1.0
        Author URI: http://susanltyler.com
    */
    
    // <--- Hooks, shortcodes, and global variables --->
    add_shortcode('email_form','submit_email_form');
    add_action('wp_ajax_email_contact','send_email');
    add_action('wp_ajax_nopriv_email_contact','send_email');
    // <----------------------------------------------->
    
    /*
    Javascript/AJAX method called by shortcode that places form on page. Form is returned as string
    so as to be displayed before the_content. $atts should be supplied in the shortcode in the form of
    email="example@example.com"
    */
    function submit_email_form($atts) {
    	extract(shortcode_atts(array('to_email' => ''), $atts));
    	$nonce = wp_create_nonce('nonce_1');
    ?>
    <script type='text/javascript'>
    <!--
    jQuery(document).ready(function(){
    	jQuery('#submit_email').click(function() {
    		jQuery.ajax({
    			type: "post",
    			url: "wp-admin/admin-ajax.php",
    			data: {action: 'email_contact', fname: jQuery('#fname').val(), femail: jQuery('#femail').val(), to_email: '<?php echo $to_email; ?>', fsubject: jQuery('#fsubject').val(), fmessage: jQuery('#fmessage').val(), _ajax_nonce: '<?php echo $nonce; ?>'},
    			success: function(data) {
    				jQuery("#email_submitted").html(data);
    				jQuery("#email_submitted").fadeIn("fast");
    				jQuery("#contactform").fadeOut("slow");
    			},
    			error: function(data) {
    				alert("Something is wrong!");
    			}
    		});
    		return false;
    	})
    })
    -->
    </script>
    <?php
    	$contactform = '<form method="post" id="contactform"><input type="text" name="name" value="Name" id="fname">
            <input type="text" name="email" value="Email" id="femail">
            <input type="text" name="subject" value="Subject" id="fsubject">
    	<textarea name="message" id="fmessage" maxlength="1000" cols="45" rows="10">Message</textarea>
    	<input type="submit" value="Submit" id="submit_email" class="submit"></form><div id="email_submitted"></div>';
    	return $contactform;
    }
    
    /*
    The function that actually sends the email after checking that the message/name/etc. are not empty and that the
    given email is valid.
    */
    function send_email() {
    	check_ajax_referer('nonce_1');
    	$name = $_POST['fname'];
    	$to_email = $_POST['to_email'];
    	$from_email = $_POST['femail'];
    	$subject = $_POST['fsubject'];
    	$message = $_POST['fmessage'];
    	if($name == '' || $to_email == '' || $from_email == '' || $message == '') {
    		echo "One or more of the necessary inputs was left blank. Please try again with a name, email, and message.";
    	}
    	$headers  = 'MIME-Version: 1.0' . "\r\n";
    	$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
    	$headers .= 'From: ' . $from_email;
    	if(validate_email($from_email)) {
    		mail($to_email,$subject,$message,$headers);
    		echo "Your message has been sent.";
    	} else {
    		echo "Please try again with a valid email.";
    	}
    	die();
    }
    
    function validate_email($email) {
    	if(filter_var($email,FILTER_VALIDATE_EMAIL)) {
    		return true;
    	} else return false;
    }
    ?>

    It did seem to work for a little while with pretty permalinks but now nothing happens when you click to submit a form. Is there something obvious I'm missing? Thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic