text submission

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

bst82551
Forum Newbie
Posts: 15
Joined: Wed Dec 29, 2004 12:25 pm
Location: Huntsville, Texas

text submission

Post by bst82551 »

I'm using a guestbook with php, and for some reason... it won't allow me to submit text. I've had the same problem with other php scripts that use text submissions. Do I have to enable a certain .dll file in php.ini? Why is this happening?

Brian
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

post your code.
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

Post by Bon Bon »

Anything that is submitted using a form with an attribute of method set to post will be sent to PHP and the posted information will be stored in the $_POST array.

If the information is sent via the address bar such as page.ext?q=hello you would be able to access the information in the address bar with the $_GET array like such:

Code: Select all

echo $_GET['q'];
The output would be:
hello

Points to look out for:
  • make sure that your form method is correct.
  • make sure that the information being sent is the right datatype.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

You must remember to use 'post' as the form action if you are planning to post data from text fields.
bst82551
Forum Newbie
Posts: 15
Joined: Wed Dec 29, 2004 12:25 pm
Location: Huntsville, Texas

2 files

Post by bst82551 »

feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


there are 2 different files...

Code: Select all

<?php

// phpSimpleGuestbook v1.0
// Copyright (c)2002 Svein-Magnus Sorensen
// First Created the 29th of March 2002
// Latest version from the 23rd of May 2002
// URL: http://devel.menneske.org/
// Contact: sveins@tihlde.org
// 
// This Software is distributed under the GNU General Public             #
// License. For more details see license.txt                             #

$pagetitle = "Brian's Guestbook";

if (is_readable("data.dat") && is_writeable("data.dat")) {
       $data = file("data.dat");
       $dataї0] = $dataї0] + 1;
       $datas = $dataї0] . "\n" . $dataї1];
       $datfile = fopen("data.dat", "w");
       fwrite($datfile, $datas);
       $success = fclose($datfile);
}
if (is_readable("guestbook.dat")) {
	$array = array();
	$array = file("guestbook.dat");
	$array = array_reverse($array);
	$arraysize = sizeof($array);
}


echo "

  <HTML>
  <HEAD>
  <TITLE>"; echo "$pagetitle"; echo "</TITLE>";

echo "<meta http-equiv="pragma" content="no-cache">";
  
	echo "<LINK REL=stylesheet HREF="../scroll.css" TYPE=text/css>";
echo "
</HEAD>
<BODY class=guestbook>

<center><table align=center width=550 border=0>
	<tr><td align=center><h1 class=guestbook>"; echo "$pagetitle"; echo "</h1>
	<P class=toptext>Number of pageviews: "; echo "$dataї0]"; echo "    Number 

of Entries: "; echo "$arraysize"; 
	echo "<br></p><br>
		<form action=sign.php method=post>
	<table align=center width=575 border=0 cellspacing=5>
	<tr><td colspan=4><center><h2 class=guestbook>Leave a greeting:</h2></td></tr>
	<tr><td width=25%><p align=right>Name:</p></td><td> <input type=text name=navn 

size=30></td>
	<td width=25%><p align=right>Location:</p></td><td> <input type=text name=sted 

size=30></td></tr>
	<tr><td width=25%><p align=right>E-Mail:</p></td><td> <input type=text name=mail 

size=30></td>
	<td width=25%><p align=right>Homepage:</p></td><td> <input type=text name=www size=30 

value="http://"></td></tr>
	
	<tr><td colspan=4><br><center>Your Greeting:<br><textarea WRAP=VIRTUAL name=greet 

align=center rows=7 cols=50></textarea></center></td></tr>
					
	<tr><td colspan=4><center><input type=reset value="Reset Fields"> <input 

type=submit value="Post message"></center></td></tr>
	
	</table></form>
</center></td></tr>";
	echo "<tr><td>";
		
