-2

Sorry if this is a repost, i didn't find an article that gives a solution to this issue.

Here's my example code:

<?php

echo "<form action=\"test2.php\" method=\"post\">";
echo "<input type=\"text\" name=\"ent1\">";

$queue = $_POST["ent1"];

$fh = fopen("temp.txt", 'w');
fwrite($fh, $queue);
fclose($fh);

echo "<input type=\"submit\">";

?>

This code should write whatever i type in the text box to the file "temp.txt" but it doesn't. It has something to do with the form action. It works fine if I take out the action and just use:

echo "<form method=\"post\">";

is there a way around this issue? I need to call an action when I click on my submit button and write to a file at the same time.

2 Answers2

0

Save the file as test2.php

<?php

echo "<form action=\"test2.php\" method=\"post\">";
echo "<input type=\"text\" name=\"ent1\">";
echo "<input type=\"submit\">";

if(isset($_POST["ent1"])){
    $queue = $_POST["ent1"];
    $fh = fopen("temp.txt", 'w');
    fwrite($fh, $queue);
    fclose($fh);
}
?>
arun
  • 3,667
  • 3
  • 29
  • 54
  • test2.php and temp.txt are two different things. I'm using test2.php as a page on my website but temp.txt is just storing some information behind the website.. the code above works the same as the code I posted originally :/ – texas techie 213 Apr 30 '15 at 08:04
0

You should split your code in two files : the form, and the form's validation :

test1.php

<?php

echo '<form action="test2.php" method="post">'
    . '<input type="text" name="ent1">'
    . '<input type="submit">';

test2.php

<?php

if(isset($_POST["ent1"]))
{
    $queue = $_POST["ent1"];
    //Some input validation code

    $fh = fopen("temp.txt", 'w');
    fwrite($fh, $queue);
    fclose($fh);
}
else
{
    //Some error handling code
}

Edit :

test1.php

<?php

echo '<form action="./test2.php" method="post">' //...

test2.php

<?php

//...
//$fh = fopen("temp.txt", 'w');
//fwrite($fh, $queue);
//fclose($fh);

/*Less calls here, do the same*/
file_put_contents("temp.txt", $queue);
//...
Yoluk
  • 310
  • 1
  • 10
  • does that work for you? I'm still having the same problem.. works without action, doesn't work with D: – texas techie 213 Apr 30 '15 at 08:09
  • I just edited my post : i added a else, where you should write something like `echo 'ERROR';`, in order to debug your app – Yoluk Apr 30 '15 at 08:12
  • What is the problem exactly ? It dosen't write in the file when you add the `action` attribute in the `form` tag ? – Yoluk Apr 30 '15 at 08:17
  • yes exactly. it doesn't write to the file when my form has an action attribute. – texas techie 213 Apr 30 '15 at 08:20
  • Which is very strange. Hold on, i'm going to test something – Yoluk Apr 30 '15 at 08:23
  • Works for me. I put the two files in the same directory, and it writes in the `temp.txt` file in the same directory. Check the edits in my post – Yoluk Apr 30 '15 at 08:38
  • What version of php are you using? I was messing around with thing and noticed that if I use a hard coded string like this: fwrite($fh, "SOME TEXT"); that it works, but not with a $variable. :S so confused – texas techie 213 Apr 30 '15 at 08:45
  • Php 5.4. Can you try to `echo $queue` in the `if` before writing in the file, `echo 'NOPE'` in the `else`, re-run, and tell me if this pass ? – Yoluk Apr 30 '15 at 08:51
  • Then, your `$_POST["ent1"]` is not set. Replace `echo 'NOPE'` with `var_dump($_POST);`, and tell me what you get (if the array is empty or not) – Yoluk Apr 30 '15 at 08:58
  • Hmmm. Please check if you made no mistakes in the names, especially `ent1` , and make sure there is no redirection between `test1.php` and `test2.php`. If you have a redirection, then your $_POST could have been flushed – Yoluk Apr 30 '15 at 09:09
  • ' . '' . ''; if(isset($_POST["ent1"])) { $queue = $_POST["ent1"]; echo $queue; $fh = fopen("temp.txt", 'w'); fwrite($fh, $queue); fclose($fh); } else { var_dump($_POST); } that's the code i'm using to test right now. copied and pasted. – texas techie 213 Apr 30 '15 at 09:13
  • im going to stop for now. thanks for all your help Yoluk! you've been very helpful :) – texas techie 213 Apr 30 '15 at 09:23
  • I tried your code, works for me. I suggest you to check this : http://stackoverflow.com/questions/1282909/php-post-array-empty-upon-form-submission. Good luck with this, it seems nasty. ;) – Yoluk Apr 30 '15 at 09:27