PDA

View Full Version : how to...


Gary King
Sat 25th May '02, 10:20pm
how can i make a page that has a list of text, and an insert field and a submit button?? what i want to happen is that when someone inserts something into the blank field, and hit submit, then it'll add that text to the list..

i'm not sure if it's possible in PHP, but i know it's possible in other languages.. so if u can help me out here, then plz reply :D:D

Dan615
Sat 25th May '02, 11:55pm
Do you want it so that it saves, and as other users post it continues to add onto one list, or do you want it for only that one user?

Goldfinger
Sun 26th May '02, 12:00am
it can be done quite easily .. ill make one up tonight for you :). how would you like it.. mysql or flat file?....

Dan615
Sun 26th May '02, 12:02am
I'll race you :)

Goldfinger
Sun 26th May '02, 12:03am
haha thats alright.. im really busy so i wasnt gonna get to it for like a couple hours ;). but ill post my code after i finish it, even if you have yours done.

Gary King
Sun 26th May '02, 12:09am
thx for the help guys !! just post your code here, and I'll try it out :):)

Dan615
Sun 26th May '02, 12:12am
Yay, I win :) This is an over simplified version of what you're asking for...it's with MySQL, and it just kinda plops the text at the end of the table, then displays it in a list...


<?

@mysql_connect($host, $user, $pass) or die(mysql_error()); // make sure the variables are set...
@mysql_select_db($mysql_db);

if (isset($act) && $act == "add") {
@mysql_query("insert into table values (\"$text\")"); // "table" is where the info is stored, and $text is the input variable
exit;
}

$result = @mysql_query("select * from table") or die(mysql_error()); // get the info from mysql
$list = "<ul>"; // start the list
while ($text = mysql_fetch_array($result)) { // loop through array...
$list .= "<li>$text['text']"; // add it to list...
}
$list .= "</ul>"; // close the list...

?>
<html>
<head>
<title> Title </title>
</head>
<body>
<?=$list?><br><br>
<form action="<?=$PHP_SELF?>?act=add" method="post">
Text: <input type=text name=text><br>
<input type=submit value="Add">
</form>
</body>
</html>


I have a feeling this isn't quite what you're looking for...it seems...too...easy ;)

Dan615
Sun 26th May '02, 12:23am
Another version, using plain text files...it puts the text in the file as HTML (automatically putting the <li> to make it a list).

<?

if (isset($act) && $act == "add") {
// open file for appending.... (don't forget to specify $file_name
$file = fopen($file_name, "a") or die("Couldn't open $file_name for appending.");
fwrite($file, "<li>$text");
fclose($file);
}

// open file for reading...
$file = fopen($file_name, "r") or die("Couldn't open $file_name for reading.");
$list = "<ul>"; // initiate list variable
while ($chunk = fread($file, 4096)) { // read 4k at a time...
$list .= $chunk;
}
$list .= "</ul>";
fclose($file);
?>
<html>
<head>
<title> Title </title>
</head>
<body>
<?=$list?><br><br>
<form action="<?=$PHP_SELF?>?act=add" method="post">
Text: <input type=text name=text><br>
<input type=submit value="Add">
</form>
</body>
</html>

rylin
Sun 26th May '02, 8:21am
both your snippets require register_globals to be on ;)
add the following to the top

$v = (int)str_replace(".","", phpversion());
if($v < 410) {
$_POST = &$HTTP_POST_VARS;
}
$act = $_POST['act'];


;)

Gary King
Sun 26th May '02, 11:21am
thx for the ton of help, guys !! I'm gonna test em out rite now, and see how it goes.. :D:D

Dan615
Sun 26th May '02, 1:37pm
Originally posted by okidoki
both your snippets require register_globals to be on ;)
add the following to the top

$v = (int)str_replace(".","", phpversion());
if($v < 410) {
$_POST = &$HTTP_POST_VARS;
}
$act = $_POST['act'];


;)

Sorry, I was just using the thing where it automatically gets form variables, not paying attention to whether or not it's turned on or not...I know there are problems, like having the same variables in the query string through GET and through POST, but this was meant to be oversimplified....and that's usually how I write my stuff :)

Thanks for the tip though ;)

scoutt
Tue 28th May '02, 11:44am
Originally posted by Dan615


Sorry, I was just using the thing where it automatically gets form variables, not paying attention to whether or not it's turned on or not...I know there are problems, like having the same variables in the query string through GET and through POST, but this was meant to be oversimplified....and that's usually how I write my stuff :)

Thanks for the tip though ;)
never heard about that "like having the same variables in the query string through GET and through POST" how do you have the same variables? as far as I know you can't, in any version.

I don't think he was talking about that. with globals off all variables that are sent through http will be empty unless you use the super globals. same goes with PHP_SELF, you have to use$_SERVER[PHP_SLEF]. PHP_SELF is deprecated in version 4.1 and above.

but, nice little scipts never-the-less.

Dan615
Tue 28th May '02, 6:33pm
Right after I posted that I looked at what I wrote and slapped myself ;) I was too tired to think, and my internet was acting up, so I just shut down and forgot.

I still use $PHP_SELF and the plain form variables, without the $_POST['variable'] or $_SERVER['PHP_SELF']...I haven't been paying enough attention to the change logs i guess :rolleyes: