Forum Moderators: coopster

Message Too Old, No Replies

A Parse error? Unexpected '{' on line 15

hmm...i guess i'll need a book soon...

         

pantheros

6:50 am on Dec 12, 2004 (gmt 0)

10+ Year Member



Okay. So pretty much i'm just taking exampls of scripts from other peoples' playing around and i'm trying to figure out how to gather bits and pieces and use them for my own. This thing i'm writing without any real PHP hardcore knowledge, just fiddlings and coding bits that do specific things. I'm writing a page where someone enters an answer, and there are multiple possible answers, rather than one, and i want it to just refresh and give them the answer.

I'm getting a parse error in the following page.

trivia.php


<? include('cookiecheck.php');?>
<? include($headervar.$skin.$extension);?>

<?php

$str = "Gentry Inc Graphics is run by Pantheros";
str_rot13($str);

$ans='answer';

if (isset($_POST['submit'])) { // if form has been submitted

if(!$ans && $ans == "") {
echo "Try answering the question, please.";
} elseif ($ans == "str_rot13($str);" {
"Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "gentry inc graphics is run by pantheros") {
"Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "pantheros") {
"Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "Pantheros") {
"Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "the webmistress") {
"Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} else {
echo "I'm sorry, your answer is incorrect. :( Please do try again, another trivia question should be up very soon!<p>Thanks for trying!";
}
}
?>

<?php

} else {// if form hasn't been submitted

?>

<p class=title>Trivia question!</p>
<p>
If you can answer this question correctly, you'll be told the prize you'll receive once you give the correct answer. If not, then

i'm sorry, you'll have to try again at another time.<p>
<b>Question is::</b><p>
Who is this site run by?<p>
The answer can be found in the string below.<p>
<? echo "$str";?>
<p>
Your answer is::</br>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" name="answer" maxlength="150" size="17">
<?php
}
?>

<? include($footervar.$skin.$extension);?>

Any idea what my problem might be? I'm not very good at troubleshooting PHP yet. : / Much less writing the stuff. Heheh.

dkin

9:54 am on Dec 12, 2004 (gmt 0)

10+ Year Member



I am very new at this as well and have not looked at any php for quite sometime.

