I created some "post-cards" on my website with each having a form to allow users to enter an email address. I need to find a way to remove the email input field once a user clicks the submit button and replace it with a p tag. I am also using evt.currentTarget so that I only target the selected "post-card". I need to make sure that when I replace the input field with a p tag I replace the only ones within the selected target.
my code below...
<div class="post-share">
<span class="share-btn">Share</span>
<form name="<?php echo the_title(); ?>" class="post-share-form" action="">
<label>Share via email</label><br>
<input title="<?php echo the_title(); ?>" type="email" link="<?php the_permalink(); ?>" required name="email" placeholder="[email protected]"><br>
<div class="flex flex-submit">
<label class="form-summary"></label>
<input type="submit" value="Submit" /><i class="fa fa-spinner fa-spin disable"></i>
</div>
</form>
</div>
function handleShareFormSubmit(evt) {
evt.preventDefault();
var $emailInput = $(evt.currentTarget).find("input[type=email][name=email]"),
$postTitle = $emailInput.attr("title"),
$postLink = $emailInput.attr("link"),
//Getting serialized form data
$formData = $(evt.currentTarget).serializeArray();
$.ajax({
url: home_params.ajaxurl,
data: {
action: 'send_email',
formData: $formData,
postTitle: $postTitle,
postLink: $postLink,
},
type: 'POST',
beforeSend: function(xhr) {
//do stuff to show user form is in motion
//add a spinner
$(evt.currentTarget).find('i.fa.fa-spinner.fa-spin').removeClass('disable');
},
success: function(response) {
//handle the response
console.log(response);
if(response) {
console.log(response);
if(response.status === 200) {
$emailInput.val('Thank you!');
console.log($postTitle + $postLink);
} else {
console.log("Failed to submit. Status not 200 :(");
$(evt.currentTarget).find("label.form-summary").text("Error.").addClass('error').delay(6000).slideToggle();
}
}
$(evt.currentTarget).find('i.fa.fa-spinner.fa-spin').addClass('disable');
$(evt.currentTarget).find("label.form-summary").text("Success! Thank you!").addClass('success').delay(6000).slideToggle();
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
console.log(textStatus);
console.log(errorThrown);
$(evt.currentTarget).find('i.fa.fa-spinner.fa-spin').addClass('disable');
}
//look into aftersend ajax function to add (??)
});
}
$(function(){
$('.post-share-form').on('submit', handleShareFormSubmit);
});