for ( $i=0 ; $i<sizeof($array) ; $i++ ) { 
	
	$greeting = "";
	$ip = strtok($arrayї$i],"€");
	$dato = strtok("€");
	$navn = strtok("€");
	$sted = strtok("€");
	$mail = strtok("€");
	$www = strtok("€");
	$tok = strtok("€");
	while ($tok) {
		$greeting = $greeting . "<br>" . $tok;		
		$tok = strtok("€");
	}
	echo "<br><hr align="center" width=50%><br>";
	echo "<table align=center width=100% border=0 cellspacing=4>";
	echo "<tr><td colspan=2><center><p class=timestamp>"; echo "$dato"; echo 

"</p></td></tr>";
	echo "<tr><td><p align=left><b>Name: </b>"; echo "$navn"; echo "</td>";
	if ($sted <> " ") { echo "<td><p align=left><b>Location: </b>"; echo "$sted"; echo 

"</td></tr>"; }
	if ($mail <> " ") { echo "<tr><td><p align=left><b>Mail: </b><a href=mailto:"; echo 

"$mail"; echo ">"; echo "$mail"; echo "</a></td>"; }
	if ($www <> " ") { echo "<td width=50%><p align=left><a href="; echo "$www"; echo ">"; 

echo "$www"; echo "</a></td></tr>"; }
	echo "<tr><td colspan=2>"; echo "$greeting"; echo "<br></td></tr>";
	if ($ip <> " ") { echo "<tr><td colspan=2><center><p class=postedby>Posted from "; echo 

"$ip"; echo "</p>"; }
	echo "</td></tr></table>";
}
echo "
<br>
</td></tr></table>
</BODY></HTML>";



?>

Code: Select all

<?php

// phpSimpleGuestbook v1.0
// Copyright (c)2002 Svein-Magnus Sorensen
// First Created the 29th of March 2002
// Latest version from the 23rd of May 2002
// URL: http://devel.menneske.org/
// Contact: sveins@tihlde.org
// 
// This Software is distributed under the GNU General Public             #
// License. For more details see license.txt                             #

$p = $HTTP_POST_VARS;
$success = false;
       $sep = "
";
       $d = getdate();
       $dato = $dї"weekday"] . " " . $dї"month"] . " " . $dї"mday"] . " " . $dї"year"] . " - " . 

$dї"hours"] . ":" . $dї"minutes"] . ":" . $dї"seconds"];
       $ip = $HTTP_SERVER_VARSї"REMOTE_ADDR"];
       $ip = gethostbyaddr($ip);
       if ($ip == "") {
       	$ip = " ";
       }
       $greet = str_replace ("\n" , "
" , $pї"greet"] );
       $greet = str_replace ("\r" , "
" , $greet );
       if ($pї"www"] == "http://" || $pї"www"] == "") {
       	$pї"www"] = " ";
       }
       if ($pї"navn"] == "") {
       	$pї"navn"] = " ";
       }
       if ($pї"sted"] == "") {
       	$pї"sted"] = " ";
       }
       if ($pї"mail"] == "") {
       	$pї"mail"] = " ";
       }
       
       $entry = "\n" . $ip . $sep . $dato . $sep . $pї"navn"] . $sep . $pї"sted"] . $sep . 

$pї"mail"] . $sep . $pї"www"] . $sep . $greet;

       if ($greet <> "" && $pї"navn"] <> " " ) {
       
       if (is_readable("guestbook.dat") && is_writeable("guestbook.dat")) {
		$file = fopen("guestbook.dat", "a");
		fwrite($file, $entry);
            $success = fclose($file);
	 }}

       

