Formularübermittlung mit AJAX und Bearbeitung der Antwort auf der WordPress-Website
Lesezeit: 5 Minuten
Supreet Singh
Ich habe eine WordPress-Website. Ich habe ein Kontaktformular erstellt und es wird von AJAX gepostet. Hier ist der Code:
<script type="text/javascript">
jQuery(document).ready(function(){
$("#error_box").hide();
$('#contact_form').submit(function(e){
// prevent the form from submitting normally
e.preventDefault();
var na=$("#1").val();
var email2=$("#2").val();
var subject2 = $("#3").val();
var message2 = $("#4").val();
var mydata = "pn2="+na+"&email="+email2+"&subject="+subject2+"&msg="+message2;
alert(mydata);
$("#contact_form").css({"opacity":"0.1"});
$.ajax ({
type: 'POST',
url: $(this).attr.action, // Relative paths work fine
data: mydata,
success: function(){
$("#contact_form").css({"opacity":"1"});
$('#error_box').fadeIn('fast').css({"height": "auto"});
}
});
});
});
</script>
Wenn das Formular gesendet wird, möchte ich, dass die Fehlerbox (#error_box) eine Nachricht gemäß den gesendeten Daten anzeigt, wenn beispielsweise eines der Felder leer ist, sollte es einen Fehler anzeigen oder eine Erfolgsmeldung anzeigen, wenn die Verarbeitung erfolgreich ist und das Formular wurde verschickt. Gibt es eine Möglichkeit, dies zu tun?
[UPDATE]
Hier ist meine contact-form.php-Datei (die Aktion)
jQuery('#contactForm').submit(ajaxSubmit);
function ajaxSubmit(){
var newContactForm = jQuery(this).serialize();
var flag = 0;
jQuery('.require', '#contactForm').each(function(){
var inputVal = jQuery(this).val();
if(jQuery.trim(inputVal) === ""){
flag = 1;
jQuery(this).next().html("Can't be blank");
jQuery(this).next().show();
}
else{
jQuery(this).next().hide();
}
});
if(flag){
return false;
}
jQuery.ajax({
type:"POST",
url: "/wp-admin/admin-ajax.php?action=contactForm",
data: newContactForm,
success:function(data){
jQuery(':input','#contactForm')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
jQuery("#feedback").html(data);
jQuery("#feedback").fadeOut(10000);
},
error: function(errorThrown){
alert(errorThrown);
}
});
return false;
}
Formulardaten in DB speichern und E-Mail senden fügen Sie den folgenden Code in functions.php hinzu
wp_enqueue_script('jquery');
add_action('wp_ajax_addContactForm', 'addContactForm');
add_action('wp_ajax_nopriv_addContactForm', 'addContactForm');
function addContactForm(){
global $wpdb;
$first_name = $_POST['firstName']; $last_name = $_POST['lastName'];
$email = $_POST['email'];
.
.
.
if($wpdb->insert('table_name',array(
'first_name' => $first_name,
'last_name' => $last_name,
'email' => $email,
.
.
.
))===FALSE){
echo "Error";
}
else {
$headers="From: xyz <[email protected]>";
$subject = "Thank you";
$body = "<p>Thank you</p><p>.........</p>";
wp_mail( $email, $subject, $body, $headers);
echo "<div class="success">Thank you for filling out your information, we will be in contact shortly.</div>";
}
exit;
}
Du solltest benutzen:
$.ajax ({
type: 'POST',
url: $(this).attr.action,
data: mydata,
success: function(response) { // here you receive response from you serverside
$("#contact_form").css({"opacity":"1"});
$('#error_box').html(response).fadeIn('fast').css({"height": "auto"});
}
});
Ihre Serveraktion url: $(this).attr.action, sollte eine Nachricht zurückgeben, die eingefügt wird #error_box
Funktioniert nicht. Jetzt taucht die Fehlerbox gar nicht mehr auf. Ist die Antwort der Text, der in der PHP-Datei wiedergegeben wird? Ich habe meine Datei contact-form.php hinzugefügt. Bitte teilen Sie mir mit, ob darin Fehler enthalten sind.