Jump to content

[SOLVED] Parse Error


magebash

Recommended Posts

I am pretty new to php and I get this error with the following script:

<script>
if (navigator.cookieEnabled == 0) {
  document.write("<b><font color='red'">You need to enable cookies for this site to work properly.  Please enable them.</b>");
}
</script>
<form action="index.php" method="POST">
<input type="text" name="word">
</form>
<?php if (isset($_POST['word']))
setcookie("word","$_POST['word']", time()+3600);
die "If you like the look of this word, click the button confirm word.  Otherwise, <a href="index.php">Click Here</a><p><form action="addcloud.php" method="post"><input type="submit" name="continue" value=" Confirm Word "></form>";
?>

It returns with this error:

 

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/www/magebash.100webspace.net/classifieds2/index.php on line 10

 

Thanks.

Link to comment
Share on other sites

Whenever you want to include variables inside of double-quoted strings, enclose the variable in curly brackets:

$name = "Mike";
echo "Hello, {$name}";

Don't be lazy and do it only occasionally.  Do it every time.

 

Also, whichever tutorial or book you are reading has told you that for if-statements where the body is a single line the curly brackets are optional.  This is true.  However, do not leave them off.  Ever.

if(true)
  echo "hi";

Should be

if(true){
  echo "hi";
}

Again, don't be lazy.  Always use them.

 

As for your parse error, look at this line:

die "If you like the look of this word, click the button confirm word.  Otherwise, <a href="

The string effectively ends when it encounters that double quote near the href.  If you want to include double-quotes as part of a double-quoted string, you have to escape them with a backslash.

 

echo "This string contains a \"!"; // <-- note the \"

Link to comment
Share on other sites

I changed it to this, but it comes up with the same errors.

<?php if (isset($_POST['word'])) {
setcookie("word","$_POST['word']", time()+3600);
die "If you like the look of this word, click the button confirm word.  Otherwise, <a href='index.php'>Click Here</a><p><form action='addcloud.php' method='post'><input type='submit' name='continue' value=' Confirm Word '></form>";
}
?>

Link to comment
Share on other sites

My code is:

<?php if (isset($_POST['word'])) {
setcookie('word','$_POST['word']',time()+3600);
die("If you like the look of this word, click the button confirm word.  Otherwise, <a href='index.php'>Click Here</a><p><form action='addcloud.php' method='post'><input type='submit' name='continue' value=' Confirm Word '></form>");
}
?>

And it comes up with this:

Parse error: parse error, unexpected T_STRING in /home/www/magebash.100webspace.net/classifieds2/index.php on line 10

Link to comment
Share on other sites

setcookie('word','$_POST['

Your single quote which starts the index into the array is ending the single-quoted string.  Think about it like this.  If you start a string with a single-quote, the next single-quote the interpretor finds will end the string.

 

Either do what I originally said, which was to enclose the variable in curly brackets inside of a double-quoted string:

$name = "Mike";
echo "Hello, {$name}!";

 

Or do what PHP Monkeh suggested and remove the quotes around the $_POST variable altogether (which is actually the better solution in this instance):

setcookie('word',$_POST['word'],time()+3600);

Link to comment
Share on other sites

I have another problem now.  The script is this.

<?php if(isset($_POST['submit'])) 
setcookie('word',$_POST['word'],time()+3600);
die("If you like the look of this word, click the button confirm word.  Otherwise, <a href='index.php'>Click Here</a><p><form action='addcloud.php' method='post'><input type='submit' name='continue' value=' Confirm Word '></form>");
?>
<script>
if (navigator.cookieEnabled == 0) {
  document.write("<b><font color='red'">You need to enable cookies for this site to work properly.  Please enable them.</b>");
}
</script>
<form action="index.php" method="POST">
<input type="text" name="word">
<input type="submit" name="submit" value="Post Word">
</form>

 

All that happens is the die function shows up without submitting the form.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.