Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

275
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda