jQWidgets Forums

jQuery UI Widgets Forums TreeGrid php integration and ajax

This topic contains 3 replies, has 2 voices, and was last updated by  phill.oflynn 10 years, 1 month ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
  • php integration and ajax #72350

    phill.oflynn
    Participant

    I have two php files that call each other to populate a tree grid. The first time works and the grid is populated. And subsequent calls via ajax seems to destroy the session variables. I think ajax is the problem. How do I get around this??

    Php file 1

    <?php 
    if (session_id() == "") 
        session_start();    ?>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <title id="Description">Todo List</title>
        <meta name="description" content="todo List">
        <link rel="stylesheet" href="../css/jqx.base.css" type="text/css" />
        <script type="text/javascript" src="../lib/jquery-1.11.1.min.js"></script>
        <script type="text/javascript" src="../lib/jqwidgets/jqxcore.js"></script>
        <script type="text/javascript" src="../lib/jqwidgets/jqxdata.js"></script>
        <script type="text/javascript" src="../lib/jqwidgets/jqxbuttons.js"></script>
        <script type="text/javascript" src="../lib/jqwidgets/jqxscrollbar.js"></script>
        <script type="text/javascript" src="../lib/jqwidgets/jqxdatatable.js"></script>
        <script type="text/javascript" src="../lib/jqwidgets/jqxtreegrid.js"></script>
    	<script type="text/javascript" src="../lib/jqwidgets/jqxmenu.js"></script>
        <script type="text/javascript">
            $(document).ready(function () {          
     
                // prepare the data
                var source =
                {
                    dataType: "json",
                    dataFields: [
                        { name: "ID",          type: "string" },
                        { name: "mpath",       type: "string" },
                        { name: "ProjectName", type: "string" },
                        { name: "DueDate",     type: "date"},
                        { name: "Priority",    type: "string" },
                        { name: "Notes",       type: "string" }
                    ],
                    hierarchy:
                    {
                        keyDataField: { name: 'ID' },
                        parentDataField: { name: 'mpath' }
                    },
                    id: 'ID',
                    url: './todo_select.php',
    				type: "POST",
    				
    				updaterow: function (rowid, rowdata, commit) {
                        // // synchronize with the server - send update command
                        // var data = "update=true&FirstName=" + rowdata.FirstName + "&LastName=" + rowdata.LastName + "&Title=" + rowdata.Title;
                        // data = data + "&Address=" + rowdata.Address + "&City=" + rowdata.City + "&Country=" + rowdata.Country + "&Notes=''";
                        // data = data + "&EmployeeID=" + rowdata.EmployeeID;
                        // $.ajax({
                            // dataType: 'json',
                            // url: 'data.php',
                            // data: data,
                            // success: function (data, status, xhr) {
                                // // update command is executed.
                                // commit(true);
                            // },
                            // error: function () {
                                // // cancel changes.
                                // commit(false);
                            // }
    						// });
    					},
    					
    				 deleterow: function (rowid, commit) {
    					// synchronize with the server - send delete command
    					alert(rowid);
    					
    					$_SESSION["test"]="test";
    					alert($_SESSION["test"]);
    					
    					$.ajax({
    						dataType: 'json',
    						url: './todo_select.php',
    						type: "POST",
    						data:{deleterow: true, ID: rowid},
    						success: function (data, status, xhr) {
    							alert ("success");
    							alert($_SESSION["test"]);
    							commit(true);
    							},
    						error: function () {
    							//alert ("error");
    							//alert($_SESSION["test"]);
    							commit(false);
    							}
    						});
    					}
    				};
                var dataAdapter = new $.jqx.dataAdapter(source);
                // create Tree Grid
                $("#treeGrid").jqxTreeGrid(
                {
                    width: '100%',
                    height: 600,
                    source: dataAdapter,
    				editable: true,
                    editSettings: { saveOnPageChange: true, saveOnBlur: true, saveOnSelectionChange: true, cancelOnEsc: true, saveOnEnter: true, editSingleCell: true, editOnDoubleClick: true, editOnF2: true },
                    sortable: true,
                    ready: function()
                    {
                        //$("#treeGrid").jqxTreeGrid('expandRow', '34');
    					//$("#error_log").html(alert($_SESSION["test"]));
                    },
    				
                    columns:
                    [
                        { text: 'ID',           dataField: "ID",          align: 'left',   width: '10%'},
                        { text: 'PID',          dataField: "mpath",       align: 'left',   width: '10%'},
                        { text: 'Project Name', dataField: "ProjectName", align: 'left',   width: '50%'},
                        { text: 'DueDate',      dataField: "DueDate",     align: 'right',  width: '10%', cellsformat: 'dd.MM.yyyy'},
                        { text: 'Priority',     dataField: "Priority",    align: 'center', width: '10%'},
                        { text: 'Notes',        dataField: "Notes",       align: 'left',   width: '10%'}
                    ]
                });
    			
                // create context menu
                var contextMenu = $("#Menu").jqxMenu({ width: 200, height: 85, autoOpenPopup: false, mode: 'popup' });
    
                $("#treeGrid").on('contextmenu', function () {
                    return false;
                });
    
                $("#treeGrid").on('rowClick', function (event) {
                    var args = event.args;
                    if (args.originalEvent.button == 2) {
                        var scrollTop = $(window).scrollTop();
                        var scrollLeft = $(window).scrollLeft();
                        contextMenu.jqxMenu('open', parseInt(event.args.originalEvent.clientX) + 5 + scrollLeft, parseInt(event.args.originalEvent.clientY) + 5 + scrollTop);
                        return false;
                    }
                });
    
                $("#Menu").on('itemclick', function (event) {
                    var args = event.args;
                    var selection = $("#treeGrid").jqxTreeGrid('getSelection');
                    var rowid = selection[0].uid
                    if ($.trim($(args).text()) == "Edit Selected Row") {
                        $("#treeGrid").jqxTreeGrid('beginRowEdit', rowid);
                    } else if ($.trim($(args).text()) == "Delete Selected Row") {
                        $("#treeGrid").jqxTreeGrid('deleteRow', rowid);
                    } else if ($.trim($(args).text()) == "Insert Row") {
                        $("#treeGrid").jqxTreeGrid('addRow', null, {}, 'first', null);
                    }
                });
            });
        </script>
    </head>
    <body class='default'>
        <div id="treeGrid"></div>
    	    <div id='Menu'>
            <ul>
                <li>Edit Selected Row</li>
                <li>Delete Selected Row</li>
                <li>Insert Row</li>
            </ul>
        </div>
    	<div id="error_log"></div>
    </body>
    </html>
    

    Php file 2

    <?php 
    if (session_id() == "") 
        session_start();    ?>
    <?php 
    
    $DBServer = $_SESSION["DBServer"];
    $DBUser   = $_SESSION["DBUser"];
    $DBPass   = $_SESSION["DBPass"];
    $DBName   = 'project_manager';
    
    header("Pragma: no-cache");
    header("Expires: 0");
    header("Content-type: application/octet-stream");
    
    $conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
     
    // check connection
    if ($conn->connect_error) {
      trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
    }
    
    //$sql='SELECT * FROM projects_query';
    $sql='CALL <code>ProjectHandler</code>(NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,'.$_SESSION["UserId"].', NULL,0)';
    $_SESSION["test"]=$sql;
    
    if (isset($_POST['update'])) {
    	// UPDATE COMMAND 
    	// $update_query = "UPDATE <code>employees</code> SET <code>FirstName</code>='".$_POST['FirstName']."',
    	// <code>LastName</code>='".$_POST['LastName']."',
    	// <code>Title</code>='".$_POST['Title']."',
    	// <code>Address</code>='".$_POST['Address']."',
    	// <code>City</code>='".$_POST['City']."',
    	// <code>Country</code>='".$_POST['Country']."',
    	// <code>Notes</code>='".$_POST['Notes']."' WHERE <code>EmployeeID</code>='".$_POST['EmployeeID']."'";
    	 // $result = mysql_query($update_query) or die("SQL Error 1: " . mysql_error());
         // echo $result;
    } else if (isset($_POST['deleterow'])) {
    	// DELETE COMMAND 
    		$delete_query = 'CALL <code>ProjectHandler</code>("'.$_POST["ID"].'",NULL,NULL,NULL,NULL,NULL,NULL,NULL,'.$_SESSION["UserId"].', NULL,1024)';
    		//$_SESSION["test"]=$delete_query;
    		//$_SESSION["test"]="test1";
    
    		$rs=$conn->query($delete_query);
    		echo $rs;
    } else {
        // SELECT COMMAND
    		$rs=$conn->query($sql);
    			
    		$rs->data_seek(0);
    		$data = array();
    		while($row = $rs->fetch_assoc()){ $data[] = $row; }
    
    		$rs->close();
    		$conn->close();
    		print json_encode($data);
    }
    
    ?>
    
    php integration and ajax #72373

    admin
    Keymaster

    Hi phill.oflynn,

    I don’t think that it’s OK to define such variables in the JavaScript. If you want to send data to your PHP, then use the Ajax call’s data parameter.

    Best Regards,
    Peter Stoev

    jQWidgets Team
    http://www.jqwidgets.com

    php integration and ajax #72431

    phill.oflynn
    Participant

    Not sure what you mean

    php integration and ajax #72966

    phill.oflynn
    Participant

    The issue is with this line
    data:{deleterow: true, ID: rowid},
    should be
    data:{‘deleterow’: true, ‘ID’: rowid},

    fiddled around a bit more. Peter I see your point with the session variables. They were only for testing anyway. I would never expose session variables so readily.

    Phill

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.