My form has a 'save' button and 'select from previous month' button. what 'save' button does is insert all the values into my database.

save button code:

    $basic = $_POST['basic'];
    $allowance = $_POST['allowance'];

        require "connect.php";
        $total=($basic + $allowance);
        $query=("INSERT INTO january (basic,allowance) VALUES ('$basic','$allowance')");
        echo ("<b>Please fill out the entire form.</b>");

and also when the user clicks on the save button the values that they have already inserted in the text boxes are still in the text box via the value attribute :

<input type="text" name="allowance" size="11" placeholder="0" value="<?php if(isset($_POST['allowance'])){echo htmlentities($_POST['allowance']);} ?>">

but now im trying to set up the 'select from previous month' button. and what i want it to do is retrieve data from the previous month and display the data inside the specific text box of the present month. for example i have a february form and when i click on the select from previous month the data from january database will be displayed in the february form according to specific text boxes. How do i achieve that?

i hope im explaining this right. :s if not tell me.

Recommended Answers

All 6 Replies

Personally I would add a little more of your code on here to give a better picture of what you already have and what you are trying to achieve.

Therefore I will take a little guess as to what you are doing.


        require "connect.php";

        $userid = $_GET['userid']; <-- this is passed by the JQuery
        $lastmonth = some function to calculate last month
        $query=("SELECT basic, allowance + anyother fields you want FROM $lastmonth WHERE some condition probably $userid?");
        while ($row = mysql_fetch_assoc($lastmonthdata)) {
            $content = array('basic'=>$row['basic'],'allowance'=>$row['allowance']);
        echo json_encode($content);




        $.getJSON('lastmonth.php', {"userid":$userid}, function(data)



    <form id="whatever">
        <input id="basic">
        <input id="allowance">
        <button id="lastmonth">Last Month</button>


This is the basic outline of what I think you are trying to do.

you probably need to adjust your database schema a little bit. With current login you will probably have 12 tables, one for each month, and every table will contain atmost one record.

At the very least, add an additional column month to your table that currently has basic and allowance colums. lets call this table salary for the time being. Also you should consider adding a column like userid to associate each record with a particlar user as suggested by @noelthefish

Next, add a hidden field to your form with name like currentmonth and pre populate it with the number of current month. Change the name of your save button to something generic like save instead of jansave . suppose you have named the button that will get the previuos months record as getlastmonth

Now in your form processing code:

if (isset($_POST['save'])) {
// save all fields to the dataabse except the `currentmonth`
else if (isset($_POST['getlastmonth'])) {
// run a query like this
// SELECT * FROM salary WHERE month = ( $_POST['currentmonth'] - 1)

Here I am suggesting that you save only an integer number representing the month in the database. it will allow you to do the relative queries ( records for previous month as in this example ) easily.

You can manage a maping a numbers to month names in your PHP code like this

$months = array(1=>'January', 2=>'Fabruary' ...);

And then you can use this mapping to translate month numbers to their corresponding names for showing to the users.

I hope this lengthy explanation will help you. Feel free to ask if something is still not clear to you. best of luck :)

pre populate the hidden field? something like this:: ??

<input type="hidden" value="1" name="currentmonth">

i understand what muhammad anas is suggesting but i don't know hot to execute the coding part like the mapping. sorry. im like a baby, knows nothing abt php. learning while im doing my project.

Yes. You have understood it right! This is exactly what I mean by pre-populating a field.

I mentioned the mapping part just as an extra user friendly feature.
For example, assuming that the current month is June and user has clicked on the "Get last month's records" button, then You can display a message on the top of the resulting page like this:

<p>Here are the records for the month of <?php echo $months[ $_POST['currentmonth'] - 1 ]; ?></p>

This would render as:

Here are the records for the month of May

However you will have to take some extra care to handle the edge case of January as the current month there is no month before January if you are not keeping track of multiple years :)

Hope it helps!

I have to add to this that you need to sanitize all HTML form data before using it in a query, to prevent both accidental errors and deliberate hacking. Specifically here:

 $basic = $_POST['basic'];
 $allowance = $_POST['allowance'];

Should be:

 $basic = mysql_real_escape_string($_POST['basic']);
 $allowance = mysql_real_escape_string($_POST['allowance']);

Since these are number fields you can also do a check like is_numeric() to verify they are numbers, but at a minimum you should escape the characters.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.