if ($success == true) {
              
       echo "
              <HTML>
              <HEAD>  
               <TITLE>phpSimpleGuestbook 1.0</TITLE>  
               <meta http-equiv="pragma" content="no-cache">
               <META HTTP-EQUIV="Refresh" CONTENT="0; URL=index.php"> 
               <LINK REL=StyleSheet HREF="../scroll.css" TYPE=text/css TITLE=Style>
              </HEAD>
               
               <body><br><br><br><br><br><center><h1>Success: Guestbook 

updated</h1></body></html>
       ";
} else { 
       echo "
              <HTML>
              <HEAD>  
               <TITLE>phpSimpleGuestbook 1.0</TITLE>  
               <meta http-equiv="pragma" content="no-cache">
               <META HTTP-EQUIV="Refresh" CONTENT="2; URL=index.php"> 
               <LINK REL=StyleSheet HREF="../scroll.css" TYPE=text/css TITLE=Style>
              </HEAD>
               
               <body><br><br><br><br><br><center><h1>FAILED TO UPDATE GUESTBOOK!</h1>
               Remember name and entry are required fields. </body></html>
       ";
}
?>
The script has worked on other servers... do I need to make change any of the file attributes? I'm hosting it on my computer with Apache and PHP... I'm using WinXP SP2.

Brian


feyd | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Ah you are writing to a file. Set the permissions to full. chmod 0777 should work. I think it would be wise to use a database for this project.
bst82551
Forum Newbie
Posts: 15
Joined: Wed Dec 29, 2004 12:25 pm
Location: Huntsville, Texas

problems

Post by bst82551 »

i'm hosting on winxp sp2, i can't chmod... sorry. I tried removing the archive and read-only attributes... nothing happened. It still won't work. I would use a database, but I seem to have a lot of problems with installing mysql. I've tried installing it about 5 different times... every time I failed in the installation. Maybe I should try one more time. Yeah, maybe. But still- i can't chmod... and nothing happened when i removed those attributes... so, what's wrong?

Brian
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

Post by Bon Bon »

You have nothing checking to see if any information has been submitted. If nothing is checking for this how is PHP supposed to know what you want to do when the information is posted?

Code: Select all

if (isset($_POSTї'submit'])) {
  // do something
}

else {
  // show the form
}
You will also need to change:

Code: Select all

<tr><td colspan=4><center><input type=reset value="Reset Fields"> <input 

type=submit value="Post message"></center></td></tr>
to:

Code: Select all

<tr><td colspan=4><center><input type=reset value="Reset Fields"> <input 

type=submit value="Post message" name="submit"></center></td></tr>
Last edited by Bon Bon on Wed Dec 29, 2004 12:55 pm, edited 1 time in total.
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

You should be able to set permissions no matter what OS you are using. Why not use a database instead?, I can guarantee that the code would be shorter and more reliable :D
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Bon Bon wrote:You have nothing checking to see if any information has been submitted. If nothing is checking for this how is PHP supposed to know what you want to do when the information is posted?

Code: Select all

if (isset($_POST['submit'])) {
  // do something
}

else {
  // show the form
}
You can do that but setting the file permissions is most important for now.
Bon Bon
Forum Commoner
Posts: 66
Joined: Sat Mar 13, 2004 10:21 pm
Location: UK

Post by Bon Bon »

What server are you using? IIS or Apache?
bst82551
Forum Newbie
Posts: 15
Joined: Wed Dec 29, 2004 12:25 pm
Location: Huntsville, Texas

Post by bst82551 »

apache 2.052 w/ php 5.02
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

And you are unable to set permissions?. I am finding that hard to believe. Put it this way, if you are unable to do that then you had better think about rewriting your code for database support.
bst82551
Forum Newbie
Posts: 15
Joined: Wed Dec 29, 2004 12:25 pm
Location: Huntsville, Texas

Post by bst82551 »

you can't really set permissions on windows. linux lets you chmod, but windows doesn't. There are 3 different file attributes- read-only, hidden, and archive. I have all 3 unchecked. In my experience, this has caused certain perl scripts to work when they didn't when they were archive. So.. I have everything unchecked. It should work.

Brian
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I've said it time and time again, do not rely on the submit button being in the submission.

the ACL exists in Windows too.. .. php doesn't know how to manipulate it though.
Post Reply