Jump to content

Recommended Posts

Okay so I do not really know php that well and I am trying to write a simple website code in it but I am running into a problem.  I have a form and I want it to add to the end of a list.  I have a text file called codes.txt on the server and idk why this won't work.

 

<html>
<body>

Your friend code <?php echo $_POST['fcode']; 
$myFile = "codes.txt";
echo $myFile;
$fh = fopen($myfile,'a+') or die("can't open file");
fwrite($fh , $fcode);
?>
has been added to the list

</body>
</html> 

 

 

site here: http://invitemasteronline.t35.com/test.html

Link to comment
https://forums.phpfreaks.com/topic/187075-php-noob-question/
Share on other sites

i believe this is along the lines of what you want. But I would suggest imposing some security restrictions on who can send the form and what data can be sent, you dont want just anybody posting huge amounts of unregulated or even malicious data to a file on your server.

 

<?php
function show_form() {
?>
<form action="" method="post">
	Friend Code: <input type="text" name="fcode" />
	<input type="submit" value="submit" />
</form>
<?php
}
$friend_file = 'friends.txt';
$friend_content = file_get_contents($friend_file);
if(isset($_POST['fcode'])) {
	$friend_code = $_POST[fcode];
	$friend_content = $friend_content . "\r\n" . $friend_code;
	if(file_put_contents($friend_file,$friend_content)) {
		print $friend_code . ' has been added to the friends file.<hr />';
	}
}
show_form();
?>

Link to comment
https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-987891
Share on other sites

i believe this is along the lines of what you want. But I would suggest imposing some security restrictions on who can send the form and what data can be sent, you dont want just anybody posting huge amounts of unregulated or even malicious data to a file on your server.

 

<?php
function show_form() {
?>
<form action="" method="post">
	Friend Code: <input type="text" name="fcode" />
	<input type="submit" value="submit" />
</form>
<?php
}
$friend_file = 'friends.txt';
$friend_content = file_get_contents($friend_file);
if(isset($_POST['fcode'])) {
	$friend_code = $_POST[fcode];
	$friend_content = $friend_content . "\r\n" . $friend_code;
	if(file_put_contents($friend_file,$friend_content)) {
		print $friend_code . ' has been added to the friends file.<hr />';
	}
}
show_form();
?>

thanks but I don't really wanna change the whole thing, cause then I don't understand what's happening at all.  I just wanna figure out why mine isn't working

Whats $fcode?

$fcode=$_POST['fcode']

 

 

HTH

Teamatomic

sorry, that was the input from a form.

Also its not even reading the file still I think.

Link to comment
https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-987898
Share on other sites

Why dont you give the full path the the file. if its in the same folder as the script then ./file.txt otherwise /path/to/file.txt. And if you have control of the server and the code is not for distribution and you have php v5+ then use

file_put_contents("$myFile", "$fcode", FILE_APPEND, LOCK_EX);

 

who owns the file? what are the perms?

 

so what if its form output. To use $fcode you need to give it a value

 

also...show us the error you get..."it's broken" is not really good enough

 

HTH

Teamatomic

Link to comment
https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-987930
Share on other sites

@crabfinger don't just write some code without thinking about it always make sure that heavy operations (or soon to be heavy) are lazy-loaded

 

$friend_file = 'friends.txt';
$friend_content = file_get_contents($friend_file);
if(isset($_POST['fcode'])) {
    $friend_code = $_POST[fcode];
    $friend_content = $friend_content . "\r\n" . $friend_code;
    if(file_put_contents($friend_file,$friend_content)) {
        print $friend_code . ' has been added to the friends file.<hr />';
    }
}

 

In the above code is the file (friends.txt) data read but not used until the form is submitted.

Link to comment
https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988082
Share on other sites

@crabfinger don't just write some code without thinking about it always make sure that heavy operations (or soon to be heavy) are lazy-loaded

 

$friend_file = 'friends.txt';
$friend_content = file_get_contents($friend_file);
if(isset($_POST['fcode'])) {
    $friend_code = $_POST[fcode];
    $friend_content = $friend_content . "\r\n" . $friend_code;
    if(file_put_contents($friend_file,$friend_content)) {
        print $friend_code . ' has been added to the friends file.<hr />';
    }
}

 

In the above code is the file (friends.txt) data read but not used until the form is submitted.

 

Sorry when i wrote that i was going to add

 

print '<hr />' . $friend_content;

 

to the end of the file for debugging purposes

Link to comment
https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988315
Share on other sites

How does that put

$friend_content = file_get_contents($friend_file);

in the correct place?

 

$friend_file = 'friends.txt';
$friend_content = file_get_contents($friend_file);
if(isset($_POST['fcode'])) {
    $friend_code = $_POST[fcode];
    $friend_content = $friend_content . "\r\n" . $friend_code;
    if(file_put_contents($friend_file,$friend_content)) {
        print $friend_code . ' has been added to the friends file.<hr />';
    }
}
print '<hr />' . $friend_content;

 

means that i only have to load the file once because it is being used wether or not the form is posted.

Link to comment
https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988397
Share on other sites

@crabfinger don't just write some code without thinking about it always make sure that heavy operations (or soon to be heavy) are lazy-loaded

 

$friend_file = 'friends.txt';
$friend_content = file_get_contents($friend_file);
if(isset($_POST['fcode'])) {
    $friend_code = $_POST[fcode];
    $friend_content = $friend_content . "\r\n" . $friend_code;
    if(file_put_contents($friend_file,$friend_content)) {
        print $friend_code . ' has been added to the friends file.<hr />';
    }
}

 

In the above code is the file (friends.txt) data read but not used until the form is submitted.

 

ok thank you soo much that worked great!! Now I am really confused though I tried to change the code, so it would check if the user has tried to submit a code before.  It does not work!

<html>
<body>
<?php
$friend_file = 'friends.txt';
$friend_content = file_get_contents($friend_file);
if(isset($_POST['fcode'])) {
    $pos = strpos($friend_content, ($_POST['fcode']);
        if ($pos === false) {
           $friend_code = $_POST[fcode];
           $friend_content = $friend_content . "\r\n" . $friend_code;
           if(file_put_contents($friend_file,$friend_content)) {
        print $friend_code . ' has been added to the friends file.<hr />';
        } }
        if ($pos === !false){
     print $friend_code . ' is already on the list!<hr />';
    }
}
?>

</body>
</html> 

 

 

error is: "Parse error: syntax error, unexpected ';' in /home/freehost/t35.com/i/n/invitemasteronline/addcode.php on line 7"

Link to comment
https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988534
Share on other sites

Change

   $pos = strpos($friend_content, ($_POST['fcode']);

to:

   $pos = strpos($friend_content, $_POST['fcode']);

There seems to have been an error when the code was written, that is line 7.

 

ahh thanks now it works perfectly!! thanks so much every one!!

 

I will probably have more questions down the road but for now i'm good.  I <3 u all :D

Link to comment
https://forums.phpfreaks.com/topic/187075-php-noob-question/#findComment-988546
Share on other sites

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.