I have a form that takes in information from the user and saves the info and then displays it in the form field..

.
.

Form:

<form method="post" action="/core/process2.php" name="step2_form" target="_blank">
    <div class="float_left">
        <table class="form_step2">
            <tr class="form_step2">
                <td class="form_step2 title"><p class="form_text">File Date</p></td>
                <td class="form_step2 input"><input type="text" class="box_form" name="file_date" value="<?php include($datadir."file_date.txt"); ?>"/></td>
            </tr>
        </table>
    </div>
</form>

That is one of the approximately 300 inputs required... as you can see, to save the file, it calls process2.php which looks like this:

.

<?php
    $files = array("file_date.txt", "supply_date.txt");

    $input_name = array("file_date", "supply_date",);

    for ($i = 0; $i <= 1; $i++) 
    {
        $data = $_POST[$input_name[$i]];
        $file = fopen ($directory.$files[$i], "w");
        fwrite ($file, $data);
        fclose($file);
    }
?>

The problem I'm having is that sometimes, when the user presses the save button, it won't save the data or it will display the old data (the one that loaded with the page).

Any solutions to this? Unfortunately, this is not an online site so I can't link you to it..
.
.
.
Is there a better way of doing what I want to do??

It may be cache. Better check file_date.txt directly.

<table class="form_step2">
<tr class="form_step2">

You may face a few issues if you're coding your CSS file because you've got two COMPLETELY different elements in your HTML file with the same names. Your row is different from your table, isn't it?

I don't see a button in your form. Why is that?

process2.php file

Isn't the above mentioned file getting some data from the other php file to process?

Cache is fine - I have the no cache headers in.

.
.
.

form_step2 is a class so they're "tag specific" right? Meaning table.form_step2 { } is different from tr.form_step2 { }

.
.

MY Save button:

<input type="submit" class="save_button" name="submit" onclick="javascript:goto('form')" value="SAVE"/>

.
.
.
EDIT: So I don't really have a save button but I use

window.onbeforeunload = confirmBrowseAway;

function confirmBrowseAway()
{
    document.forms["step2_form"].submit();
}

to save data...
.
.
.
The script:

<script type="text/javascript">
        window.onload = function()
        {
            document.forms[0][0].focus();
        }

        window.onbeforeunload = confirmBrowseAway;

        function confirmBrowseAway()
        {
            document.forms["step2_form"].submit();
        }

        function goto(id)
        {
            if (id == "form")
                window.location = 'http://localhost:8080/form_2.php';
        }
    </script>

Like you suggested I checked the file itself and found that the file updates but when the page reloads, it loads the older version! :S

..
.
.
I think what's happening is that when the process2.php is called, a window opens and before that window has a chance to finish processing all of the data, the MAIN window (where user enters info) already refreshes and it uses the old data to load the data.

How can I prevent this?

function goto(id)
{
    if (id == "form")
        window.location = 'http://localhost:8080/form_2.php';
}

This is your problem.

On form submission, you're redirecting the user to form_2.php, rather than processing the form submission itself.

Hmm yeahh I realized when I posted that LOL

but what I don't understand is why does this problem only show up on other people's computers? I can, for example, close the browser and reopen it and load the updated data but when I do the same on another computer, it doesn't work! It still shows the older data :S

Do you have JavaScript enabled in your browser? Do the text files have the correct permissions (read and write)?

Are you able to re-post a succient and complete copy of your code? It's a bit difficult to follow across all code snippets at the moment.

Okay so:

I have form2.php that people enter information into, and it looks something like this:

blah blah - unimportant code here...

<form method="post" action="/core/process2.php" name="step2_form" target="_blank">
    <div class="float_left">
        <table class="form_step2">
            <tr class="form_step2">
                <td class="form_step2 title"><p class="form_text">File Date</p></td>
                <td class="form_step2 input"><input type="text" class="box_form" name="file_date" value="<?php include($datadir."file_date.txt"); ?>"/></td>
            </tr>

            <tr class="form_step2" <?php if (display_only_if(array("nfld", "ns"), array(""), array(""), array("")) == 0) echo "id=\"hide\""; ?>>
                <td class="form_step2 title"><p class="form_text">Supply Date (<i>Required</i>) </p></td>
                <td class="form_step2 input"><input type="text" class="box_form" name="supply_date" value="<?php include($datadir."supply_date.txt"); ?>"/></td>
            </tr>
        </table>
    </div>

