Jump to content

Recommended Posts

I'm new to the whole web page design thing. i'm trying to make a very simple script thats writes only unique entries to a txt file but i dont know how th disallow duplicates. i need to know how to do that. i would like the script as small and simple as possible and security is no issue as the data is not vital. also i need a script that redirects to a random page chosen from a line on a text file in a frame with an menu to change the delay time without repeating and pages. the second request i haven't started on yet so i might be able to figure that out. in its entirety i dont want the script using anything but html, php, and js. no databases. heres what i got so far.

---------------------------------------------------

 

in folder "usrs"

"index.htm"

"usr.php"

"usr.txt"

=====================================

usrs/index.htm

-----------------------------------------------------------------------------

<html>
<body>

<SCRIPT language=javascript>
<!--
function onlyNum(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
return false;

return true;
}
//-->
</SCRIPT>

<form action="usr.php" method="post">
User: <input type="text" name="usr" onkeypress="return onlyNum(event)" />
<input type="submit" />
</form>

</body>
</html>

=================================================================

usrs/usr.php

-------------------------------------------------------------------------------------------

<?PHP

header("refresh: 3; url=http://localhost/usrs");
$usrs = "usr.txt";
$input = $_POST['usr'];

$usrp = fopen ($usrs, "a+");
if ($usrp) {
fwrite ($usrp, "$input\r\n");
fclose ($usrp);
echo ("<center>Database updated<br>Thank you for the submission.</center>");


}
else {
echo ("An error occurred and database was not updated");
}

?>

===================================================================

usrs/usr.txt (examples only)

----------------------------------------------------------------------------------------------

012345
123456
234567

===================================================================

The script works but i do not know how to disallow duplicate entries

Link to comment
https://forums.phpfreaks.com/topic/102791-very-new-very-simple/
Share on other sites

Something like:

 

<?php

header("refresh: 3; url=http://localhost/usrs");
$usrs = "usr.txt";
$input = $_POST['usr'];

$lines = file($usrs);

$usrp = fopen ($usrs, "a+");
if (in_array($input, $lines)) {
    echo 'duplicate entry';
}
else if ($usrp) {
fwrite ($usrp, "$input\r\n");
fclose ($usrp);
echo ("<center>Database updated<br>Thank you for the submission.</center>");


}
else {
echo ("An error occurred and database was not updated");
}

?>

Link to comment
https://forums.phpfreaks.com/topic/102791-very-new-very-simple/#findComment-526520
Share on other sites

lol, that dont surprise me, i dont know what i'm talking about. instead of writing the usr to a txt file would it help to write it to a js file as a js variable? and if i did that could i put a js snippet in there that checks to see if the variable exists and if it doesn't add it to the js file? does this make sense?

Link to comment
https://forums.phpfreaks.com/topic/102791-very-new-very-simple/#findComment-526560
Share on other sites

That wouldn't be a good idea. You could however utilize Javascript in form of AJAX to do some validation on the client side before the form is submitted. You should however still check it in your PHP script though.

 

Do a Google search for AJAX. There are plenty of resources out there.

Link to comment
https://forums.phpfreaks.com/topic/102791-very-new-very-simple/#findComment-526562
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.