Jump to content

Recommended Posts

There are many ways you can join a string, probably the most simplest method being to concatenate several variables together, for example:

 

$user_info = $fname . "\n" . $lname . "\n" . $email; //etc.

 

It would depend how you want the information formatted in the text file though as to what you would join them together with. In the example I'm using "\n" which is a line break. So the data in the text file would end up being (with example data):

 

John

Smith

jsmith@example.com

 

You'd probably want either a double line break ("\n\n") or some form of delimiter to know 2 sets of data apart. The example I gave is, as I mentioned using line breaks, but you could also use other delimiters such as:

 

$user_info = $fname . "||" . $lname . "||" . $email; //etc.

 

Which would give you:

 

John||Smith||jsmith@example.com

 

You could then separate sets of data up with a line break or a different delimiter (e.g. "||||").

 

If you're feeling up to it though I'd suggest entering the user data into an array and using JSON or serialize, store the data in a more interchangeable format.

Before i try your code i think i may have got it to work this way

Only problem is that it doesnt the ELSE

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');

if (isset($_POST['email'] ,
	 $_POST['fname'] ,
	 $_POST['lname'] ,
	 $_POST['age'] ,
	 $_POST['address'] ,
	 $_POST['city']))
{
    // some form of email validation
    $email = ($_POST['email']);
$fname = ($_POST['fname']);
$lname = ($_POST['lname']);
$age   = ($_POST['age']);
$address = ($_POST['address']);
$city  = ($_POST['city']);

    // 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));
	if (!fwrite	($handle, $fname));
	if (!fwrite	($handle, $lname));
	if (!fwrite	($handle, $age));
	if (!fwrite	($handle, $address));
	if (!fwrite	($handle, $city));

        {
            echo 'Cannot write to file';
        }
	else
        {
            echo 'Added to our System';
        }
    }

    fclose($handle);
}
   
?>

 

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

 

its just a different way ive tried it, i am also working on the example u provided.

 

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.