模态未定义变量,Alert()返回未定义的值

时间:2018-03-09 09:39:02

标签: javascript php mysql

我正在尝试创建一个模态,但总会有一个通知:

Notice: Undefined variable: material_ID in C:\wamp64\www\projman\bootstrap\editmaterialdata.php on line 31 Call Stack #TimeMemoryFunctionLocation 10.0011242576{main}( )...\editmaterialdata.php:0 ">

我在我的列名上尝试了alert(),但它返回了一个未定义的值但是当我在我的sql上测试了该列时,它返回了我想要的值。帮助请求我一直在看这个错误好几个小时,我就是拿不到它。

DataTable和JavaScript

<div class="container" id="subwrapper">
        <h1>Material's Data Table</h1>
        <table id="example" class="display" cellspacing="0" width="100%">
            <thead>
            <tr>
                <th>Material ID</th>
                <th>Material Name</th>
                <th>Material Description</th>
                <th>Unit Price</th>
                <th>Action</th>           
            </tr>
            </thead>
            <tfoot>
            <tr>
                <th>Material ID</th>
                <th>Material Name</th>
                <th>Material Description</th>
                <th>Unit Price</th>
                <th>Action</th>         
            </tr>
            </tfoot>
        </table>

        <div class="modal fade" id="myModal" role="dialog">
            <div class="modal-dialog">
                <div id="content-data"></div>
            </div>
        </div>
    </div>      
   <script>
        var $=jQuery.noConflict();
        $(document).ready(function(){
            var dataTable=$('#example').DataTable({       
                "processing": true,
                "serverSide":true,
                "bLengthChange": false,
                "ajax":{
                    url:"fetchmaterial.php",
                    type:"post"
                }
            });
        });
    </script>

    <!--script js for get edit data-->
    <script>
        $(document).on('click','#getEdit',function(e){
            e.preventDefault();
            var material_ID=$(this).data('materialID');
            alert(material_ID);
            /*$('#content-data').html('');
            $.ajax({
                url:'editmaterialdata.php',
                type:'POST',
                data:'materialID='+material_ID,
                dataType:'html'
            }).done(function(data){
                $('#content-data').html('');
                $('#content-data').html(data);
            }).fial(function(){
                $('#content-data').html('<p>Error</p>');
            }); */  
        });
    </script>

用于编辑数据的PHP代码

<?php
    /**
     for display full info. and edit data
     */
    // start again
    $con=mysqli_connect('localhost','root','','projectmanagementdb');  // this one in error
    if(isset($_REQUEST['materialID'])){
        $id=intval($_REQUEST['materialID']);
        $sql="SELECT * FROM materialinfo WHERE materialID=$id";
        $run_sql=mysqli_query($con,$sql);
        while($row=mysqli_fetch_array($run_sql)){
            $material_ID=$row[0];
            $material_Name=$row[1];
            $material_Description=$row[2];
            $unit_Price=$row[3];
        } //end while 


    ?>
    <form class="form-horizontal" method="post">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Edit Material Information</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <div class="box-body">
                        <div class="form-group">
                            <label class="col-sm-4 control-label" for="txtname">Material ID</label>
                            <div class="col-sm-6">
                                <input type="text" class="form-control" id="txtname" name="txtname" value="<?php echo $material_ID;?>">
                            </div>
                        </div>

                    </div>
                </form>
            </div>
        </div>
    </form>
<?php
}//end if ?>

用于从mysql获取数据的PHP代码

<?php
$con=mysqli_connect('localhost','root','','projectmanagementdb')
    or die("connection failed".mysqli_errno());
$request=$_REQUEST;
$col =array(
    0   =>  'materialID',
    1   =>  'materialName',
    2   =>  'materialDescription',
    3   =>  'unitPrice'
);  //create column like table database

$sql ="SELECT * FROM materialinfo";
$query=mysqli_query($con,$sql);

$totalData=mysqli_num_rows($query);
$totalFilter=$totalData;

//Search function of Jquery Data table
$sql ="SELECT * FROM materialinfo WHERE 1=1";
if(!empty($request['search']['value'])){
    $sql.=" AND (materialID Like '".$request['search']['value']."%' ";
    $sql.=" OR materialName Like '".$request['search']['value']."%' ";
    $sql.=" OR materialDescription Like '".$request['search']['value']."%' ";
    $sql.=" OR unitPrice Like '".$request['search']['value']."%' )";
}
$query=mysqli_query($con,$sql);
$totalData=mysqli_num_rows($query);


//Order function of Jquery Data table
$sql.=" ORDER BY ".$col[$request['order'][0]['column']]."   ".$request['order'][0]['dir']."  LIMIT ". $request['start']."  ,".$request['length']."  ";
$query=mysqli_query($con,$sql);


$data=array();
while($row=mysqli_fetch_array($query)){
    $subdata=array();
    $subdata[]=$row[0]; //materialID
    $subdata[]=$row[1]; //materialName
    $subdata[]=$row[2]; //materialDescription
    $subdata[]=$row[3]; //unitprice         //create onclick event to display mcrypt_module_get_algo_block_size(algorithm)
    $subdata[]= '<button type="button" id="getEdit" class="btn btn-warning btn-xs" data-toggle="modal" data-target="#myModal" data-id="'.$row[0].'"><i class="glyphicon   glyphicon-pencil">&nbsp</i>Edit</button> <button type="button" class="btn btn-danger btn-xs"><i class="glyphicon glyphicon-trash">&nbsp</i>Delete</button>';
    $data[]=$subdata;
}
$json_data=array(
    "draw"              =>  intval($request['draw']),
    "recordsTotal"      =>  intval($totalData),
    "recordsFiltered"   =>  intval($totalFilter),
    "data"              =>  $data
);

echo json_encode($json_data);
?>

0 个答案:

没有答案