Trouble with PHP_SELF, I think
Moderator: General Moderators
Trouble with PHP_SELF, I think
I am having trouble with the attached code. When I select the Add Joke link, echo("<p><a href='$PHP_SELF?addjoke=0'>Add a Joke!</a></p>");, it does not show the textarea to add a joke. It just brings up the original page with the new address, http://localhost/jokes.php?addjoke=0.
Can someone help?
Thanks
<html>
<head>
<title> The Internet Joke Database </title>
</head>
<body>
<?php
if (isset($addjoke)): // If the user wants to add a joke?>
<form action="<?php echo($_SERVER("PHP_SELF"))?>" method="post">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40" wrap></textarea><br />
<input type="submit" name="submitjoke" value="SUBMIT" /></p>
</form>
<?php
else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "", "");
if (!$dbcnx) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "<p>Unable to locate the joke " .
"database at this time.</p>" );
exit();
}
// If a joke has been submitted,
// add it to the database.
if ($submitjoke == "SUBMIT") {
$sql = "INSERT INTO Jokes SET
JokeText='$joketext',
JokeDate=CURDATE()";
if (@mysql_query($sql)) {
echo("<p>Your joke has been added.</p>");
} else {
echo("<p>Error adding submitted joke: " .
mysql_error() . "</p>");
}
}
echo("<p> Here are all the jokes in our database: </p>");
// Request the text of all the jokes
$result = @mysql_query("SELECT JokeText FROM Jokes");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("<p>" . $row["JokeText"] . "</p>");
}
// When clicked, this link will load this page
// with the joke submission form displayed.
echo("<p><a href='$PHP_SELF?addjoke=0'>Add a Joke!</a></p>");
endif;
?>
</body>
</html>
Can someone help?
Thanks
<html>
<head>
<title> The Internet Joke Database </title>
</head>
<body>
<?php
if (isset($addjoke)): // If the user wants to add a joke?>
<form action="<?php echo($_SERVER("PHP_SELF"))?>" method="post">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40" wrap></textarea><br />
<input type="submit" name="submitjoke" value="SUBMIT" /></p>
</form>
<?php
else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "", "");
if (!$dbcnx) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "<p>Unable to locate the joke " .
"database at this time.</p>" );
exit();
}
// If a joke has been submitted,
// add it to the database.
if ($submitjoke == "SUBMIT") {
$sql = "INSERT INTO Jokes SET
JokeText='$joketext',
JokeDate=CURDATE()";
if (@mysql_query($sql)) {
echo("<p>Your joke has been added.</p>");
} else {
echo("<p>Error adding submitted joke: " .
mysql_error() . "</p>");
}
}
echo("<p> Here are all the jokes in our database: </p>");
// Request the text of all the jokes
$result = @mysql_query("SELECT JokeText FROM Jokes");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("<p>" . $row["JokeText"] . "</p>");
}
// When clicked, this link will load this page
// with the joke submission form displayed.
echo("<p><a href='$PHP_SELF?addjoke=0'>Add a Joke!</a></p>");
endif;
?>
</body>
</html>
- daven
- Forum Contributor
- Posts: 332
- Joined: Tue Dec 17, 2002 1:29 pm
- Location: Gaithersburg, MD
- Contact:
## Line 6: a) there should be a '{', not a ':' after the if statement; b) you need to use $_GET['addjoke'] to access the URL variable
## Line 7: it should be $_SERVER['PHP_SELF'] (brackets & single quotes)
## line 58: a) use . to concatenate stuff b) escape double quotes
You can use $_SERVER['PHP_SELF'] or $PHP_SELF. Either/both are acceptable. I tend to use $_SERVER['PHP_SELF'] because I like explicit variable names and I also jump between languages a lot so it is easier for me to read.
Code: Select all
<?php
if (isset($_GET['addjoke'])){ // If the user wants to add a joke?>Code: Select all
<form action="<?php echo($_SERVER['PHP_SELF'])?>" method="post">Code: Select all
<?php
echo("<p><a href="".$PHP_SELF."?addjoke=0">Add a Joke!</a></p>");
?>Parse error: parse error, unexpected T_ELSE
Now I am getting this error:
Parse error: parse error, unexpected T_ELSE in C:\Program Files\Apache Group\Apache2\htdocs\jokes.php on line 19
Parse error: parse error, unexpected T_ELSE in C:\Program Files\Apache Group\Apache2\htdocs\jokes.php on line 19
Textarea to show up on same page
The GET method brings the text area up on a different page. I want it to show up on the same page. Also, when using the get method and filling out the textarea, it still does not update the database.
Please Help!
Please Help!
http://www.php.net/manual/en/control-st ... syntax.php
is alright; one suggestion: choose one syntax and stay with it (and choose the the C-like style
)
$_SERVER("PHP_SELF") that would call a function with the parameter PHP_SELF (and $_SERVER would contain the name/reference of that function).
$_SERVER['PHP_SELF'] most likely is what you want.
Read http://www.php.net/manual/en/reserved.variables.php to learn more about the superglobal arrays defeined in php.
Also read viewtopic.php?t=511 to see wether isset($addjoke) is ok for your version of php
Code: Select all
if (condition):
...
elseif:
...
else:
...
endif;$_SERVER("PHP_SELF") that would call a function with the parameter PHP_SELF (and $_SERVER would contain the name/reference of that function).
$_SERVER['PHP_SELF'] most likely is what you want.
Read http://www.php.net/manual/en/reserved.variables.php to learn more about the superglobal arrays defeined in php.
Also read viewtopic.php?t=511 to see wether isset($addjoke) is ok for your version of php
yes it's a request for a complete new document.The GET method brings the text area up on a different page.
- daven
- Forum Contributor
- Posts: 332
- Joined: Tue Dec 17, 2002 1:29 pm
- Location: Gaithersburg, MD
- Contact:
The $_GET method is for when you submit things with form method="get" or do a direct url encode. When you use form method="post" you need to use $_POST. Try something like the following:
Go read viewtopic.php?t=511 for info on variable passing.
Hope this helps.
Code: Select all
<?php
// Form to enter joke
if (isset($_GET['addjoke'])){?>
<form action="<?php echo($_SERVER['PHP_SELF'])?>" method="post">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40" wrap></textarea>
<br />
<input type="submit" name="submitjoke" value="SUBMIT" /></p>
</form>
<?php }?>
<?php // to process joke upload
if (isset($_POST['joketext'])) { // check to see if a joke has been posted
$sql = "INSERT INTO Jokes SET JokeText='$joketext', JokeDate=CURDATE()";
mysql_query($sql,$dbcnx) or die(mysql_error());
}
?>
<?php // the link to add a new joke
echo("<p><a href="".$_SERVER['PHP_SELF']."?addjoke=0">Add a Joke!</a></p>");
?>Hope this helps.
doesn't seem like section of code is working
The get method brings up the textarea page, and I can submit the text. But the database is not being updated. Do you have to use Post for this to occur? When I try to use Post nothing happens. I bolded the section of code that is not executing.
Any suggestions?
<html>
<head>
<title> The Internet Joke Database </title>
</head>
<body>
<?php
if (isset($_POST['addjoke'])): // If the user wants to add a joke?>
<form action="<?php echo($_SERVER['PHP_SELF'])?>" method="POST">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40" wrap></textarea><br />
<input type="submit" name="submitjoke" value="SUBMIT" /></p>
</form>
<?php
else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "", "");
if (!$dbcnx){
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "<p>Unable to locate the joke " .
"database at this time.</p>" );
exit();
}
// If a joke has been submitted,
// add it to the database.
if ($submitjoke == "SUBMIT"){
$sql = "INSERT INTO Jokes SET
JokeText='$joketext',
JokeDate=CURDATE()";
if (@mysql_query($sql)) {
echo("<p>Your joke has been added.</p>");
}
else{
echo("<p>Error adding submitted joke: " .
mysql_error() . "</p>");
}}echo("<p> Here are all the jokes in our database: </p>");
// Request the text of all the jokes
$result = @mysql_query("SELECT JokeText FROM Jokes");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("<p>" . $row["JokeText"] . "</p>");
}
// When clicked, this link will load this page
// with the joke submission form displayed.
echo("<p><a href=\"".$PHP_SELF."?addjoke=1\">Add a Joke!</a></p>");
endif;
?>
</body>
</html>
Any suggestions?
<html>
<head>
<title> The Internet Joke Database </title>
</head>
<body>
<?php
if (isset($_POST['addjoke'])): // If the user wants to add a joke?>
<form action="<?php echo($_SERVER['PHP_SELF'])?>" method="POST">
<p>Type your joke here:<br />
<textarea name="joketext" rows="10" cols="40" wrap></textarea><br />
<input type="submit" name="submitjoke" value="SUBMIT" /></p>
</form>
<?php
else: // Default page display
// Connect to the database server
$dbcnx = @mysql_connect("localhost", "", "");
if (!$dbcnx){
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
// Select the jokes database
if (! @mysql_select_db("jokes") ) {
echo( "<p>Unable to locate the joke " .
"database at this time.</p>" );
exit();
}
// If a joke has been submitted,
// add it to the database.
if ($submitjoke == "SUBMIT"){
$sql = "INSERT INTO Jokes SET
JokeText='$joketext',
JokeDate=CURDATE()";
if (@mysql_query($sql)) {
echo("<p>Your joke has been added.</p>");
}
else{
echo("<p>Error adding submitted joke: " .
mysql_error() . "</p>");
}}echo("<p> Here are all the jokes in our database: </p>");
// Request the text of all the jokes
$result = @mysql_query("SELECT JokeText FROM Jokes");
if (!$result) {
echo("<p>Error performing query: " . mysql_error() . "</p>");
exit();
}
// Display the text of each joke in a paragraph
while ( $row = mysql_fetch_array($result) ) {
echo("<p>" . $row["JokeText"] . "</p>");
}
// When clicked, this link will load this page
// with the joke submission form displayed.
echo("<p><a href=\"".$PHP_SELF."?addjoke=1\">Add a Joke!</a></p>");
endif;
?>
</body>
</html>
have read viewtopic.php?t=511 carefully?
If not: please do so, certainly it will answer your question
If not: please do so, certainly it will answer your question
- daven
- Forum Contributor
- Posts: 332
- Joined: Tue Dec 17, 2002 1:29 pm
- Location: Gaithersburg, MD
- Contact:
Aside from passing variables, your SQL statement is flawed (sorry, I did not catch this earlier).
What you have:
What it should be:
The syntax for an insert statement is INSERT INTO table(column1, column2, . . ., columnN) VALUES(value1, value2, . . . , valueN).
The syntax for an update statement is UPDATE table SET column1=value1, column2=value2, . . . , columnN=valueN WHERE condition.
You were mixing up the statements.
What you have:
Code: Select all
$sql = "INSERT INTO Jokes SET
JokeText='$joketext',
JokeDate=CURDATE()";Code: Select all
$sql="INSERT INTO Jokes(JokeText,JokeDate) VALUES('".$joketext."', '".CURDATE()."')";The syntax for an update statement is UPDATE table SET column1=value1, column2=value2, . . . , columnN=valueN WHERE condition.
You were mixing up the statements.
http://www.mysql.com/doc/en/INSERT.html
or INSERT [LOW_PRIORITY | DELAYED] [IGNORE]
[INTO] tbl_name
SET col_name=(expression | DEFAULT), ...
[ ON DUPLICATE KEY UPDATE col_name=expression, ... ]