I have a little trouble with understanding of recording data to MySQL database. Now, I have some table in my DB showing in Grid. I attached an Editor, but i don't know, how to use it correctly (sending editable data into a php-script). May I ask for help?
Thanks.
view.js
Ext.onReady(function() {
var cols = [
{ dataIndex: 'id', header: 'id', hidden: true },
{ dataIndex: 'title', header: 'Title', width: 200, editor: 'textfield'},
{ dataIndex: 'author', header: 'Author', width: 200, editor: 'textfield' },
{ dataIndex: 'isbn', header: 'ISBN', width: 100, editor: 'numberfield' },
],
fields = [];
for(var i=0; i<cols.length; i++) {
fields.push(cols[i].dataIndex);
}
Ext.define('Book', {
extend: 'Ext.data.Model',
fields: fields
});
var store = Ext.create('Ext.data.JsonStore', {
model: 'Book',
proxy: {
type: 'ajax',
url: 'view.php',
reader: {
type: 'json'
}
}
});
Ext.create('Ext.grid.Panel', {
columns: cols,
width: 520,
style: 'margin-top: 20%; margin-left: 35%',
plugins: [
Ext.create('Ext.grid.plugin.RowEditing', {
clicksToEdit: 2
})
],
renderTo: Ext.getBody(),
store: store
});
store.load();
});
view.php
<?php
require_once 'db.php';
$result = mysql_query('SELECT * FROM books');
while ($row = mysql_fetch_assoc($result)) {
for ($i=0; $i < mysql_num_fields($result); $i++) {
$meta = mysql_fetch_field($result, $i);
}
$rows[] = $row;
}
print (json_encode($rows));
?>
edit.js
Ext.onReady(function(){
Ext.QuickTips.init();
var simpleForm = new Ext.FormPanel ({
labelWidth: 75,
url:'edit.php',
frame:true,
title: 'Add book',
bodyStyle:'padding:5px 5px 0',
width: 350,
defaults: {width: 230},
defaultType: 'textfield',
items: [{
fieldLabel: 'Title',
name: 'title',
allowBlank:false
},{
fieldLabel: 'Author',
name: 'author'
},{
fieldLabel: 'ISBN',
name: 'isbn'
}],
buttons: [{
text: 'Save',
handler: function () {
simpleForm.getForm().submit({
waitMsg: 'Saving...',
success: function () {
Ext.MessageBox.alert ('Message','Data has been saved');
simpleForm.getForm().reset();
},
failure: function () {
Ext.MessageBox.alert ('Message','Saving data failed');
}
});
}
},{
text: 'Cancel',
handler: function () {
simpleForm.getForm().reset();
}
}]
});
simpleForm.render ('simple-form');
});
edit.php - ????????
<?php
require_once 'db.php';
$q=mysql_query ("INSERT INTO books VALUES (null, '".$_POST['title']."','".$_POST['author']."','".$_POST['isbn']."')
") or die ('{"success":"false"}');
// json output to notify the insert is success or not
if ($q) {
echo '{"success":"true"}';
}
else {
echo '{"success":"false"}';
}
?>
Related
I currently new creating a highcart I just want to know if these possible in the highcart. I want to create a cart with 3 categories
3 Categories:
Low Priority
Medium Priority
High Priority
So if my categories is 3 so the bar is 3 also with different name and different datas.
In my console.log the data result shows.
My Output:
My Function Code:
$.getJSON('ajax/ams_sla_report_chart.php', function(data,name){
console.log(data);
var json_data = "";
var json_name = "";
var chart = new Highcharts.Chart({
chart: {
renderTo: 'containers',
type: 'column',
inverted: false
},
legend: {
layout: 'horizontal',
align: 'right',
verticalAlign: 'middle'
},
yAxis: {
title: {
text: 'SLA PERCENTAGE'
}
},
title: {
text: 'Priority Based on SLA'
},
series:[{
name:'Low Priority',
colorByPoint: true,
data:data[0]
},
{
name:'Medium Priority',
colorByPoint: true,
data:data[1]
},
{
name:'High Priority',
colorByPoint: true,
data:data[2]
},
]
});
json_data = chart.series.data = data;
function showValues() {
$('#alpha-value').html(chart.options.chart.options3d.alpha);
$('#beta-value').html(chart.options.chart.options3d.beta);
$('#depth-value').html(chart.options.chart.options3d.depth);
}
// Activate the sliders
$('#sliders_eng input').on('input change', function () {
chart.options.chart.options3d[this.id] = parseFloat(this.value);
showValues();
chart.redraw(false);
});
showValues();
});
Output must be:
Highcharts.chart('container', {
chart: {
type: 'column'
},
title: {
text: 'SLA BASED ON PRIORITY AND PERCENTAGE OF HIT'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: {
categories: [
'Low',
'Medium',
'High',
],
crosshair: true
},
yAxis: {
min: 0,
title: {
text: 'SLA BASED ON PERCENTAGE (mm)'
}
},
tooltip: {
headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
'<td style="padding:0"><b>{point.y}</b></td></tr>',
footerFormat: '</table>',
shared: true,
useHTML: true
},
plotOptions: {
column: {
pointPadding: 0.2,
borderWidth: 0
}
},
series: [{
name: 'CLOSE MR',
data: [49.9, 71.5, 106.4]
}, {
name: 'OPEN MR',
data: [83.6, 78.8, 98.5]
}, {
name: 'TOTAL MR HIT',
data: [48.9, 38.8, 39.3]
}, {
name: 'TOTAL MR HIT AVERAGE',
data: [50, 38.8, 39.3]
}]
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
You need to preprocess your data to the format required by Highcharts:
var data = [
['Low Priority', 100, 9],
['Medium Priority', 100, 2],
['High Priority', 100, 1]
],
categories = [],
series = [];
data.forEach(function(arr) {
arr.forEach(function(el, i) {
if (i === 0) {
categories.push(el);
} else if (series[i - 1]) {
series[i - 1].data.push(el)
} else {
series.push({
data: [el]
});
}
});
});
Highcharts.chart('container', {
...
series: series
});
Live demo: http://jsfiddle.net/BlackLabel/xu6wy910/
I have a login with username and password, and a button with login. I want to send data from username and password to server-side PHP.
Ext.require([
'Ext.form.Panel',
'Ext.layout.container.Anchor'
]);
var log = Ext.onReady(function () {
Ext.create('Ext.form.Panel', {
renderTo: 'login',
title: 'Login section',
bodyPadding: '10 10 0',
width: 300,
fieldDefaults: {
labelAlign: 'top',
msgTarget: 'side'
},
defaults: {
border: false,
xtype: 'panel',
flex: 1,
layout: 'anchor'
},
layout: 'hbox',
items: [{
items: [{
xtype: 'textfield',
fieldLabel: 'User Name',
anchor: '-5',
name: 'first',
id: 'userName'
}, {
xtype: 'textfield',
fieldLabel: 'Password',
anchor: '-5',
name: 'password',
inputType: 'password',
id: 'password'
}]
}
],
buttons: ['->', {
text: 'Login',
name: 'submit',
/*listeners: {
tap: function () {
var form = Ext.getCmp('userName');
//var values = form.getValues();
Ext.Ajax.request({
url: 'index.php',
params: form,
success: function (response) {
var text = response.responseText;
Ext.Msg.alert('asfasfaf', text);
},
failure: function (response) {
Ext.Msg.alert('Error', 'Error while submitting the form');
console.log(response.responseText);
}
});
}
}*/
/* handler: function () {
Ext.Ajax.request({
url: 'index.php',
method: 'POST',
params: Ext.getCmp('userName').getValue(),
success: function (response) {
Ext.Msg.alert('success ' + Ext.getCmp('userName').getValue());
},
failure: function (response) {
Ext.Msg.alert('server-side failure with status code ' + response.status);
}
});
}*/
},
{
text: 'Register?'
}]
});
});
I see that you have already tried to extract values and send request to php. May be this structure will help you. But you have to be sure that your php url accepts parameters with names 'param1' and 'param2' (or whatever your php accepts:) )
{
xtype : 'button',
text : "Submit"
formBind : true,
handler : function() {
var userName = this.up('form').down('#userName');
var password = this.up('form').down('#password');
Ext.Ajax.request({
url: url, // your php url
method: 'POST',
params: {param1: userName, param2:password },
disableCaching: false,
success: function(response, opts)
{
var text = response.responseText; // for debugging print text and decodedText
var decodedText = Ext.decode(text);
if(decodedText.success)
{
}
}
failure: function()
{
}
});
}
}
Ext.getCmp('userName').getValue();
You can use the Ext Form's submit method from within your button handler, e.g.:
// ....
buttons: [{
text: 'Submit!',
handler: function(btn) {
btn.up('form').getForm().submit({
url: 'mybackend.php',
success: function(ret) {},
failure: function(ret) {},
});
}
}],
// .....
Are there any security risks using the jqwidget way of data loading from remote database i.e giving the path of the php file that just echoes the json data?
Here is a sample code
$(document).ready(function () {
var source =
{
datatype: "json",
datafields: [
{ name: 'CompanyName'},
{ name: 'ContactName'},
{ name: 'ContactTitle'},
{ name: 'Address'},
{ name: 'City'}
],
url: 'grid_data.php',
cache: false
};
var dataAdapter = new $.jqx.dataAdapter(source);
$("#jqxgrid").jqxGrid(
{
source: source,
columns: [
{ text: 'Company Name', datafield: 'CompanyName', width: 250},
{ text: 'ContactName', datafield: 'ContactName', width: 150 },
{ text: 'Contact Title', datafield: 'ContactTitle', width: 180 },
{ text: 'Address', datafield: 'Address', width: 200 },
{ text: 'City', datafield: 'City', width: 120 }
]
});
});
I'm trying to do a jquery ajax tutorial but I'm not able to get it to work.
$(document).ready(function () {
//var theme = getDemoTheme();
var url = "http://localhost/schoollife/services/getchapters.php?a=showdata";
// prepare the data
var source =
{
datatype: "json",
datafields: [
{ name: 'curriculam_type', type: 'string' },
{ name: 'class_type', type: 'string' },
{ name: 'subject_type', type: 'int' },
{ name: 'chapter_name', type: 'string' },
{ name: 'chapter_number', type: 'int' },
{ name: 'author_name', type: 'string' },
{ name: 'reviewer_name', type: 'int' }
],
//id: 'id',
url: url
};
var dataAdapter = new $.jqx.dataAdapter(source);
$("#jqxgrid").jqxGrid(
{
width: 940,
source: dataAdapter,
//theme: theme,
columnsresize: true,
columns: [
{ text: 'Curriculum', datafield: 'curriculam_type', width: 100 },
{ text: 'Class', datafield: 'class_type', width: 100 },
{ text: 'Subjects', datafield: 'subject_type', width: 100 },
{ text: 'Chapter', datafield: 'chapter_name', width: 160 },
{ text: 'Chapter Number', datafield: 'chapter_number', cellsalign: 'center',width: 60},
{ text: 'Content Author', datafield: 'author_name'},
{ text: 'Content Reviewer', datafield: 'reviewer_name'},
{ text: 'Edit', datafield: 'Edit', width: 60, cellsrenderer: function () {
return '<div style="width:100%"><img src="../images/edit.png" style="margin-left: 25%"/></div>';
},
},
{ text: 'Delete', datafield: 'delete', width: 60, cellsrenderer:function () {
return "<div style='width:100%'><a href='javascript:void(0)' class='**delbutton**'><img src='../Images/delete.png' style='margin-left:25%'/></a></div>";
},
},
]
});
$(document).ready(function() {
$(".**delbutton**").click(function(){
alert('sdsdsd');
alert('Are You sure you want to delete!');
evt.preventDefault();
var id=this.href;
var url='http://localhost/schoollife/services/getchapters.php?a=deletechapter&chapter_id='+data[i].id;
$.messager.confirm('Confirm','Are you sure you want to delete record?');
$.ajax({
cache:false,
dataType:'json',
url:'http://localhost/schoollife/services/getchapters.php?a=deletechapter&chapter_id='+data[i].id,
async:false,
success:function(data){
alert(data);
}
})
});
});
});
If I do the same in a .js file I wouldn't have any problem. What am I missing here?
Thanks
Well, what about <script> tag:
<script>
//js code here
</script>
You need of course to place it after including jquery.
you have to close }); first document on load function . and consider about open bracket and close bracket
I have a working graph using Highcharts with data from a mySQL database. I need it now to view the lines on different axis so that the chart is better laid out.
Here's my code and JSFiddle for the graph with my data:
http://jsfiddle.net/petenaylor/tGfau/3/
(function($){ // encapsulate jQuery
$(function() {
var seriesOptions = [],
yAxisOptions = [],
seriesCounter = 0,
names = ['Electric', 'Oil', 'Gas'],
colors = Highcharts.getOptions().colors;
$.each(names, function(i, name) {
$(function() {
$.getJSON('http://www.highcharts.com/samples/data/jsonp.php?filename=aapl-c.json&callback=?', function(data) {
// Create the chart
window.chart = new Highcharts.StockChart({
chart : {
renderTo : 'container',
zoomType: 'x'
},
rangeSelector : {
selected : 12
},
title : {
text : 'Energy Prices'
},
series : [{
name : 'Electric',
<?php
$result = mysql_query (" SELECT * FROM energyprices ORDER BY id ASC") or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
extract($row);
$date = strtotime($row['pDate']);
$date = $date."000"; // convert from Unix timestamp to JavaScript time
$data1[] = "[$date, $electric]";
}
?>
data: [<?php echo join($data1, ',') ?>],
tooltip: {
valueDecimals: 2
}
},{
name : 'Oil',
<?php
$result = mysql_query (" SELECT * FROM energyprices ORDER BY id ASC") or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
extract($row);
$date = strtotime($row['pDate']);
$date = $date."000"; // convert from Unix timestamp to JavaScript time
$data2[] = "[$date, $oil]";
}
?>
data: [<?php echo join($data2, ',') ?>],
tooltip: {
valueDecimals: 2
}
},{
name : 'Gas',
<?php
$result = mysql_query (" SELECT * FROM energyprices ORDER BY id ASC") or die (mysql_error());
while ($row = mysql_fetch_array($result)) {
extract($row);
$date = strtotime($row['pDate']);
$date = $date."000"; // convert from Unix timestamp to JavaScript time
$data3[] = "[$date, $gas]";
}
?>
data: [<?php echo join($data3, ',') ?>],
tooltip: {
valueDecimals: 2
}
}]
});
});
});
});
// create the chart when all data is loaded
function createChart() {
chart = new Highcharts.StockChart({
chart: {
renderTo: 'container'
},
rangeSelector: {
selected: 4
},
/*yAxis: {
labels: {
formatter: function() {
return (this.value > 0 ? '+' : '') + this.value + '%';
}
},
plotLines: [{
value: 0,
width: 2,
color: 'silver'
}]
},*/
yAxis: [{ // Primary yAxis
labels: {
formatter: function() {
return (this.value > 0 ? '+' : '') + this.value + '%';
},
style: {
color: '#89A54E'
}
},
title: {
text: 'Electric',
style: {
color: '#89A54E'
}
},
opposite: true
}, { // Secondary yAxis
gridLineWidth: 0,
title: {
text: 'Oil',
style: {
color: '#4572A7'
}
},
labels: {
formatter: function() {
return this.value;
},
style: {
color: '#4572A7'
}
}
}, { // Tertiary yAxis
gridLineWidth: 0,
title: {
text: 'Gas',
style: {
color: '#AA4643'
}
},
labels: {
formatter: function() {
return this.value;
},
style: {
color: '#AA4643'
}
},
opposite: true
}],
plotOptions: {
series: {
compare: 'percent'
}
},
tooltip: {
pointFormat: '<span style="color:{series.color}">{series.name}</span>: <b>{point.y}</b> ({point.change}%)<br/>',
valueDecimals: 2
},
series: seriesOptions
});
}
});
})(jQuery);
As you can see this works as it should, however, I need this to function like the below example. It's important that I have the y axis showing the values and the zoom must work. I have tried to adapt the below code with my own data but I get the issue unresponsive script. I think my data could be too much for the chart to handle.
http://jsfiddle.net/petenaylor/c73u5/5/
$(function () {
var chart;
$(document).ready(function() {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
zoomType: 'xy'
},
title: {
text: 'Average Monthly Weather Data for Tokyo'
},
subtitle: {
text: 'Source: WorldClimate.com'
},
xAxis: [{
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
}],
yAxis: [{ // Primary yAxis
labels: {
formatter: function() {
return this.value +'°C';
},
style: {
color: '#89A54E'
}
},
title: {
text: 'Temperature',
style: {
color: '#89A54E'
}
},
opposite: true
}, { // Secondary yAxis
gridLineWidth: 0,
title: {
text: 'Rainfall',
style: {
color: '#4572A7'
}
},
labels: {
formatter: function() {
return this.value +' mm';
},
style: {
color: '#4572A7'
}
}
}, { // Tertiary yAxis
gridLineWidth: 0,
title: {
text: 'Sea-Level Pressure',
style: {
color: '#AA4643'
}
},
labels: {
formatter: function() {
return this.value +' mb';
},
style: {
color: '#AA4643'
}
},
opposite: true
}],
tooltip: {
formatter: function() {
var unit = {
'Rainfall': 'mm',
'Temperature': '°C',
'Sea-Level Pressure': 'mb'
}[this.series.name];
return ''+
this.x +': '+ this.y +' '+ unit;
}
},
legend: {
layout: 'vertical',
align: 'left',
x: 120,
verticalAlign: 'top',
y: 80,
floating: true,
backgroundColor: '#FFFFFF'
},
series: [{
name: 'Rainfall',
color: '#4572A7',
type: 'column',
yAxis: 1,
data: [49.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}, {
name: 'Sea-Level Pressure',
type: 'spline',
color: '#AA4643',
yAxis: 2,
data: [1016, 1016, 1015.9, 1015.5, 1012.3, 1009.5, 1009.6, 1010.2, 1013.1, 1016.9, 1018.2, 1016.7],
marker: {
enabled: false
},
dashStyle: 'shortdot'
}, {
name: 'Temperature',
color: '#89A54E',
type: 'spline',
data: [7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6]
}]
});
});
});
Can anyone help?
Many thanks
Pete
I think this is what you want: http://jsfiddle.net/mxrhS/
I did it by defining 3 yaxis, and then setting each series to use a different yaxis:
yAxis:[{opposite:false},{opposite:true},{opposite:true,offset:50}],
and
series: [{
name: 'Electric',
yAxis:0,