I would have thought that by using setResizable( false ); I could have still changed the size of the main window without changing the size of the content pane but instead I noticed that I can't even resize the java window anymore. I guess this is what it does :-)
ok thanks.
thanks guys. I am not too worried about this, but I have decided to do what stultuske suggested (I have tried to set a maximum size adding these lines
Dimension maximumSize = new Dimension(900, 600);
calculatorLayoutTest.setMaximumSize( maximumSize );//set dimensions
but as JamesCherrill said no joy!) so I eventually found a way to disable resizing altogether adding setResizable(false);
(I found this solution on the net). That seems to do the trick :-)!
/*CalculatorLayoutTest.java
Testing CalculatorLayout class
*/
import javax.swing.JFrame;
import java.awt.Dimension;//to sort specify the dimension
public class CalculatorLayoutTest{
public static void main( String[] args ){
CalculatorLayout calculatorLayoutTest = new CalculatorLayout();
calculatorLayoutTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//calculatorLayoutTest.setSize( 400, 200 );
calculatorLayoutTest.setVisible( true );
Dimension minimumSize = new Dimension(400, 200);//specify dimensions
calculatorLayoutTest.setMinimumSize( minimumSize );//set dimensions
calculatorLayoutTest.setResizable( false );
/*Dimension maximumSize = new Dimension(900, 600);
calculatorLayoutTest.setMaximumSize( maximumSize );//set dimensions */
calculatorLayoutTest.pack();
}//end of main
}//end of CalculatorLayoutTest class
Brilliant, I will bear that in mind.
Yes I did try to resize it and - I don't know whether this is desiderable or not - the keys and the text field grow/shrink with the window, becoming excessively big/small...
Hello all, I wonder if you can help me with this. I wanted to create a page which, among the other things, has a div with a lot of text inside. Below this div there will be 2 buttons. When viewed on a mobile, the height of this div become fixed to fit the screen and the content inside becomes scrollable so that the two buttons below the div are always visible. That's all done with media queries and it works absolutely fine except for the fact that users will never know that the content in the div is scrollable because - I have just realized - mobile devices won't show scrollbars by default and in fact not even during the scrolling. I have done a bit of googling and nobody seems to suggest any pure CSS/HTML solution, maybe because there isn't. Is anybody aware of any CSS/HTML fix? If not what would be a good reliable and fairly easy to integrate plug-in that will achieve that?
Here is a screenshot from android, as you can see no scrollbars
Code-wise, it is a simple html page
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Test</title>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="contentWrapper">
<div class="contentConstrained">
<div class="content">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam nec magna dui. Proin eget lacinia augue. Quisque vel sagittis tortor. Duis fringilla magna sit amet ullamcorper posuere. Suspendisse suscipit interdum nibh. Nullam pulvinar porta nulla, …
ahhhhh, OK, now things are becoming clearer.
Done and works:
/*CalculatorLayout.java
ex 14.9 p 662
*/
import java.awt.BorderLayout;//arrange the components
import java.awt.GridLayout;//layout for controller
import javax.swing.JFrame;//provides basic window feature
import javax.swing.JPanel;//for the panels
import javax.swing.JTextField;//for the txt field
import javax.swing.JButton;//for the keypad
public class CalculatorLayout extends JFrame{
private BorderLayout layout;//layout object. Do I need a flowlayout at all?
private JPanel numberKeysPanel;//contains the number keys panel
//private JPanel calculationAreaPanel;//contains the calculation panel
private GridLayout numberKeysGrid;//contains the number keys
private JTextField calculationArea;//text field where numbers are displayed
private JButton numberKey;//symbols
private String[] labels;//hold the labels
//constructor
public CalculatorLayout(){
super( "Calculator" );
layout = new BorderLayout( 0, 5 );//create FlowLayout. Do I need a flowlayout at all?
setLayout( layout );
// calculationAreaPanel = new JPanel();
calculationArea = new JTextField( "", 30 );//created empty display
//calculationAreaPanel.add( calculationArea ); //add the text field to the jpanel
numberKeysPanel = new JPanel();
String[] labels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+" };
numberKeysGrid = new GridLayout( 4, 4, 5, 5 );//create grid layout
for( int counter = 0; counter < labels.length; counter++ ){
numberKey = new JButton( labels[ counter ] );//create button with apppropriate label
numberKeysPanel.add( numberKey );//add buttons to panel
}//end of loop
numberKeysPanel.setLayout( numberKeysGrid );
//set up GridLayout manager
//numberKeysPanel.add( numberKeysGrid );//add grid to jpanel
add( calculationArea, BorderLayout.PAGE_START );//add calc area jpanel to jframe
add( numberKeysPanel, BorderLayout.CENTER );//add jpanel to the jframe
}//end of constructor
}//end of CalculatorLayout class
And I …
Apologies, yes you're right I forgot to use the lower-case for the variable, thanks for pointing that out, I will amend
thanks JamesCherrill, yes it is really simplistic you're right, in fact I don't think I will ever use it, but because I am still at the very early stage of GUI I thought I'd go through a few different layout manager, no matter how easy/useless they are. This is just so I get to grips with the whole GUI thing.
I added a min size and that seems to have done the trick for reasons that I don't quite understand as yet
/*testing FlowLayoutManager.java
FlowLayoutManagerTest.java
*/
import javax.swing.JFrame;
import java.awt.Dimension;//to sort specify the dimension
public class FlowLayoutManagerTest{
public static void main( String args[] ){
FlowLayoutManager FlowLayoutManagerTest = new FlowLayoutManager();
FlowLayoutManagerTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//FlowLayoutManagerTest.setSize( 400, 200 );
Dimension minimumSize = new Dimension(400, 250);//specify dimensions
FlowLayoutManagerTest.setMinimumSize( minimumSize );//set dimensions
FlowLayoutManagerTest.setVisible( true );
FlowLayoutManagerTest.pack();
}//end of main
}//end of class
Thanks for the link, very useful!
Hi guys, I tought I'd experiment a bit with layout managers, trying to get to grips with them. I will start with a flowLayout, then hopefully move on to more complex ones.
OK, so here is the code for the first example, very simple one:
/*FlowLayoutManager.java
layout manager tests*/
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
public class FlowLayoutManager extends JFrame{
private JTextField textField1;
private JTextField textField2;
private JCheckBox checkBox1;
private JCheckBox checkBox2;
private FlowLayout layout;
public FlowLayoutManager(){
super( "Flow Layout" );
//creating and setting layout for controllers
layout = new FlowLayout();
setLayout( layout );
//creating controllers
textField1 = new JTextField( "", 10 );
textField2 = new JTextField( "", 10 );
checkBox1 = new JCheckBox( "checkbox 1" );
checkBox2 = new JCheckBox( "checkbox 2" );
//attaching controllers to JFrame window
add( textField1 );
add( textField2 );
add( checkBox1 );
add( checkBox2 );
}//end of constructor
}//end of class
and test class
/*testing FlowLayoutManager.java
FlowLayoutManagerTest.java
*/
import javax.swing.JFrame;
public class FlowLayoutManagerTest{
public static void main( String args[] ){
FlowLayoutManager FlowLayoutManagerTest = new FlowLayoutManager();
FlowLayoutManagerTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
//FlowLayoutManagerTest.setSize( 400, 200 );
FlowLayoutManagerTest.setVisible( true );
FlowLayoutManagerTest.pack();
}//end of main
}//end of class
This produces this layout:
Now, it's OK but the height of the window is not reakky what I want. How do I increase that to a more user friendly size?
Also, you will notice in the FlowLayoutManagerTest.java that I have commented out //FlowLayoutManagerTest.setSize( 400, 200 );
Although …
thanks for your help guys.
@JamesCherrill before I close this thread I would like to pick up on something you said:
setSize is call for when you're not using a layout manager.
Can you actually draw a GUI without using a layout manager? I thought that a layout manager had to be used at all times.
Thanks
Right, I think I am a bit confused and ended up with the below:
I think I need to get this all GUI thing cleared in my head. Let's start with my code perhaps.
In the calculator program I have created created 2 jpanels, one for the keys and one for the text field. The keys are in a GridLayout so that I can add columns and rows. The textfield is not in a GridLayout, because I thought uit's not necessary, but seeing the results perhaps it needs to be?
Here is the code:
/*CalculatorLayout.java
ex 14.9 p 662
*/
import java.awt.FlowLayout;//arrange the components
import java.awt.GridLayout;//layout for controller
import javax.swing.JFrame;//provides basic window feature
import javax.swing.JPanel;//for the panels
import javax.swing.JTextField;//for the txt field
import javax.swing.JButton;//for the keypad
public class CalculatorLayout extends JFrame{
private FlowLayout layout;//layout object. Do I need a flowlayout at all?
private JPanel numberKeysPanel;//contains the number keys panel
private JPanel calculationAreaPanel;//contains the calculation panel
private GridLayout numberKeysGrid;//contains the number keys
private JTextField calculationArea;//text field where numbers are displayed
private JButton numberKey;//symbols
private String[] labels;//hold the labels
//constructor
public CalculatorLayout(){
super( "Calculator" );
layout = new FlowLayout();//create FlowLayout. Do I need a flowlayout at all?
setLayout( layout );
calculationAreaPanel = new JPanel();
calculationArea = new JTextField( "", 20 );//created empty display
calculationAreaPanel.add( calculationArea ); //add the text field to the jpanel
numberKeysPanel = new JPanel();
String[] labels = { "7", "8", "9", "/", "4", "5", "6", "*", "1", "2", "3", "-", …
thanks, although the exercise doesn't require the acton listener, which is a good idea because I can concentrate on the layout. I have found an exercise which involves action listeners and it is a bit more complicated, so I will do that at a later stage.
I will keep the thread open in case I get stuck when I build the calculator.
thanks
Hello,
me again :-). This time I need to build a small calculator-like GUI, like this:
This time I thought I'd ask a few questions before I start building it!
1)In terms of layout, I was thinking to use the GridLayout again: one for the text field and one for the buttons. Now, do I need a to include a FlowLayout as well or can I leave it out?
2)the buttons: it seems to me to be a bit tedious to add 16 different JButton variables, each of them for a different button as required. So, would it be doable to have an array of buttons and insert them in the panel with a loop perhaps? The only thing is, how would I assign the correct labels to the button if I add them in a loop?!
thanks
Hello peeps,
I hope you can help me with this.
I have just bought a new “high capacity” battery for my gt-i900 android phone (it currently has a 1500mAh battery), a 3700mAh. Now, what should I do the first time? I was thinking to leave the battery in charge for about 16 hours (phone off of course), but is it the right thing to do, or should I charge it till it is completely charge (probably 2-3 hours) and then switch my phone on and use it normally?
OK, so essentially I have to use pack()
. I don't want to sound thick but what's the point of setting constraints like guiTest.setSize( 400, 200 );
if then pack()
will override them? So, have I corrected implemented the pack()
method in the GuiTest.java? I was a bit unsure of that.
but a setMinimumSize should be honoured.
I take that by minimum size you mean that the panel will be as big as the content inside it.
I don't think it is absolutely imperative for me to get it perfect, at the end of the day I am not enrolled in any course or doing coursework, I am just learning JAVA in my own time so as long as I understand how tHings work I am more than happy :-)!
I appreciate that the GridLayout, BorderLayout and FlowLayout are very basic layout managers, but I think that I am happy to work with them for the time being: this was my first very basic GUI attempt so I would like to learn a bit more about these before moving to more complicated layout like GridBagLayout.
I know I could use an IDE and save me all the work but I think it is useful to understand how to code them from scratch, at least a basic understanding
Hello, I think I am getting somewhere, although it's not perfect as yet.
This is what I have now:
As you can see there are some problems in there: there is too little distance between the textfields (where the 8's are) and the buttons and too much between the labels (x and y) and the text fields. I have played around with the parameters in the gridLayout constructor but I don't seem to be able to get the right conbination (if that's the problem) to get the same layout as in the brief.
I have added (I believe) the gridLayout manager for the panels, I am not sure if I have implemented it correctly though. Here's what I have done.
1)declared 3 new gridLayout objects:
//3 grid layouts for 3 different types of controllers
private GridLayout checkboxGrid;
private GridLayout textfieldsGrid;
private GridLayout buttonsGrid;
2)Then in the constructor I set up the 3 grids:
//set up layout manager for checkbox panels
checkboxGrid = new GridLayout( 0, 1 );//1 col and as many rows as needed
checkBoxesPanel.setLayout( checkboxGrid );//set the layout of the jpanel?
//set up layout manager for textfields panels
textfieldsGrid = new GridLayout( 0, 2, 0, 5 );//2 col and as many rows as needed
textFieldsPanel.setLayout( textfieldsGrid );//set the layout for the jpanel?
//set up layout manager for textfields panels
buttonsGrid = new GridLayout( 0, 1, 5, 10 );//1 col and as many rows as needed
buttonsPanel.setLayout( buttonsGrid );//set …
Right, I have made some changes, but I must have missed something really obvious just for a change.
So:
1)I now have 3 JPanels for 3 different types of controllers
2)the checkboxes are in the first panel, the text fields in the second and the buttons in the third
3)I have attached the three types of controllers to the relevant JPanel and then the JPanels to the JFrame.
One thing though is that I am not too sure whether I am supposed to keep the flowLayout manager or not. I left it there but I believe I am not using it?
The position of the elements hasn't changed. Sorry maybe I misunderstood byt I thought that the elements would have been placed in a vertical stack automatically. Here is a screenshot of how it looks like after updating the code
And here is the updated code:
/*Exercise 14.8 p 662. Create a GUI as described
create Gui.java
-checkboxes
-buttons
-text fields
*/
import java.awt.FlowLayout;//arrange the components
import javax.swing.JFrame;//basic window feature
import javax.swing.JTextField;//text fields
import javax.swing.JButton;//display the buttons
import javax.swing.JCheckBox;//for checkboxes
import javax.swing.JLabel;//for labels
import javax.swing.JPanel;//for the JPanel
public class Gui extends JFrame{
private JCheckBox snapCheckbox;//snap to grid checkbox
private JCheckBox showGridCheckBox;//show grid checkbox
private JLabel xLabel;//X
private JLabel yLabel;//Y
private JTextField xTextField;//x text
private JTextField yTextField;//y text
private JButton okButton;// OK
private JButton cancelButton;//Cancel
private JButton helpButton;//Help
private FlowLayout layout;//layout object
private JPanel checkBoxesPanel;//panel object of JPanel type …
Thanks JamesCherrill. I didn't know I could do that, so you mean like having a JFrame as I currently do and then inside 3 JPanels.
How is a panel different from a JFrame?
I will have a go and then post back
Hello guys,
I hope you can help me with this.
I have read a bit about GUI and now I am trying to do a few exercises to consolidate what I have read.
In the first exercise I have to reproduce a simple GUI. Well I thought it was simple…
Anyway, I have attached a screenshot (exercise.png)
that shows what I have to do.
The result of my code is instead shown in the second screenshot (result.png)
As you can see I didn’t quite manage to group the elements together and place them in the correct order.
Now, let me say that this GUI business isn’t incredibly clear to me and this was my first attempt. I have used a FlowLayout layout manager probably because it is the simplest one and I thought I could achieve that format with it. With hindsight though, maybe that’s not the best option. From the little I know, FlowLayout arranges the elements one after the other till the space runs out, whereas I want them to be arranged in 3 separate groups I reckon and inside the group, one above the other one as in the exercise screenshot
Does anybody have any suggestion? I am not looking for code, I’d like to give it another go first and try to understand the whole thing before admitting defeat.
Which layout manager should I use?
…
ok got it, thank you very much for your help!
thanks JorgeM, that does work. But then, why mine doesn't?! Is it just because the value is a string and I need to parse it? it would appear so...
Hi, this usage of css() is very well documented on the jquery site, so it should work, it might be that I am doing something wrong
well yes it accepts 2 values, one of which can be a function that takes "index" and "value". Now, neither index or value - if you look at my previous example - shouldn't be declared I don't think...
ah I see, so they are essentially the same thing, and yes the second one extends a JPanel
Hi guys, I have come across something really confusing, so I wonder if somebody can help me with that.
I wanted to increment the width of a container using this version of the .css()
method:$( "div.example" ).css( "width", function( index )
as per http://api.jquery.com/css/#css-propertyName-functionindex--value
So here is my html:
<div class="myDiv"> </div>
and my css for that container
.myDiv {
background: url("overlayArrow_03.png") no-repeat scroll -20px 0 rgba(0, 0, 0, 0);
border: 1px solid #0000FF;
height: 150px;
left: 35px;
position: absolute;
top: 217px;
width: 300px;
}
OK. So this version of a jquery script does the trick:
$(".myDiv").css("width", function(index){
return index + 120;
});
although the final width of the container is now 120px and not 420 as expected. I take that's because the value of index is 0, so it would make sense.
On the jquery site, they also say that the version of the above funcion should be instead
$( "div.example" ).css( "width", function( index, value )
that applied to my case is
$( "div.example" ).css( "width", function( index, value )
return value + 120;
});
but alas, it doesn't work. It doesn't cause an error and the width of the container remains 300px.
Any idea please?
thanks
Fab, thanks for all your help!
hi guys, I wonder if anybody can clarify something for me.
Take these 2 fragments of code (2 constructors belonging to 2 different applications):
First one:
public MouseTrackerFrame()
{
super( "Demonstrating Mouse Events" );
mousePanel = new JPanel(); // create panel
mousePanel.setBackground( Color.WHITE ); // set background color
add( mousePanel, BorderLayout.CENTER ); // add panel to JFrame
statusBar = new JLabel( "Mouse outside JPanel" );
add( statusBar, BorderLayout.SOUTH ); // add label to JFrame
// create and register listener for mouse and mouse motion events
MouseHandler handler = new MouseHandler();
mousePanel.addMouseListener( handler );
mousePanel.addMouseMotionListener( handler );
} // end MouseTrackerFrame constructor
private class MouseHandler implements MouseListener,
MouseMotionListener
...
Second one:
public MouseDetailsFrame()
{
super( "Mouse Clicks and Buttons" );
statusBar = new JLabel( "Click the mouse" );
add( statusBar, BorderLayout.SOUTH );
addMouseListener( new MouseClickHandler() ); // add handler
} // end MouseDetailsFrame constructor
private class MouseClickHandler extends MouseAdapter{
...
Now, the first one is registering the handlers like so:
MouseHandler handler = new MouseHandler();
mousePanel.addMouseListener( handler );
mousePanel.addMouseMotionListener( handler );
with an object of type JPanel (mousePanel
) that calls addMouseListener method.
In the second example the handler is registered only with addMouseListener( new MouseClickHandler() ); // add handler
...so what is addMouseListener
method applied to if there is no object that is used to call the method?!
I hope what I mean is clear,
Hi JorgeM, thanks for all the info and your patience :-)!
OK, I guess one of the mistakes I was doind was to try to add another row on submit. Assuming I am posting to the same page, I take that if I add a row and then submit the form even if the added rows disappear they are still there somewhere?
thanks
oh fantastic, thanks!
thanks mKorbel, yes unfortunately I am very far from having an excellent knowledge of java Essential Classes (wherever they are ... oops!).
I think I have found a list of event listeners... http://docs.oracle.com/javase/tutorial/uiswing/events/api.html but it doesn't have the components though...
thanks, I think I will look for this table then because 1)there are no events there, 2) looking up the method seems a bit confusing, for example, for the JCheckBox there are a few addxxxListener methods and also - as you suggested - the inherited ones, and I am not sure which one it is that I can/should use.
thanks
Hi all,
yes still a question about the API! I am looking into event handling at the moment (basic things with text fields, checkboxes etc ) and I have noticed that different components generates different events which are in turn handled by different objects. As an example,let's take a checkbox. When a users makes a selection, the check box generates a ItemEven
which is handled by a ItemListener
. Now, I obviously will never know what component generates which event (and which object handles that event), so I presume that the best way to know that is looking up on the API. I did just that but there is nowhere here http://docs.oracle.com/javase/7/docs/api/javax/swing/JCheckBox.html saying that when a user interacts with a checkbox an ItemEvent
occurs. How am I supposed to know/ find out that? and where?
thanks
thanks JorgeM. Sorry maybe I am making this extremely complicated. I am not sure why I am finding so difficult to explain what I want to do :-)! OK, so I think I will start changing my .php file and trying to do one by one all the things I want.
So - please bear with me - to start with, I'd like to add a row to the table that contains the tds and input fields where the user input will be inserted.
If this is the wrong approach then please let me know.
Its OK for JavaScript to add elements to the page. If your script adds input elements, make sure that you include an ID attribute and Name attribute on those input elements so you can access them from the PHP code like you would any other input element within the form.
So, I used javascript to do that but I am having problems, because when I hit submit, since the php page calls itself on submission and therefore 'reload' the page, the row is never inserted. However I have added the ids and names as you have suggested.
this is the HTML (I haven't added any php code as yet)
<!DOCTYPE html>
<html>
<head>
<title>This is a test</title>
<link rel="stylesheet" type="text/css" href="styles.css" >
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<form action="overtime.php" method="post" id="theForm">
<table>
<tr>
<th>Week ending</th>
<th>Total week hours worked</th>
<th>Week overtime available</th>
<th>Total overtime</th>
<th>Comments</th>
</tr> …
oh...and so it is! thanks!
Hi all, as some of you might know the API is my nightmare. I know it's important and therefore I am trying to use it as much as I can. Today I am looking at a program that uses buttons to change the font of some text.
So, I have this line:texField.setFont( new Font( "Serif", Font.PLAIN, 14) );
I looked in the API first for the setFont method in http://docs.oracle.com/javase/7/docs/api/javax/swing/JTextField.html and found this:
public void setFont(Font f)
Sets the current font. This removes cached row height and column width so the new font will be reflected. revalidate is called after setting the font.
Overrides:
setFont in class JComponent
Parameters:
f - the new font
See Also:
Component.getFont()
which is great. Then I moved to the Font class http://docs.oracle.com/javase/7/docs/api/java/awt/Font.html but what I was hoping to find was something like new Font(parameter1, parameter2, parameter3) which I did:
public Font(String name,
int style,
int size)
but I am not sure this is what I am looking for, and I think this is because of my inability to search throught the API (the second parameter in the code above - Font.PLAIN surely can't be an int!?)
I know this isn't a question as such, more a rant really, but I just find it very difficult to find my way across the API...
JorgeM, I have had a really good look and played a bit with your code. I think things are clearer now, so thanks for the practical demonstration.
I want to practice a little more and modify my own PHP page, and made me wonder...I have seen how the php code and javascript can happily coexist, and in my case - I don't know if you remember - my script also created some HTML after hitting the submit button. You have showed me how to display the data returned by the php file. So, the problem for me now is that if my javascript creates the new table where the data returned by the PHP file will go, how do I get that data in the right place if the javascript and php happen at different times? So this is what happens:
Scenario 1:)I click the submit button and my script creates the variables and the new HTML table. Then the PHP returns some values. I could store those values in PHP variables but presumably I will need another script that runs after the PHP file, takes those variables and put them in the right place (ie in the table).
Scenario 2) I create another table in the html, make it invisible with css, then my script will make sure that after the submit button has been pressed the hidden table becomes visible again, the PHP returns the values but I still need to put them into some …
thanks guys, sorry for the delay.
@noelthefish I have noticed that if I change that to a button tag the PHP file doesn't work anymore, no idea why, happy yo try again
@JorgeM: I don't necessarily need a second button I don't think because the application will be used just by me. Anyway thanks for your code, I will play around with that a bit and let you know how things go :-)
thanks
thank you, things are a little clearer now :-).
Sorry for the code formatting, something must have gone a bit wonky there (I think it's the indentation of the text file that caused problems ).
Anyway, I sort of experimented a bit with retrieving data from the php file, it seems reasonable enough. Further from what you were saying, I changed my form page from HTML to PHP extension and post back to itself like so:
overtime.php
<!DOCTYPE html>
<html>
<head>
<title>This is a test</title>
<link rel="stylesheet" type="text/css" href="styles.css" >
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<form action="overtime.php" method="post" id="theForm">
<table>
<tr>
<th>Week ending</th>
<th>Total week hours worked</th>
<th>Week overtime available</th>
<th>Total overtime</th>
<th>Comments</th>
</tr>
<tr class="editable">
<td class="editableField"><input type="text" class="date" placeholder="Date" name="the_date"></td>
<td class="editableField"> <input type="text" class="hoursWorked" placeholder="Hours worked" name="the_hours_worked"></td>
<td class="overtimeAvailable"><input type="hidden" class="overtimeInput"></td>
<td class="totalOvertime"><input type="hidden" class="totalOvertimeInput"></td>
<td class="editableField"> <textarea type="text" class="comments" placeholder="Insert comment" name="the_comments"></textarea></td>
</tr>
</table>
<!-- <button>Submit</button> -->
<input type="submit" text="Submit" />
</form>
</body>
</html>
Now, my javascript isn't really doing any animation as such, all it's doing is to add some HTML (so a bit more complex than calculation). If possible, I would like it to do it clientside simply because I am a bit more confident with this. Here is the script in a better format (hopefully):
$(document).ready(function(){
var totalOvertime = 0;
$("button").click(function(){
var tableRow;
var date;
var hoursWorked;
var overtimeAvailable;
var comment;
date = $("input.date").val();
//console.log(date);
hoursWorked = parseFloat($("input.hoursWorked").val());
overtimeAvailable = hoursWorked - 37.5;
totalOvertime += …
thanks. Sorry one more question so I get this clear in my head. After the form is submitted, the page action.php is displayed. Now, I want the html page to change not the action.php. I want to have what the user has input to be displayd permanently on the HTML page so should I do something similar to this and update the php page with the code that is on the HTML page?
action.php (which works nicely although I don't think this is the way forward)
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" type="text/css" href="styles.css" >
</head>
<body>
<table>
<tr>
<th>Week ending</th>
<th>Total week hours worked</th>
<th>Week overtime available</th>
<th>Total overtime</th>
<th>Comments</th>
</tr>
<tr class="editable">
<td class="editableField">Date:<?php echo $_POST["the_date"]; ?></td>
<td class="editableField">Hours worked:<?php echo $_POST["the_hours_worked"]; ?></td>
<td class="overtimeAvailable"><input type="hidden" class="overtimeInput"></td>
<td class="totalOvertime"><input type="hidden" class="totalOvertimeInput"></td>
<td class="editableField">Comment:<?php echo $_POST["the_comments"]; ?></td>
</tr>
</table>
</body>
</html>
Also, I take that what my js script does (create a table on the fly) is not needed anymore. So what should my js script do? Will PHP take care of this aspect or does it somehow take the data and injects it into the HTML page?
Oh sorry I didn't mean to be annoying about the error, I just noticed and so I mentioned it.
Oh by the way I figured out what was wrong with my code.
1)the virgin media server seems to have problems with PHP. I transferred the page to a different domain and it worked, the 500 error disappeared. What was preventing the PHP page to bring back the content was the button. The moment I replaced the <button> tag with an <input type="submit"> the PHP page worked a treat and brought back the values. Why this is I don't know, I am not skilled enough in PHP.
SO, I will play around a bit with this, and leave this thread open, I am sure I will post back
thanks
thanks for that. I wonder what I got wrong on mine then..well nevermind, although yours returned the same error a few times, it got better only after a few crtl f5. Anyway, I will play around with that and try to fix mine too. So from what you are saying I understand that I can use php variables and javascript ones at the same time?
Hi, Hi thanks for the info.
I had a quick look at the PHP guide, Is there any topic in particular that I should look up in there?
I also had a look at the database discussion...there is quite a bit of code that I don't understand...anyway.
I had a go at what you suggested.
I have created a php file action.php:
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" type="text/css" href="styles.css" >
</head>
<body>
Date:<?php echo $_POST["the_date"]; ?>
Hours worked:<?php echo $_POST["the_hours_worked"]; ?>
Comment:<?php echo $_POST["the_comments"]; ?>
</body>
</html>
I have changed the HTML code accordingly:
...
<form action="action.php" method="post" id="theForm">
<table>
<tr>
<th>Week ending</th>
<th>Total week hours worked</th>
<th>Week overtime available</th>
<th>Total overtime</th>
<th>Comments</th>
</tr>
<tr class="editable">
<td class="editableField"><input type="text" class="date" placeholder="Date" name="the_date"></td>
<td class="editableField"> <input type="text" class="hoursWorked" placeholder="Hours worked" name="the_hours_worked"></td>
<td class="overtimeAvailable"><input type="hidden" class="overtimeInput"></td>
<td class="totalOvertime"><input type="hidden" class="totalOvertimeInput"></td>
<td class="editableField"> <textarea type="text" class="comments" placeholder="Insert comment" name="the_comments"></textarea></td>
</tr>
</table>
<button>Submit</button>
</form>
...
And the javascript:
...
$(".editable").before(tableRow);
/* $(".overtimeAvailable").each(function(){
totalOvertime = totalOvertime + overtimeAvailable;
$(".totalOvertime").html(totalOvertime + " hrs");
}); */
$("input, textarea").val("");//clear input
$("#theForm").submit();
});
});
Now, I have tried at work and the action.php page doesn't display the data returned...not sure why, everything seems to be as it should.
I have tried at home http://antobbo.webspace.virginmedia.com/overtime/overtime.html and I get a 500 PWP error that I can't explain because I am fairly sure V irgin serves do support PHP(I have used it before.)
Hi JorgeM, thanks for that. Yes, it sounds good to me.
I have amended the html to include the form and I will now play around with posting data and retrieving it as per your example. I will come back to you soon.
In general terms, I know ery very little about SQL - and PHP for that matter - I just played around with a few statements time and time ago, but happy to give it a go but definitely need some help.
One more question before I set off. In my PHP file, do I need any doctype, anything at all, or can I just start the way you did with a body tag?
thanks
Ah sorry, forgot to ask. SOme of the fields are not input fields, like the <td
class="overtimeAvailable"> </td>
<td class="totalOvertime"></td>
even if they sit inside a form. Do they have to be converted in input fields or can they remain tds when it comes down to send/retrive data from the server?
Hi guys, I have an HTML page that I built and at the moment the content on it gets changed via javascript. What I would like to achieve is to change the content permanently on this page (which will be uploaded to the server).
Now, I must admit that I have very little experience with PHP but I use javascript on a regular basis and I know a bit of JAVA too, I hope these skills might be useful. The only experience with PHP I had was ages ago when I built a small form.
So, here is the code, it might be useful to see it I think:
HTML
<!DOCTYPE html>
<html>
<head>
<title>This is a test</title>
<link rel="stylesheet" type="text/css" href="styles.css" >
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<table>
<tr>
<th>Week ending</th>
<th>Total week hours worked</th>
<th>Week overtime available</th>
<th>Total overtime</th>
<th>Comments</th>
</tr>
<tr class="editable">
<td><input type="text" class="date"></td>
<td> <input type="text" class="hoursWorked"></td>
<td class="overtimeAvailable"> </td>
<td class="totalOvertime"></td>
<td> <textarea type="text" class="comments"></textarea></td>
</tr>
</table>
<button>Submit</button>
</body>
</html>
Javascript
$(document).ready(function(){
var totalOvertime = 0;
$("button").click(function(){
var tableRow;
var date;
var hoursWorked;
var overtimeAvailable;
var comment;
date = $("input.date").val();
//console.log(date);
hoursWorked = parseFloat($("input.hoursWorked").val());
overtimeAvailable = hoursWorked - 37.5;
totalOvertime += overtimeAvailable;
comment = $(".comments").val();
console.log(comment);
console.log("hours " + typeof hoursWorked + " overtime " + typeof overtimeAvailable );
tableRow = '<tr>' +
'<td class="date">' + date + '</td>' +
'<td class="hoursWorked">' + hoursWorked + '</td>' +
'<td class="overtimeAvailable">' + overtimeAvailable + '</td>' +
'<td class="totalOvertime">' …
Oh I see, sorry I have never heard of HTML5 localstorage, just looked it up now. It sounds interesting, but I would like to explore first the PHP option, and see how complicated is to achieve what I want.
thanks guys
thanks. Now, sorry a bit confused about the terminology, when you say local storage what do you mean? DO you mean literally save the file onto my machine and change the html every time I need to update it, right?
it's just for me really but I would like to put it on our servers and have the ability to input my data using the input field rathen than changing the html everytime (in a previous version I did just that). For that I take I need a serverside language?
thanks
Hi guys,
I was wondering if you can help me at all. I've just built a small html page which is meant to keep track of overtime. The data is entered through input fields. Then with jquery I extract the data from the input fields, create and populate a table. I want to make these changes permanent, meaning that I would like to have the ability to close the page, reopen it and see the data input previously. I take that's not achievable with jquery and I need php?
any idea?
Here is the code so you get an idea.
HTML
<!DOCTYPE html>
<html>
<head>
<title>This is a test</title>
<link rel="stylesheet" type="text/css" href="styles.css" >
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
<table>
<tr>
<th>Week ending</th>
<th>Total week hours worked</th>
<th>Week overtime available</th>
<th>Total overtime</th>
<th>Comments</th>
</tr>
<tr class="editable">
<td><input type="text" class="date"></td>
<td> <input type="text" class="hoursWorked"></td>
<td class="overtimeAvailable"> </td>
<td class="totalOvertime"></td>
<td> <textarea type="text" class="comments"></textarea></td>
</tr>
</table>
<button>Submit</button>
</body>
</html>
Script
$(document).ready(function(){
var totalOvertime = 0;
$("button").click(function(){
var tableRow;
var date;
var hoursWorked;
var overtimeAvailable;
var comment;
date = $("input.date").val();
//console.log(date);
hoursWorked = parseFloat($("input.hoursWorked").val());
overtimeAvailable = hoursWorked - 37.5;
totalOvertime += overtimeAvailable;
comment = $(".comments").val();
console.log(comment);
console.log("hours " + typeof hoursWorked + " overtime " + typeof overtimeAvailable );
tableRow = '<tr>' +
'<td class="date">' + date + '</td>' +
'<td class="hoursWorked">' + hoursWorked + '</td>' +
'<td class="overtimeAvailable">' + overtimeAvailable + '</td>' +
'<td class="totalOvertime">' + totalOvertime + '</td>' +
'<td …
thanks pritaeas, yes I thought about that already, I was kind of hoping to find a different tool :-)!