i have the following php files.

<?php
    require_once '../moonlight/includes/mysql_connect.php';
    require_once '../moonlight/includes/functions.php';
    require_once 'includes/reader.php';
/**
 * @author FreeUser
 * @copyright 2013
 */

    if ( isset( $_POST['submitted'] ) ) {
        $errors = array();

        if ( $_POST['authorityID'] == "" ) {
            $errors[] = 'Select the paying authority.';
        } else {
            $authorityID = (int)$_POST['authorityID'];
        }

        if ( $_POST['periodPayableID'] == "" ) {
            $errors[] = 'Select the period payable.';
        } else {
            $periodPayableID = (int)$_POST['periodPayableID'];
        }

        /** $tempLoc = $_FILES['receipt']['tmp_name']; // the file name in the temporary location
        if ( !$tempLoc ) {
            $errors[] = 'Please select the file.';
        } else {

        } */

        if ( empty( $_FILES['receipt']['tmp_name'] ) ) {
            $errors = array();
        } else {
            $tempLoc = $_FILES['receipt']['tmp_name']; // the file name in the temporary location
            $file = $_FILES['receipt']['name'];
            $newFile = 'schedules/' . $file;
            $fileArray = explode( '.', $file );
            $fileExt = end( $fileArray );                     
            if ( file_exists( $newFile ) ) {
                $errors[] = 'The file already exists.';
            } else {
                $moveResult = move_uploaded_file( $tempLoc, $newFile );
                if ( $moveResult ) {
                    $data = new Spreadsheet_Excel_Reader();
                    $data->setOutputEncoding('CP1251'); // set output encoding
                    $data->read( $newFile );
                    for ( $i = 2; $i <= $data->sheets[0]['numRows']; $i++ ) {
                    // Step 1 - pick the variables in each row
                        $nationalID = $data->sheets[0]['cells'][$i][1];
                        $name = ucwords( $data->sheets[0]['cells'][$i][2] );
                        $amountPaid = $data->sheets[0]['cells'][$i][3];
                        $payDate = $data->sheets[0]['cells'][$i][4];
                        $ECNumber = $data->sheets[0]['cells'][$i][5];
                        $pNumber = trim( $data->sheets[0]['cells'][$i][6] );
            $policyNumber = checkPolicyNumber( $pNumber );         
                        // get the variables to use
            $authority = getAuthorityCode( $authorityID ); // get the authority code of the
            $periodName = getPeriodName( $periodPayableID );
            $receiptNumber = $authority . $periodName;
            // Step 2 - check if the policyNumber is in the database
            $sql = "SELECT * FROM policydetails WHERE policyNumber = '$policyNumber'";
            $result = mysql_query( $sql );
            if ( mysql_num_rows( $result ) == 0 ) { // policy number not found
                $sql = "INSERT INTO tblssberrors ( dateAdded, ECNumber, fullName, IDNumber, policyNumber, premium, receiptNumber ) ";
                $sql .= "VALUES ( now(), '{$ECNumber}', '{$name}', '{$nationalID}', '{$policyNumber}', {$amountPaid}, '{$receiptNumber}' )";
                $result = mysql_query( $sql ) or die( "Could not execute query: <br />$sql <br />" . mysql_error() );
            } else {
                $ipAddress = @gethostbyname( $_SERVER_NAME ); // get the IP Address of the client machine
                $paymentModeID = getPaymentModeID( 'Stop order' );
                $firstPayDate = getFirstPayDate( $policyNumber ); // assign the value returned by getFirstPayDate() to $firstPayDate
                if ( is_null( $firstPayDate ) ) { // no previous payment for this policy
                mysql_query( "SET AUTOCOMMIT = 0" ); // do not automatically commit queries
                mysql_query( "START TRANSACTION" ); // start a MySQL transaction
                $sql = "UPDATE tblpolicydetails SET firstPayDate = curdate(), policyStatusID = 2, coverdate = DATE_ADD( curdate(), INTERVAL 60 DAY), ";
                $sql .= "dateLastChanged = curdate(), lastChangedUserID = 2 WHERE policyNumber = '{$policyNumber}'";
                $result = mysql_query( $sql );

                $sql1 = "INSERT INTO tblpayments ( actualPayment, amountPaid, dateCaptured, endPeriodID, ";
                $sql1 .= "IPAddress, monthsPaid, payingAuthority, paymentDate, paymentModeID, policyNumber, "; 
                $sql1 .= "premium, receiptNumber, scheduleNumber, sheetNumber, startPeriodID, timeCaptured, userID) ";
                $sql1 .= "VALUES ( {$amountPaid}, {$amountPaid}, now(), {$periodPayableID}, '{$ipAddress}' ";
                $sql1 .= ", 1, {$authorityID}, '{$payDate}', {$paymentModeID}, '{$policyNumber}', {$amountPaid}, ";
                $sql1 .= "'{$receiptNumber}', '{$receiptNumber}', '{$receiptNumber}', {$periodPayableID}, ";
                $sql1 .= "curtime(), 2 )";
                $result1 = mysql_query( $sql1 );

                if ( $result && $result1 ) { // both queries executed successfully
                    mysql_query( "COMMIT" );
                } else {
                    mysql_query( "ROLLBACK" );
                }
                } elseif ( !is_null( $firstPayDate ) ) {
                $sql = "INSERT INTO tblpayments ( actualPayment, amountPaid, dateCaptured, endPeriodID, ";
                $sql .= "IPAddress, monthsPaid, payingAuthority, paymentDate, paymentModeID, policyNumber, "; 
                $sql .= "premium, receiptNumber, scheduleNumber, sheetNumber, startPeriodID, timeCaptured, userID) ";
                $sql .= "VALUES ( {$amountPaid}, {$amountPaid}, now(), {$periodPayableID}, '{$ipAddress}' ";
                $sql .= ", 1, {$authorityID}, '{$payDate}', {$paymentModeID}, '{$policyNumber}', {$amountPaid}, ";
                $sql .= "'{$receiptNumber}', '{$receiptNumber}', '{$receiptNumber}', {$periodPayableID}, ";
                $sql .= "curtime(), 2 )";
                $result = mysql_query( $sql );
                }
            }
            }
                } else {
                    $errors[] = 'File could not be processed.';
                }
            }
        }
    }
