Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
Reading data not working in CRUD style table. PHP Ajax, SQL Server

I am trying to implement a table that I can dynamically add data into with ajax, I have the insert working and it's going into my database fine, but I can seem to get the data to display on the page.

These are my ajax functions:

<script>
    function saveData(){
    var qid = '25';
    var ccode = '0123';
    var pname = $('#prodName').val();
    var pcode = $('#prodCode').val();
    var banda = $('#bandA').val();
    var nprice = $('#newPrice').val();
    $.ajax({
        type: "POST",
        url: "quote2.php?p=add",
        data: "prodName="+pname+"&prodCode="+pcode+"&bandA="+banda+"&newPrice="+nprice,
        success: function(data){
            viewData();
        }
    });
    }
    function viewData(){
        $.ajax({
            type: "GET",
            url: "quote2.php",
            success: function(data){
                $('tbody').html(data);
            }
        });
    }
</script>

This is my php script:

<?php 
    $page = isset($_GET['p'])?$_GET['p']:'';
    if($page == 'add'){
        $quoteID = '25';
        $customerCode = '0123';
        $productCode = $_POST['prodCode'];
        $productName = $_POST['prodName'];        
        $bandA = $_POST['bandA'];
        $nprice = $_POST['newPrice'];

        $query = "INSERT INTO po_special_price_products (QuoteID, CardCode, SlpCode, SlpName, BandA, NewPrice) 
                            VALUES ('$quoteID', '$customerCode', '$productCode', '$productName', '$bandA', '$nprice')"; 
        $stmt = sqlsrv_prepare($sapconn2, $query); 
        if (sqlsrv_execute($stmt) === false) { 
            die(print_r(sqlsrv_errors(), true)); 
        } 
    }else{
        $query = "SELECT * FROM po_special_price_products WHERE QuoteID = '$quoteID'"; 
        $stmt = sqlsrv_prepare($sapconn2, $query);
        $result = sqlsrv_execute($stmt);
        while($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
    ?>
        <tr>
            <td><?php echo $row['ID'];?></td>
            <td><?php echo $row['QuoteID'];?></td>
            <td><?php echo $row['CardCode'];?></td>
            <td><?php echo $row['SlpCode'];?></td>
            <td><?php echo $row['SlpName'];?></td>
            <td><?php echo $row['BandA'];?></td>
            <td><?php echo $row['NewPrice'];?></td>
            <td>
                <button>edit</button>
            </td>
        </tr>
    <?php } 
    }
?>

But when I load the page the table is empty, can anyone help?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

If i understand your php code correctly, You need to change this line in php script

$query = "SELECT * FROM po_special_price_products WHERE QuoteID = '$quoteID'";

to

if(empty($quoteID) || !isset($quoteID)){
  $query = "SELECT * FROM po_special_price_products "
} else {
  $query = "SELECT * FROM po_special_price_products WHERE QuoteID = '$quoteID'";
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!