ajax post在多行表中没有刷新

时间:2018-02-26 06:11:10

标签: php jquery ajax

我获得第二个选择选项所需的$ pgcode值,但表单已刷新,我不想要。我还尝试添加一些html div,但在这种情况下,表单会复制一些内容并刷新。

如何在不刷新的情况下完成这项工作?

<?php

if (session_id() == '') {
    session_start();
    ob_start();
}
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', '1');

include("dbc.php");

if (isset($_POST['model'])) {
    $pgcode = $_POST['model'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="js/jquery-1.10.1.min.js" type="text/javascript"></script>
<script>
        $('#dataTable').on('change','.select-desc',function(){
        var cur_val = $(this).val();
        $.ajax({
            method:"POST",
            url:"cashtran.php",
            data:{model:cur_val},
            success:function(result){
                $('body').html(result);
                    },

        });
    });
});
</script>
</head>
<body id="top">
<br class="clear" />
<div class="wrapper col4">
  <div id="container">
    <div id="content">
        <form method="post" name="frmcashtran" action="">
        <h3><span class="orange">Cash Payment Details</span></h3>
        <fieldset>
            <table>
                <tr>
                    <td><input type="button" value="Add Row" id="addRow" /></td>
                    <td><input type="button" value="Remove Row" id="deleteRow"/></td>
                </tr>
            </table>
            <table id="dataTable" border="0">
                <tr>
                   <td></td>
                   <td><label>Main A/c</label></td>
                   <td><label>Subledger</label></td>
                   <td><label>Narration</label></td>
                   <td><label>Amount</label></td>
                </tr>
                <tr>
                       <td>
                       <?php
$sql = "SELECT gcode,acname FROM account ";
$result = mysqli_query($conn, $sql) or die(mysqli_error());
echo "<select name='acname[]' class='select-desc' tabindex='2'>";
echo "<option value=''>-- Select Main A/c --</option>";
while ($row = mysqli_fetch_array($result)) {
    echo "<option value = '{$row['gcode']}'";
    if ($pgcode == $row['gcode'])
        echo "selected = 'selected'";
    echo ">{$row['acname']}</option>";
}
echo "</select>";
?>
                       </td>
                       <td>
                       <?php
$sql = "SELECT scode,sname FROM subldg where gcode='$pgcode' ";
$result = mysqli_query($conn, $sql) or die(mysqli_error());
echo "<select name='slname[]'  tabindex='2'>";
echo "<option value=''>-- Select Subledger--</option>";
while ($row = mysqli_fetch_array($result)) {
    echo "<option value = '{$row['scode']}'";
    if ($pscode == $row['scode'])
        echo "selected = 'selected'";
    echo ">{$row['sname']}</option>";
}
echo "</select>";
?>
                        </td>
              </table>
        </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

如果在响应ajax请求时发回整个页面,请更改以下PHP代码

if (isset($_POST['model'])) {
    $pgcode = $_POST['model'];
}

if( isset( $_POST['model'] ) ) {
    ob_clean();
    $pgcode = $_POST['model'];
    exit( $pgcode );
}