Hello....I need some help :)

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

Post Reply
drifter
Forum Newbie
Posts: 4
Joined: Mon Oct 13, 2003 6:25 am

Hello....I need some help :)

Post by drifter »

Hi all

I wrote some php code (I am a beginner at this) and part of it won't work. Access to DB works well, create, update, view of records works but what doesn't work is when I cklick the link where it should pass a variable to itself...and reload itself and to the other thing with the var set. It won't work....do I have to set something on my web server (apache) so it works?

echo("<p><a href='$PHP_SELF?addjoke=1'>Add joke</a></p>");
that is the code that acctually won't work but I send the complete code, maybe the error is somewhere else..

Thanks for the help, the code is below.

d.

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>PHP testing page</title>
</head>

<body>

<?php
//if the user wants to add a joke
if (isset($addjoke))
{
?>

<form action="<?php echo($PHP_SELF); ?>" METHOD=POST>
<p>Upisi vic ovdje:<br>
<textarea name="joketext" ROWS=10 COLS=40 WRAP></textarea><br>
<input type=submit name="submitjoke" VALUE="SUBMIT">
</p>
</form>

<?php
}
else
{
	//Connect to DB server
	$dbcnx = @mysql_connect("localhost");
	if (!$dbcnx)
	{
		echo("<p> Unable to connect to DB server! </p>");
		exit();
	}
	else
	{
//		echo("<p> Connection established! </p>");
	}
	
	if(! @mysql_select_db("jokes",$dbcnx))
	{
		echo("<p> Couln't find the database!</p>");
		exit();
	}
	else
	{
//		echo("<p> Database found! </p>");
	}
	
	//If a joke has been submited add it to DB
	if ("SUBMIT" == $submitjoke)
	{
		$sql = "insert into jokes set JokeText = '$joketext', JokeDate = CURDATE()";
		$result = mysql_query($sql);
		if($result)
		{
//			echo("<p> Query sucessfull! </p>");
		}
		else
		{
			echo("<p> Query failed! </p>");
			echo("<p> error: " . mysql_error() . " </p>");
		}
	}
	
	echo("<p> Jokes in DB </p>");
	
	$result = mysql_query("select JokeText from Jokes");
	if($result)
	{
//		echo("<p> Query sucessfull! </p>");
		while($row = mysql_fetch_array($result))
		{
			echo("<p> " . $row["JokeText"] . " </p>");
		}
	}
	else
	{
		echo("<p> Query failed! </p>");
		echo("<p> error: " . mysql_error() . " </p>");
	}
	
	//When clicked this link will load this page with the joke subbmission form displayed
	if(!empty($addjoke))
	{
		echo("<p>addjoke set</p>");
	}
	else
	{
		echo("<p>addjoke NOT set</p>");
	}
	echo("<p><a href='$PHP_SELF?addjoke=1'>Add joke</a></p>");
	
}
?>



</body>
</html>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

drifter
Forum Newbie
Posts: 4
Joined: Mon Oct 13, 2003 6:25 am

Post by drifter »

I did read it but I am still puzzled because I don't know where is my error? Because the METHOD=POST never gets called since the
?addjoke=1 doesn't set the var.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if viewtopic.php?t=511 applies to your version of php (<?php phpinfo(); ?> will tell you) $PHP_SELF does not exist anymore. You have to use $_SERVER['PHP_SELF']
anything you pass via GET (like <a href='blablabl.php?addjoke=1'>) you have to fetch via the superglobal array $_GET (e.g. $_GET['addjoke'] )
Anything you POST (like <form action="blablabl.php" METHOD="POST"> ) you have to fetch via the superglobal $_POST (e.g. $_POST['joketext'])

Somewhere between $joketext and $_POST['joketext'] is the superglobal $_REQUEST, it contains (among other things) the elements of $_GET and $_POST. You can access the parameters regardless of wether they have been passed via GET or POST but you cannot tell the methods apart from each other (just like the old $joketext).

see also: http://www.php.net/manual/en/reserved.variables.php
drifter
Forum Newbie
Posts: 4
Joined: Mon Oct 13, 2003 6:25 am

Post by drifter »

thank you very much, now it is much clearer...I am new at this so didn't quite get in what case this should be used, but as I see in both :)

once more thanks !

d.
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

you also might want to look into heredocs, and if you're going to make many pages, look into modularity and using includes.

we can give you pointers if you need it, but search php.net for echo and read about the heredocs, and if you're unsure of what modularity is, just ask (or search google)
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Heredoc? hehe. If there's anything to do with heredoc look at me :D

-Nay
Post Reply