famak te
famak te

Reputation: 25

How to carry out a second function if the first function was successfull using php

Hello i have a php page that makes a post using a webservice, also on the page i am supposed to send an email alert, if the post through the webservice was successfull, my data is successfully posted through the webservice and the email alerts are sent, but i noticed that when there is an error and the webservice dosent post, my email alert is still sent, which i knw is terribly wrong.

this is what i have tried

my webservice code

<?php


    // code to post data here
    require_once('includes/nusoap.php');

$wsdlfile = "https://niid.autoreglive.org/nia_api/service.asmx?wsdl";
$wsdlfile = "https://www.niid.org/NIA_API/Service.asmx?wsdl";

//$wsdlfile = "http://localhost:82/cscart/service/index.php";

 $msg = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>
<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">
<Username>j####.Ukwueze</Username>
<Password>Japh</Password>
<NiaNaicomID>nnn</NiaNaicomID>
<PolicyNo>TP/1/1</PolicyNo>
<InsuredName>Sunky Yaki</InsuredName>
<ContactAddress>Yaba Lagos</ContactAddress>
<GSMNo>08021231234</GSMNo>
<Email>[email protected]</Email>
<EffectiveCoverDate>2013-09-20</EffectiveCoverDate>
<ExpirationDate>2014-09-19</ExpirationDate>
<TypeOfCover>Comprehensive</TypeOfCover
><VehicleCategory>Saloon</VehicleCategory>
<EngineNo>uhdu</EngineNo>
<ChasisNo>dksdj</ChasisNo>
<VehicleColor>green</VehicleColor>
<YearofMake>1999</YearofMake>
<VehicleMake>Toyota</VehicleMake>
<RegistrationNo>gg11jj</RegistrationNo>
<OldRegistrationNo>11122</OldRegistrationNo>
<VehicleType>Saloon</VehicleType>
<EngineCapacity>4</EngineCapacity>
<VehicleModel>Camry</VehicleModel>
<SumAssured>233300</SumAssured>
<Premium>230</Premium>
<CoverNoteNo>1211</CoverNoteNo>
<CertificateNo>test 2</CertificateNo>
<GeographicalZone>North East</GeographicalZone>
</soap:Envelope>
 ";

 $Username = 'kbl';
$Password = 'kb@@@@'; 
$NiaNaicomID = 'NIA000'; 
$PolicyNo = $_POST['Policy_Number']; 
$InsuredName = $_POST['Insured_Name'];
$ContactAddress = $_POST['Residential_Address']; 
$GSMNo = $_POST['Telephone']; 
$Email = $_POST['Email'];
$EffectiveCoverDate = $_POST['Date'];
$ExpirationDate = $_POST['Date_Expiry'];
$TypeOfCover = $_POST['Type_Of_Insurance']; 
$VehicleCategory = 'null'; 
$EngineNo = $_POST['Engine_Number']; 
$ChasisNo = $_POST['Chassis_Number'];
$VehicleColor = $_POST['Colour']; 
$YearofMake = '2004'; 
$VehicleMake = $_POST['Make_Of_Car']; 
$RegistrationNo = $_POST['Registeration_Number']; 
$VehicleType = $_POST['Vehicle_Class']; 
$EngineCapacity = ''; 
$VehicleModel = 'Model'; 
$SumAssured = 2.3; 
$Premium = '2.3'; 
$CoverNoteNo = 'No note';
$CertificateNo = 'No certificate No';
$GeographicalZone = '6';

$params = array('Username' => $Username, 
                                                           'Password' => $Password, 
                                                           'NiaNaicomID' => $NiaNaicomID,
                                                           'PolicyNo' => $PolicyNo,
                                                           'InsuredName' => $InsuredName,
                                                           'ContactAddress' => $ContactAddress, 
                                                           'GSMNo' => $GSMNo, 
                                                           'Email' => $Email,
                                                           'EffectiveCoverDate' => $EffectiveCoverDate,
                                                           'ExpirationDate' => $ExpirationDate,
                                                           'TypeOfCover' => $TypeOfCover,
                                                           'VehicleCategory' => $VehicleCategory, 
                                                           'EngineNo' => $EngineNo, 
                                                           'ChasisNo' => $ChasisNo,
                                                           'VehicleColor' => $VehicleColor,
                                                           'YearofMake' => $YearofMake,
                                                           'VehicleMake' => $VehicleMake, 
                                                           'RegistrationNo' => $RegistrationNo, 
                                                           'VehicleType' => $VehicleType,
                                                           'EngineCapacity' => $EngineCapacity,
                                                           'VehicleModel' => $VehicleModel,
                                                           'SumAssured' => $SumAssured,
                                                           'Premium' => $Premium,
                                                           'CoverNoteNo' => $CoverNoteNo,
                                                           'CertificateNo' => $CertificateNo,
                                                           'GeographicalZone' => $GeographicalZone
                                                    );