?>
<html>
    <head>
        <title>Electronic Data Interchange</title>
        <style> 
            .error {
                color: #FF0000;
                font-weight: bold;   
            }
        </style>
        <script>
            function showResults( policynumber ) {
                if ( policynumber == "" ){
                    document.getElementById( "txtHint" ).innerHTML = "";
                    return;
                }

                if ( window.XMLHttpRequest ) {// code for IE7+, Firefox, Chrome, Opera, Safari
                    xmlhttp=new XMLHttpRequest();
                } else { // code for IE6, IE5
                    xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP" );
                }
                xmlhttp.onreadystatechange = function() {
                    if ( xmlhttp.readyState == 4 && xmlhttp.status == 200 ) {
                        document.getElementById( "txtHint" ).innerHTML = xmlhttp.responseText;
                    }
                }

                xmlhttp.open( "GET", "getpaymenthistory.php?pnumber="+policynumber,true );
                xmlhttp.send();
            }
        </script>
    </head>
    <body>
        <h3>Electronic Data Interchange</h3>
        <?php
            if ( isset( $errors ) && is_array( $errors ) ) {
                echo '<ul>';
                foreach( $errors as $error ) {
                    ?>
                        <li class="error"><?php echo $error; ?></li>
                    <?php
                }
                echo '</ul>';
            }
        ?>
        <form method="post" action="" enctype="multipart/form-data" >
            <table>
                <tr>
                    <td><b>Select File:</b></td>
                    <td><input type="file" name="receipt" /></td>
                    <td><b>Paying Authority</b></td>
                    <td>
                        <select name="authorityID">
                            <option value="">Select paying authority</option>
                            <?php
                                $sql = "SELECT authorityID, payingAuthorityName AS authority FROM tblpayingauthorities WHERE EDI = 'yes'";
                                $result = mysql_query( $sql );
                                while( $row = mysql_fetch_array( $result ) ) {
                                    if ( isset( $_POST['authorityID'] ) && ( $_POST['authorityID'] ==  $row['authorityID'] ) ) {
                                        $selectedAuthority = ' Selected ';
                                    } else {
                                        $selectedAuthority = '';
                                    }
                                    ?>
                                        <option value="<?php echo $row['authorityID']; ?>"<?php echo $selectedAuthority; ?>><?php echo $row['authority']; ?></option>
                                    <?php
                                }
                            ?>
                        </select>
                    </td>
                </tr>  
                <tr>
                    <td><b>Period Payable:</b></td>
                    <td>
                        <select name="periodPayableID">
                            <option value="">Select period payable</option>
                            <?php
                                $sql = "SELECT periodID, periodName FROM tblperiods ORDER BY periodID";
                                $result = mysql_query( $sql );
                                while( $row = mysql_fetch_array( $result ) ) {
                                    if ( isset( $_POST['periodPayableID'] ) && ( $_POST['periodPayableID'] == $row['periodID'] )  ) {
                                        $selectedPeriod = ' Selected ';
                                    } else {
                                        $selectedPeriod = '';
                                    }
                                    ?>
                                        <option value="<?php echo $row['periodID']; ?>"<?php echo $selectedPeriod; ?>><?php echo $row['periodName']; ?></option>
                                    <?php
                                }
                            ?>
                        </select>
                    </td>
                </tr>    
                <tr>
                    <td colspan="4" align="right">
                        <input type="submit" value="Process" />
                        <input type="hidden"  name="submitted" />
                    </td>
                </tr>      
            </table>
        </form>
        <form>
            <p><b>Search Policy Number:</b>&nbsp;&nbsp;<input type="text" name="txtSearchPolicyNumber" id="txtSearchPolicyNumber" onblur="showResults( this.value);" /></p>
        </form>
        <div id="txtHint"></div>
    </body>