more of the same code as above .. just A LOT of it
</form>




    <div class="form_footer">
        <input type="submit" class="save_button" name="submit" onclick="javascript:goto('form')" value="SAVE"/>
        <input type="submit" class="reset_button" name="submit" onclick="javascript:goto('reset')" value="RESET"/>
        <input type="submit" class="clear_button" name="submit" onclick="javascript:goto('clear')" value="CLEAR FORM"/>
    </div>

    <script type="text/javascript">
        window.onload = function()
        {
            document.forms[0][0].focus();
        }

        window.onbeforeunload = confirmBrowseAway;

        function confirmBrowseAway()
        {
            document.forms["step2_form"].submit();
        }

        function goto(id)
        {
            if (id == "provinces")
                window.location = 'http://localhost:8080/form_1.php';
            if (id == "form")
                window.location = 'http://localhost:8080/form_2.php';
            if (id == "open")
                window.location = 'http://localhost:8080/form_3.php';
            if (id == "reset")
                window.location = 'http://localhost:8080/core/reset_index.php';
            if (id == "clear")
                window.location = 'http://localhost:8080/core/clear_index.php';
        }
    </script>
</body>
</html>

okay so when the user tries to leave the page, the form gets submitted (by the javascript) and that form data is processed by process2.php which looks something liek this:

<?php
    $directory = "./data/form_data/";

    $files = array("on_coverletter_third_paragraph.txt", "nfld_cover_list.txt", "pbx_coverletter_paragraph.txt", "file_date.txt", "supply_date.txt");

    $input_name = array( "on_coverletter_third_paragraph", "nfld_cover_list", "pbx_coverletter_paragraph", "file_date", "supply_date",);

    for ($i = 0; $i <= 4; $i++)
    {
        $data = $_POST[$input_name[$i]];
        $file = fopen ($directory.$files[$i], "w");
        fwrite ($file, $data);
        fclose($file);
    }
?>


<script type="text/javascript">
    window.open('', '_self', '');
    window.close();
</script>

.
.
.
.
There are of course the style sheets and reset scripts and what not but they're not relevant or don't get called when saving information...

Assuming your script does actually reach process2.php, rather than following the JS redirect, I cannot immediately see why it shouldn't save the data.

You said you've tried this on other machines... are you running it as localhost on those machines? If so, and you transferred all files, do the text files have write permission?

Otherwise, I am drawing a blank at the moment, and would need to debug step by step.
To do that, you could write the number of bytes written to each text file to a log file to see that it is successfully writing the files.

Oh yeah I know the script reaches the process2.php 'cause all the other files get saved but it's always one or two random input boxes that stubbornly don't save OR display the older data.

I transfer all of the files so it's not the permission issues..

I shall try the log to figure out what the problem is. I think my best solution at this point is to redirect the main screen to the process script and when it finishes take the user to where he/she wants to go - to ensure the process2.php has successfully and completely executed before trying to load the data.

it's always one or two random input boxes that stubbornly don't save OR display the older data

Have you checked that the input names match those in your input array? And ditto for the file name?

If it works for all but a couple, that points more to a typo or similar simple error, rather than an issue with the JS. Otherwise none of them would work.

I transfer all of the files so it's not the permission issues..

You've checked the files are owned by the correct user and/or group and that the permissions include writing permission. E.g. apache:apache 664?

Yup correct permissions are set and yes I triple checked the input names array to make sure the right names correspond to the right file - like I said it works perfectly on some computers! It's really weird

Try to read the text file via php and echo out instead of using 'include()' function to show the text file.

<?php include($datadir."file_date.txt"); ?>

Instead

<?php include($data_dir . 'read_data.php'); ?>

Read the text file and echo the result in 'read_data.php'. Set file permission for that text file to 755 would be better.

Okay so I figured out that the problem is when someone clicks SAVE, it opens a new window and runs the process2.php script and starts to write the data. HOWEVER, the user is redirected to the next page before all of the data is written. So when the next page loads, it uses the old data!

.
.

How can I fix this?? I tried to remove the target="_blank" without any improvements.

Member Avatar for iamthwee

Could you not write a php function to return complete only when all data has been written... Then redirect?

Could you not write a php function to return complete only when all data has been written... Then redirect?

Perfect, this worked like a charm! :) Thank you!

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.