Forum Moderators: coopster

Message Too Old, No Replies

PHP code pausing

...whilst waiting for a form to be completed

         

dwhite

1:18 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



Using an upload form type ("file"), a user sends a text file with three numbers in.... say:
"573"

(The PHP program file will read this in as "5", "7" and "3").

The server spits back the numbers to the user via a HTML page. But the output also contains a text field to accept a number. The user can input various numbers, BUT the PHP program is PAUSED while it waits for more the user to complete this second form to send more information.

Any idea how to do this? Unfortunately, the PHP interpreter will go through the rest and not pause to wait for more user input. Here's the code with one or two important comments to signify what I want done.

***************CONTENTS OF TEXT FILE: "573"

***************CONTENTS OF HTML FILE: "test.html"
<html><head></head>
<body>
This form uploads a file to the server.<br>

<form enctype="multipart/form-data" action="getfile.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000">
Choose a file to upload: <input name="uploadFile" type="file">
<input type="submit" value="Upload File">
</form>

</body></html>

***************CONTENTS OF PHP FILE: "getfile.php"
<html><body>

<?php
$f=fopen($_FILES['uploadFile'] ['tmp_name'], "r");
while (!feof($f)) { $x=fgetc($f); echo $x+1; }
?>

<br><br>
<form enctype="multipart/form-data" action="getfile.php" method="POST">
Choose a file to upload: <input name="uploadFile" type="file"><br>
<input type="submit" value="Upload File">
</form>
<br><br>

// AT THIS POINT IN THE CODE, I WANT PHP TO WAIT
// UNTIL THE ABOVE SECOND FORM HAS BEEN COMPLETED

<?php
$f=fopen($_FILES['uploadFile'] ['tmp_name'], "r");
while (!feof($f)) { $x=fgetc($f); echo $x+2; }
?>

</body></html>

LeChuck

2:31 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



You are going about this in the wrong way - the script is executed once on every request.

If you want the data from the previous post to be availible the next time it executes you will either have to keep track of it yourself (database/textfile) or make sure it is reposted every time by including it as a hidden form field in the html you serve them after the first time (simple and requires no keeping track of sessions etc).

dwhite

9:15 pm on Jan 4, 2006 (gmt 0)

10+ Year Member



Hi LeChuck,

Thanks for the info. This is what I feared...

The hidden form field technique sounds good, except there are 1 and 2 dimensional arrays in my code (some of them quite big), and I'm not sure this kind of data can be sent back and forth.

Is there another solution?

jatar_k

5:20 am on Jan 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could put them into a session

LeChuck

4:23 pm on Jan 5, 2006 (gmt 0)

10+ Year Member



Maybe you could explain a little more what the purpose of the script is? Your use of big multidimensional arrays got me curious. Maybe I can come up with something...

The arrays can usually be sent back and forth as long as you're using POST instead of GET. Not sure how convenient it will be though. Sessions sound better.