I am trying to create a file that with some values the user entered in the webpage. But my code doesnt create a file. And I have other code that is more or less similar that creates the file:

Here is my code:

        <?php
            echo "<p> In order to create an account we need certain information about you. We will not in anyway use this information <br> ";
            echo "  To identify you personaly. Read out privacy statement <a href='privacy.php'>HERE</a></p>";
            echo "<div class='min_h_personal'>";
            echo "  <h2 class='per_h2'>Required information</h2> ";
            echo "</div>";

            echo "<center>";
            echo "<form>";
            echo "First name:<br>";
            echo "<input type='text' name='fname'/><br>";
            echo "Last name:<br>";
            echo "<input type='text' name='lname'/><br>";
            echo "Email: <br>";
            echo "<i style='font-size:12;'> Is also your account name </i><br>";
            echo "<input type='text' name='email'/><br>";
            echo "Password: <br>";
            echo "      <i style='font-size:12;'> Must contain atleas 1 number and 1 upper case character </i><br>";
            echo "      <input type='password' name='password'/><br>";
            echo "      <input type='checkbox' name='gender'/> Male<br>";
            echo "      <input type='checkbox' name='gender'/> Female<br>";
            echo "      Country:<br>";
            echo "      <!-- this took me a long time to list all the countries :| -->";
            echo "      <select>";
            echo "          <option name='c' id='country'> --- Select onse --- </option>";
            echo "          <option name='africa' id='country'> Africa </option>";
            echo "          <option name='america' id='country' America </option>";
            echo "          <option name='europe' id='country' Europe </option>";
            echo "          <option name='ociania' id='country'> Ociania </option>";
            echo "          <option name='antartica' id='country'> Antartica </option>";
            echo "      </select>";
            echo "      <br>";
            echo "      <iframe src='forumrulesexcept.php' width='500' height='450'>";
            echo "      </iframe><br>";
            echo "      <input type='checkbox' name='no'/> No <br>";
            echo "      <input type='checkbox' name='yes'/> Yes <br>";
            echo "  </form>";
            echo "</center>";

            echo "<div class='add_info'>";
            echo "  <h2 class='add_h2'>Additional infromation</h2>";
            echo "</div>";
            echo "<center>";
            echo "  <form>";
            echo "      Job position: <br>";
            echo "      <input type='text' name='job'/><br>";
            echo "      Company: <br> ";
            echo "      <input type='text' name='company'><br>";
            echo "      Cellphone: <br>";
            echo "      <input type='text' name='cell'/><br>";
            echo "  </form>";
            echo "  <input type='submit' name='submit' value='Submit'>";
            echo "</center>";

            if(isset($_REQUEST['submit'])){
            $email = $_REQUEST['email'];
            $pass = $_REQUEST['password'];

            $file = fopen($email.$pass.".html");

            fwrite($file, "$email");

            fclose($file);
            }

            ?>

This is only a peice, this piece is contained in a larger .php file.
What is wrong and how do I fix it?

Thanks...

Recommended Answers

All 5 Replies

First of all, using $_REQUEST may pose some danger to security, because if I would now go to your PHP file's location and put "?submit=true&email=somedangerousvirus" in the link, I would gain access to writing a file to your server.

Then, why would your file writing not be working? Did you check if your script passes your if() statement? You can check by adding a simple line, for example:

if(isset($_REQUEST['submit']))
{
    echo 'We\'ve passed the if() statement!';

    // Rest of code
}

Also, there is an easier way of "just" writing a file, which might also suit your needs: file_put_contents(). Parameters: $file_name, $data. E.g.:

file_put_contents('file.txt', 'Hello!');

Also, I think that this line:

fwrite($file, "$email");

is now literally writing "$email" to your file, instead of the contents of $email. Not sure though, as I always use single quotes (') instead of double.

I inserted the code and it doesn't display anything. So I am guesing my if statement isn't working.

I am guessing the same ;). You could try putting the following at the start of your code:

echo '<br>$_REQUEST:<br><pre>';
var_dump($_REQUEST);
echo '</pre>';
echo '<br>$_POST:<br><pre>';
var_dump($_POST);
echo '</pre>';
echo '<br>$_GET:<br><pre>';
var_dump($_GET);
echo '</pre>';

And see if it returns what you want. If not, your forms are not working properly. You could also add this to your forms:

<form ... method="post">

And then read out your data using $_POST instead of $_REQUEST.

And one last thing. If you have a lot of lines of HTML, it might be easier to read by just typing it in HTML, not in PHP, by "pausing" the PHP lines. For example:

<?php
// PHP Code here, a lot of HTML is coming up.
// Close PHP for a second:
?>

<HTML>
blabalbla
a lot of HTML

<?php
// Phew, that was a lot of HTML. Continue writing PHP.

Thanks, I am going to try the code and hopefully it will work. I was under the impression that the html had to be written by PHP or else the $_REQUEST wouldn't work.

It won't matter, but remember that you cannot just use PHP inside your HTML then. You would have to use, for example:

<input type="text" name="username" value="<?php echo $username; ?>">

If you wanted to set the value of that input field to $username.

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.