Jump to content

Recommended Posts

Hi im very new to this php coding, ive mostly done just html and xhtml.

although i know its not the safest way to store information.

The situation is that i have a html form with javascript validation on it to make sure it reaches certain validation points such as proper email etc...

i just want to place the information on sumbit into a .txt file threw php.

Link to comment
https://forums.phpfreaks.com/topic/171784-solved-html-forms-php/
Share on other sites

Firstly, don't *rely* on JavaScript for your validation. JS can be disabled and manipulated by the user, which makes it completely insecure. Always validate with PHP (although feel free to validate with JS as well). Read up on how to use the fwrite function to write the data to a text file. There's good explanation and examples on the PHP manual (see the link).

Thank you for the quick reply.

Yeah i know javascript has it major flaws but im just learning, i had a look at the fwrite before i posted here just didnt understand it to well.

Say i was going to use a php to store

 

<form action="text.php" method="post">
<table>
	<tr>
		<td>Email:</td>
			<td><input name="email" value="" type="text" /></td>
	</tr>
</table>
<input type="submit" value="Submit" onclick="clientside();return false;" />

 

Its just if i have a slight example how to use i may be able to try it

i tried using the example before and it was just giving me a blank page (example on fwrite)

Well just a quick example similar to in the manual:

 

if (isset($_POST['email']))
{
    // some form of email validation
    $email = validateEmail($_POST['email']);

    // write-to file
    $filename = 'path/to/emails.txt';

    // attempt to open the file
    if ($handle = fopen($filename, 'a'))
    {
        // attempt to write to the file
        if (!fwrite($handle, $email))
        {
            echo 'Cannot write to file';
        }
        else
        {
            echo 'Written to file';
        }
    }

    fclose($handle);
}

well here it is the code for php im using is the one above.

ill add to it once i get it up and going

<?php
if (isset($_POST['email']))
{
    // some form of email validation
    $email = validateEmail($_POST['email']);

    // write-to file
    $filename = 'text.txt';

    // attempt to open the file
    if ($handle = fopen($filename, 'a'))
    {
        // attempt to write to the file
        if (!fwrite($handle, $email))
        {
            echo 'Cannot write to file';
        }
        else
        {
            echo 'Written to file';
        }
    }

    fclose($handle);
}

?>

 

and in my html file im using

<form action="text.php" method="post">
   <table>
      <tr>
         <td>Email:</td>
            <td><input name="email" value="" type="text" /></td>
      </tr>
   </table>
   <input type="submit" value="Submit" onclick="clientside();return false;" />
</form>

 

there is more but its just repeditive code, i can post it if you wish.

 

 

'validateEmail()' isn't actually a built-in PHP function, just with the many, many ways of validating email available out there on the net, I didn't want to complicate the code.. which I guess I actually did. I'm assuming that $handle = fopen($filename, 'a') returned false, or that $_POST['email'] wasn't set - but that's less likely looking at the form HTML. Check the write permissions on the file & directory where your text file is, and add an 'else' clause to see if there's an error returned whilst trying to open the file:

 

    // attempt to open the file
    if ($handle = fopen($filename, 'a'))
    {
        // attempt to write to the file
        if (!fwrite($handle, $email))
        {
            echo 'Cannot write to file';
        }
        else
        {
            echo 'Written to file';
        }
    }
    else
    {
        echo 'Unable to open file';
    }

 

Should point out what's going wrong.

Well it needs to be run from a web server that has PHP installed, yeah... And no, you need a server-side programming language capable of file operations. You could install PHP and a web server on your computer quite easily though with some of the packages out there.. LAMP, XAMPP, etc.

Parse error: syntax error, unexpected T_ELSE in C:\xampplite\htdocs\part2\text.php on line 25

 

im getting this error with this code

<?php
$filename = 'text.txt';
$somecontent = "Add this to the file\n";

// Let's make sure the file exists and is writable first.
//if (is_writable($filename)) {

// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);

//} else {
//echo "The file $filename is not writable";
//}
?>

Sorry ive acted like a idiot i was running the wrong script.

Fatal error: Call to undefined function validateEmail() in C:\xampplite\htdocs\part2\text1.php on line 5

 

i am know using the script that u first gave me.

Since the other one did work

im sorry ive been away from this web programming for tolong lol.

thanks for your patience

is it possible to make it enter in format such as Email : Email then next time i sumbit its on another line

 

Sorry?

 

and is it possible to refer so that it says Email added on my html page.

or is that not possible.

 

Unless you specify PHP to parse HTML pages it won't, however it's pretty easy to set it up that way. A quick search on Google will find you many, many tutorials I imagine on how to do it. You may need to re-work the code slightly.

mr adam

i dont think i can use this fwrite

as i have more variables then it can take

        // attempt to write to the file
        if (!fwrite($handle, $email, $fname, $lname, $age, $address, $city))

im getting this error

 

Warning: fwrite() expects at most 3 parameters, 7 given in C:\xampplite\htdocs\part2\text1.php on line 24
Cannot write to file

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.