0

I am doing a newsletter sign up via DotMailer and have been able to get it to add the email address and region to DotMailer but when I try and get it to return a success/invalid message at the end, it just says invalid despite it going through successfully.

I know it's linked to the $success function but hit a brick wall, if anyone can help?...

PHP

$username = "username_here"; //Your API username
$password = "password_here";  //your API password
$client = new SoapClient("http://apiconnector.com/api.asmx?WSDL"); 
//Instantiate the Soap client
$addressbookid=id_here;

$email = $_POST["email"];   
$Expo2017 = $_POST["region"];
$AudienceType="B2B";
$OptInType="Single";
$EmailType="Html";

$keys = array("EXPO2017");
$var2 = new SoapVar($Expo2017,XSD_STRING,"string","http://www.w3.org/2001/XMLSchema");
$values = array($var2);
$Datafields = array ('Keys'=>$keys,'Values'=>$values);
$contact = array ("Email"=>$email,"AudienceType"=>$AudienceType,"OptInType"=>$OptInType,"EmailType"=>$EmailType,"ID"=>-1,"DataFields"=>$Datafields);
$params = array ("username"=>$username,"password"=>$password,"contact"=>$contact,"addressbookId"=>$addressbookid);
return $client->AddContactToAddressbook($params);

$success = "success";

// redirect to success page
if ($success){
   echo "success";
}else{
    echo "invalid";
}

jQuery

jQuery(".footer_form").submit(function(event){
    // cancels the form submission
    event.preventDefault();
    submitForm();
});

function submitForm(){
    // Initiate Variables With Form Content
    var region = $(".footer_form_select").val();
    var email = $(".footer_form_input").val();

    // console.log(region);
    // console.log(email);

    jQuery.ajax({
        type: "POST",
        url: "../wp-content/themes/expo/php/form-process.php",
        data: "region=" + region + "&email=" + email,
        success : function(text){
            if (text == "success"){
                formSuccess();
            } else{
                formError();
            }
        }
    });
}


function formError(){
    jQuery( ".form_message_failed" ).removeClass( "hidden" );        
}

    function formSuccess(){
    jQuery( ".form_message_success" ).removeClass( "hidden" );
}

All tips and help useful, thanks!

Sam
  • 41
  • 1
  • 8

1 Answers1

0

First point you have to know that the code mentioned after the "return" statement in your php will not be traited !
Second point, which type of data returns the function AddContactToAdressBook ? You con add a console.log(text); just before your test in succes function jquery to see.
If boolean, you can do in jquery in success function:

success : function(text){
        if (text){
            formSuccess();
        } else{
            formError();
        }
    }
Gothiquo
  • 841
  • 1
  • 8
  • 31
  • Sorry not coming back sooner, got pulled off this and on to something else. I added the console.log(text); to the jquery but got back the following which seemed confusing.. – Sam Jul 25 '17 at 14:52
  • Fatal error: Uncaught SoapFault exception: [soap:Server] Server was unable to process request. ---> The Email field is required. ERROR_CONTACT_INVALID in C:\xampp\htdocs\expo\wp-content\themes\expo\php\form-process.php:19 Stack trace: #0 C:\xampp\htdocs\expo\wp-content\themes\expo\php\form-process.php(19): SoapClient->__call('AddContactToAdd...', Array) #1 C:\xampp\htdocs\expo\wp-content\themes\expo\php\form-process.php(19): SoapClient->AddContactToAddressbook(Array) #2 {main} thrown in C:\xampp\htdocs\expo\wp-content\themes\expo\php\form-process.php on line 19 – Sam Jul 25 '17 at 15:07