</html>

and

<?php

    /**
    * @author FreeUser
    * @copyright 2013
    */
    require_once '../../moonlight/includes/mysql_connect.php';
    $policyNumber = $_GET['pnumber']; // assign the $_GET['pnumber'] to $policyNumber

    $sql = "SELECT * FROM paymentshistory WHERE policyNumber = '{$policyNumber}' ORDER BY dateCaptured DESC";
    $result = mysql_query( $sql ) or die( mysql_error() . "<br />$sql" );

    if ( mysql_num_rows( $result ) > 0 ){
        echo '<table><tr>
                <td><b>Policy Number</b></td>
                <td><b>Name</b></td>
                <td><b>Amount Paid</b></td>
                <td><b>Actual Payment</b></td>
                <td><b>Receipt Number</b></td>
                <td><b>Paying Authority</b></td>
                <td><b>Period</b></td>
                <td><b>User</b></td>';
        while( $row = mysql_fetch_array( $result ) ) {
                echo '<tr>';
                echo '<td>' . $row['policyNumber'] . '</td>';
                echo '<td>' . $row['client'] . '</td>';
                echo '<td>' . $row['amountPaid'] . '</td>';
                echo '<td>' . $row['actualPayment'] . '</td>';
                echo '<td>' . $row['receiptNumber'] . '</td>';
                echo '<td>' . $row['authorityName'] . '</td>';
                echo '<td>' . $row['periodName'] . '</td>';
                echo '<td>' . $row['user'] . '</td>';
                echo '</tr>';
        } 
        echo '</table>';
    }


?>

the first file has a text box that uses an ajax function for the payment history of the policynumber supplied in the text box. the problem is i'm not getting anything when i search. where am i getting it wrong?

Recommended Answers

All 2 Replies

Have you tried calling the function with onblur then get the value to pass?

<input type="text" name="txtSearchPolicyNumber" id="txtSearchPolicyNumber" onblur="showResults();" />

When you perform the "onBlur", it will call the function, get the value and check to see if its blank.

function showResults() {
    var policynumber = document.getElementById("txtSearchPolicyNumber").value;
    if ( policynumber == "" ){
        //test
        alert("no number");
        document.getElementById("txtHint").innerHTML = "";
        return;                                
    }
// and the rest of the code..
}

Also, in your php, just try running the sql statement without the table headers and just echo a single row. Just for testing purposes

require_once '../../moonlight/includes/mysql_connect.php';
$policyNumber = $_GET['pnumber']; 
// assign the $_GET['pnumber'] to $policyNumber

$sql = "SELECT * FROM paymentshistory WHERE policyNumber = '$policyNumber' ORDER BY dateCaptured DESC";
$result = mysql_query( $sql ) or die( mysql_error() . "<br />$sql" );
while( $row = mysql_fetch_array( $result ) ) {
    echo $row["policyNumber"];
}

what i would also try doing is type in the url code with a policy number to see if there are any errors on the php page.
somewebsite.com/getpaymenthistory.php?pnumber=12345

if i hard code the policy number and run the getpaymenthistory i get the intended results. the problem is with the AJAX function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.