$s = new nusoap_client($wsdlfile, 'wsdl');
//$s = new nusoap_client($wsdlfile);
// if (empty($proxyhost))
// {
// }else
// {
//         $s->setHTTPProxy($proxyhost,$proxyport,$proxyusr,$proxypassword);
// }



$result = $s->call('Vehicle_Policy_Push', $params, '', '', false, true);
if($result){
print_r($result);
echo '<META HTTP-EQUIV="Refresh" Content="3; URL=records.php">';





}else{
    echo '<META HTTP-EQUIV="Refresh" Content="3; URL=error.php">';

}






?>

my alert code

<?php
$reply = $_POST['Email'];



    $replysubject = "Auto-Reply: support | kblinsurance.com";
    $replyfrom = "From: [email protected]\r\n";
     $replymessagess .= "You Are Getting This Email.\r\n\r\n";
      $replymessagess .= "Because You Just Purchased A Third Party Insurance.\r\n\r\n";


     $replymessagess .= $_POST["Insured_Name"];
    $replymessagess .= "\r\n";

      $replymessagess .= "\r\n";
     $replymessagess .= "Telephone Number :\r\n";
    $replymessagess .= $_POST["Telephone"];
    $replymessagess .= "\r\n";
     $replymessagess .= "\r\n";
    $replymessagess .= "With Registeration Number :\r\n";
    $replymessagess .= $_POST["Registeration_Number"];
    $replymessagess .= "\r\n\r\n";

    $replymessagess .= "http://www.kblinsurance.com\r\n\r\n";
    $replymessagess .= "This e-mail is automated, so please do not reply.\r\n";
     $replymessagess .= "Regards.\r\n";
    mail($reply,$replysubject, $replymessagess, $replyfrom);
    ?>

i only want the alert to be sent when my webservice post is successfull, any ideas please

Upvotes: 0

Views: 42

Answers (2)

beingalex
beingalex

Reputation: 2476

Try this:

$result = $s->call('Vehicle_Policy_Push', $params, '', '', false, true);

if($result){

    $reply = $_POST['Email'];

    $replysubject = "Auto-Reply: support | kblinsurance.com";
    $replyfrom = "From: [email protected]\r\n";
    $replymessagess .= "You Are Getting This Email.\r\n\r\n";
    $replymessagess .= "Because You Just Purchased A Third Party                   Insurance.\r\n\r\n";


    $replymessagess .= $_POST["Insured_Name"];
    $replymessagess .= "\r\n";

    $replymessagess .= "\r\n";
    $replymessagess .= "Telephone Number :\r\n";
    $replymessagess .= $_POST["Telephone"];
    $replymessagess .= "\r\n";
    $replymessagess .= "\r\n";
    $replymessagess .= "With Registeration Number :\r\n";
    $replymessagess .= $_POST["Registeration_Number"];
    $replymessagess .= "\r\n\r\n";

    $replymessagess .= "http://www.kblinsurance.com\r\n\r\n";
    $replymessagess .= "This e-mail is automated, so please do not reply.\r\n";
    $replymessagess .= "Regards.\r\n";
    mail($reply,$replysubject, $replymessagess, $replyfrom);
}

Just a guess would need more info.

Upvotes: 0

beingalex
beingalex

Reputation: 2476

if (mail($reply,$replysubject, $replymessagess, $replyfrom)) {
    // Your alert here: e.g:
    echo "Successfully sent";
} 

This basically means if the mail function returns true then carry out the alert. You can also check if it wasn't successful:

if (!mail($reply,$replysubject, $replymessagess, $replyfrom)) {
    // Was not successful
    echo "Not successfully sent";
} // Note the !

Or for both successful and unsuccessful:

if (mail($reply,$replysubject, $replymessagess, $replyfrom)) {
    // Your alert here
    echo "Successfully sent";
} else {
    // Your unsucessful alert
    echo "Not successfully sent";
}

http://php.net/manual/en/function.mail.php Return Values ¶

Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.

Upvotes: 1

Related Questions