try to delete the } here

} else {// if form hasn't been submitted

Salsa

11:56 am on Dec 12, 2004 (gmt 0)

10+ Year Member



There is a problem here...on line 15 ;)

} elseif ($ans == 
"
str_rot13($str)
;" {

See how the parentheses don't match up on your elseif? The quotes and semicolin aren't helping either.

Try:

} elseif ($ans == str_rot13($str)[red][b])[/b][/red] {

After you fix that, the next error you're likely to get is the one that dkin pointed out. You closed the first if condition too many times. Try deleting the } that dkin suggested or the next one up.

I wish you well,

pantheros

8:17 pm on Dec 13, 2004 (gmt 0)

10+ Year Member



How can i tell the browser in the last elseif statement to echo the message if the answer is anything other than what i've already listed? As an example, as if someone answered the question with 'dog' instead of any of the correct answers.

Okay, i changed the script, which now looks like the following, but now i'm getting "Parse error: parse error, unexpected '{', expecting '(' in /trivia.php on line 25".


<? include('cookiecheck.php');?>
<? include($headervar.$skin.$extension);?>

<?php

$str = "Gentry Inc Graphics is run by Pantheros";
str_rot13($str);

$ans='answer';

if (isset($_POST['submit'])) { // if form has been submitted

if(!$ans && $ans == "") {
echo "Try answering the question, please.";
} elseif ($ans == str_rot13($str)) {
"Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "gentry inc graphics is run by pantheros") {
"Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "pantheros") {
"Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "Pantheros") {
"Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "the webmistress") {
"Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif {
echo "I'm sorry, your answer is incorrect. :( Please do try again, another trivia question should be up very soon!<p>Thanks for trying!";
}
?>

<?php

else {// if form hasn't been submitted
?>
<p class=title>Trivia question!</p>
<p>
If you can answer this question correctly, you'll be told the prize you'll receive once you give the correct answer. If not, then i'm sorry, you'll have to try again at another time.<p>
<b>Question is::</b><p>
Who is this site run by?<p>
The answer can be found in the string below.<p>
<?php echo "$str";?>
<p>
Your answer is::</br>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" name="answer" maxlength=150 size=17>

<? include($footervar.$skin.$extension);?>

Jack_Hughes

9:42 pm on Dec 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am a long way from being an expert in php but...

your first if statement is never closed (no }).

your final elseif in the second if statement doesn't have a conditional statement ie the bit between the ().

you can do a else statement in an if statement where you are using else ifs.

so, for example:

if (something) {
// statements
} else if (something_else) {
// more statements
} else {
// catchall statements
}

olwen

10:06 pm on Dec 13, 2004 (gmt 0)

10+ Year Member



Note that you are missing the echo in several lines as well. That may give you errors until you fix it.

pantheros

3:15 am on Dec 14, 2004 (gmt 0)

10+ Year Member



Okay. I've changed it up some more and now it looks like below, but i'm now getting the following error: "Parse error: parse error, unexpected $ in trivia.php on line 44"


<? include('cookiecheck.php');?>
<? include($headervar.$skin.$extension);?>

<?php

$str = "Gentry Inc Graphics is run by Pantheros";

$ans='answer';

if (isset($_POST['submit'])) { // if form has been submitted
$answered = 1;
if(!$ans && $ans == "") {
echo "Try answering the question, please.";}
elseif ($ans == "gentry inc graphics is run by pantheros") {
echo "Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "pantheros") {
echo "Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "Pantheros") {
echo "Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "the webmistress") {
echo "Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} else {
echo "I'm sorry, your answer is incorrect. :( Please do try again, another trivia question should be up very soon!<p>Thanks for trying!";
}
?>

<?php

} else {
?>
<p class=title>Trivia question!</p>
<p>If you can answer this question correctly, you'll be told the prize you'll receive once you give the correct answer. If not, then i'm sorry, you'll have to try again at another time.<p>
<b>Question is::</b>
<p>Who is this site run by:<p>
<p>Your answer is::</br>
<form method="post" action="echo "<? echo $_SERVER['PHP_SELF']?>"><input type="text" name="answer" size="17">

<? include($footervar.$skin.$extension);?>

NOW what's wrong with it? -.-;

Salsa

3:43 am on Dec 14, 2004 (gmt 0)

10+ Year Member



Well, the good news is that the higher the line numbers where your fatal errors are, the closer you're getting! The bad news is that your error is on (or just above) line 44, but you only posted 38 lines of code ;)

Post just the next 10 lines, and well see what we've got--or find the $ sign on line 44, and trace backwards, looking closely for a missing piece of punctuation.

pantheros

3:47 am on Dec 14, 2004 (gmt 0)

10+ Year Member



Well, problem is, that that's ALL the coding on the page. My site's been doing funny things with codes that would usually be totally fine. I have no clue though. Earlier on, i was getting errors on lines 64 and such. : /

Salsa

3:58 am on Dec 14, 2004 (gmt 0)

10+ Year Member



Well, if that's all the coding there is, then that's part of the problem. For example, you never closed the "else {" on line 29. It has to be closed someplace. What is the code in $footervar.$skin.$extension that you included on the last line? You might have to look in cookiecheck.php (which I suspect does more than check cookies) to see what the values are for the variables used for the include files.

pantheros

5:16 am on Dec 14, 2004 (gmt 0)

10+ Year Member



Weehee! Thank you! Okay, i closed the else { on line 29. Now at least the content shows up. The next problem is that the browser tries to look for "http://url.com/echo" instead of echoing the statement. How can i fix this problem? If i can fix that, then i think i should be pretty much set, aside from setting it up so that a user can only answer the question once.

Salsa

11:33 am on Dec 14, 2004 (gmt 0)

10+ Year Member



I'm glad you can finally see something! Now, you might not realize that your program is using at least three other files. That's what the lines that start with "include" mean--they're including other files. One of the files is cookiecheck.php, in the same directory. To see where and what the other two are, try temporarily adding these two lines right after your first <?php line:

echo "header include file is: ".$headervar.$skin.$extension."<br>\n"; 
echo "footer include file is: ".$footervar.$skin.$extension."<br>\n";

...then take a look in whatever the footer include file is. That's where the else condition probably should closed. Also where the form's sumbit button should be, and probably some hidden form fields...

Besides, if I were you, I'd want to know what's in all of the include files. Who knows what sort of mischief they're up to--like connecting to url.com?

pantheros

7:57 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



Heheh, i probably should have explained those earlier.

Those other files are my skinning files. Basically, it's the header and footer HTML of the page, but it only has to load once, making the site load faster for me. It remembers which layout the visitor was using last. It also makes layout changing muuuuch easier on me. Cookiecheck.php grabs the cookie from the user for the layout and starts the session for the login script on the site. $headervar.$skin.$extension grabs the header file for the chosen header, and $footervar.$skin.$extension gets the footer info. They're not doing anything bad really.

The echo thing took away the layout information and simply told me which layout i have currently (though it doesn't show).

But why does the elseif (blah == blah) { echo "blahblah" } try to go to url.com/echo?

Salsa

9:10 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



Okay, but then I'm really confused. How are you even submitting this form without a submit button or without even closing the </form> tag?

pantheros

9:18 pm on Dec 14, 2004 (gmt 0)

10+ Year Member



Sorry, that part must've gotten fogotten when i copied it over to here. The code looks like so right now:


<?
include('cookiecheck.php');
include($headervar.$skin.$extension);
?>

<?php

$str = "Gentry Inc Graphics is run by Pantheros";

$ans='answer';

if (isset($_POST['submit'])) { // if form has been submitted
return;
if(!$ans && $ans == "") {
echo "Try answering the question, please.";}
elseif ($ans == "gentry inc graphics is run by pantheros") {
echo "Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "pantheros") {
echo "Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "Pantheros") {
echo "Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} elseif ($ans == "the webmistress") {
echo "Woohoo! You got it right. Now go sMail pantheros the answer you entered to get your Rainbow Star Rod!";
} else {
echo "I'm sorry, your answer is incorrect. :( Please do try again, another trivia question should be up very soon!<p>Thanks for trying!";
}
?>

<?php

} else { }
?>
<p class=title>Trivia question!</p>
<p>If you can answer this question correctly, you'll be told the prize you'll receive once you give the correct answer. If not, then i'm sorry, you'll have to try again at another time.<p>
<b>Question is::</b>
<p>Who is this site run by:<p>
<p>Your answer is::</br>
<form method="post" action="echo "<? echo $_SERVER['PHP_SELF']?>"><input type="text" name="answer" size="17">
<input type="submit" name="submit" value="Submit">
</form>

<? include($footervar.$skin.$extension);?>

It still wants to go to url.com/echo though.

[edit]Found that problem. Now it just shows a blank page when i enter an answer.[/edit]

pantheros

5:45 am on Dec 15, 2004 (gmt 0)

10+ Year Member



Why won't it echo the statements for the elseif conditions? I haven't been able to figure out why it won't echo them, but leaves only a blank page, and after i submit the form, it doesn't load the footer.

Salsa

2:38 pm on Dec 15, 2004 (gmt 0)

10+ Year Member



First, get rid of the
return;
line. When you enter the if condition, that's killing the whole script. Then do some echos, etc. to find your other errors. For example, try temporarily putting an

echo "\$ans = $ans<br>\n";

line before and after the

$ans='answer';
line.

Also, do you understand that the name you use in a form field determines the $variable name retrieved by a script?

I wish you well.