umuur
umuur

Reputation: 91

PHP $_POST Problem

i have problem at php $_POST code. this is the my codes? what i do wrong?

this is index.php

<form action="upload.php" method="post" id="form">
<input type="text" name="fname" size="87" />
<input type="submit" style="display:none" />

if i click the submit, page going to upload.php and white screen. and i look at the sample.txt, it's like a:

<h1></h1>

this is upload.php

$yazi=$_POST['fname'];
$fo = fopen("sample".".txt", "a");
fwrite($fo, '<h1>'.$yazi.'</h1>');
fclose($fo);

what can i do for fix? and i using jquery submit. so not a problem display:none, i think.

Upvotes: 0

Views: 2103

Answers (4)

BreEve Antonio
BreEve Antonio

Reputation: 1

You can verify first you $_POST method before performing the action to upload.php

<php>
  if(isset($_POST['fname'])){
    //your echo or any code here..
    //this is to test your method
  }
?>

Upvotes: 0

Shef
Shef

Reputation: 45589

Change

$fo = fopen(sample.".txt", "a");

To

$fo = fopen("sample.txt", "a");

Complete code:

<?php
var_dump($_POST);

$filename = 'sample.txt';
$yazi = '<h1>'.$_POST['fname'].'</h1>';

if (is_writable($filename)) {
    if (!$fo = fopen($filename, 'a')) {
         echo "Cannot open file ($filename)";
         exit;
    }
    if (fwrite($fo, $yazi) === FALSE) {
        echo "Cannot write to file ($filename)";
        exit;
    }
    echo "Success, wrote ($yazi) to file ($filename)";
    fclose($fo);
} else {
    echo "The file $filename is not writable";
}
?>

Upvotes: 0

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146350

I see nothing in your code that would prevent the form field from ending up in the file so the error is most likely somewhere else. Here you are a couple of tricks you can use to help you find the issue:

  1. Add this on top of all your scripts:

    ini_set('display_errors', TRUE);
    error_reporting(E_ALL);
    
  2. Add this on top of upload.php:

    var_dump($_POST);
    

    ... and go to your browser's "View-> Source" menu to see what your data looks like.

  3. Validate your generated HTML with the W3C online validator.

Upvotes: 0

Vinicius Kamakura
Vinicius Kamakura

Reputation: 7778

There is nothing wrong happening, the white screen is how it should be working.

After you are done processing in upload.php, you can redirect the browser somewhere with the Location header directive. For example:

header("Location: http://mypage.com/where_to_go_after.php");

The empty $var problem, try using " like:

fwrite($fo, "<h1>".$yazi."</h1>");

Upvotes: 1

Related Questions