I created a shortcode for user submission form. I want to submit this form without loading the page (Ajax).
Here is my shortcode code
add_shortcode('et-test-plugin_shortcode', function($atts, $content) {
$atts = shortcode_atts(array(
'name-field' => 'Name',
'phone-field' => 'Phone Number',
'email-field' => 'Email Address',
'budget-field' => 'Desired Budget',
'min-budget' => '1000',
'max-budget' => '10000',
'message-field' => 'Message',
'submit-btn-label' => 'Submit',
), $atts);
ob_start();
?>
<form action="<?php echo admin_url('admin-ajax.php'); ?>" class="et_test_form" method="post">
<div class="row">
<div class="col-sm-6">
<input type="text" name="_name" placeholder="<?php echo esc_attr($atts['name-field']) ?>">
</div>
<div class="col-sm-6">
<input type="text" name="_phone" placeholder="<?php echo esc_attr($atts['phone-field']) ?>">
</div>
<div class="col-sm-6">
<input type="email" name="_email" placeholder="<?php echo esc_attr($atts['email-field']) ?>">
</div>
<div class="col-sm-6">
<input type="number" name="_budget" min="<?php echo esc_attr($atts['min-budget']) ?>" max="<?php echo esc_attr($atts['max-budget']) ?>" placeholder="<?php echo esc_attr($atts['budget-field']) ?>">
</div>
</div>
<textarea class="form-control" rows="3" cols="10" name="_message" placeholder="<?php echo esc_attr($atts['message-field']) ?>"></textarea>
<input type="submit" name="submit" value="<?php echo $atts['submit-btn-label']; ?>"/>
<div id="note"></div>
</form>
<?php
$html = ob_get_clean();
add_action('wp_ajax_et_test_plugin_create_post', 'et_test_plugin_create_post');
add_action('wp_ajax_nopriv_et_test_plugin_create_post', 'et_test_plugin_create_post');
function et_test_plugin_create_post() {
if (isset($_POST['submit'])) {
$name = !empty($_POST['_name']) ? $_POST['_name'] : '';
$phone = !empty($_POST['_phone']) ? $_POST['_phone'] : '';
$email = !empty($_POST['_email']) ? $_POST['_email'] : '';
$budget = !empty($_POST['_budget']) ? $_POST['_budget'] : '';
$message = !empty($_POST['_message']) ? $_POST['_message'] : '';
if ($name) {
wp_insert_post(
array(
'post_title' => wp_strip_all_tags($name) . ' submitted a form',
'post_type' => 'customer',
'post_status' => 'private', /* Or "draft", if required */
'meta_input' => array(
'et-test-plugin_customer_name' => wp_strip_all_tags($name),
'et-test-plugin_customer_phone' => wp_strip_all_tags($phone),
'et-test-plugin_customer_email' => wp_strip_all_tags($email),
'et-test-plugin_customer_budget' => wp_strip_all_tags($budget),
'et-test-plugin_customer_message' => wp_strip_all_tags($message),
)
)
);
}
}
}
?>
<script type="text/javascript">
var $a = jQuery.noConflict();
$a(window).load(function(){
$a(".et_test_form").submit(function(e){
e.preventDefault();
var input_data = $a(this).serialize();
$a.ajax({
type: "POST",
url: "<?php echo admin_url('admin-ajax.php') ?>",
data: input_data,
action: 'et_test_plugin_create_post',
success: function(alrt){
$a("#note").ajaxComplete(function(event, request, settings){
if(alrt == "OK"){
result = "Thanks for sending the message.";
$a(".et_test_form").hide();
} else {
result = 'Error';
}
$a(this).html(result);
});
}
});
return false;
});
});
</script>
<?php
return $html;
});
I actually don't know why this returning error. Probably, the wp_ajax_et_test_plugin_create_post
action isn't working in the shortcode.