magebash Posted January 25, 2008 Share Posted January 25, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/87839-solved-parse-error/ Share on other sites More sharing options...
roopurt18 Posted January 25, 2008 Share Posted January 25, 2008 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 \" Quote Link to comment https://forums.phpfreaks.com/topic/87839-solved-parse-error/#findComment-449327 Share on other sites More sharing options...
magebash Posted January 25, 2008 Author Share Posted January 25, 2008 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>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/87839-solved-parse-error/#findComment-449331 Share on other sites More sharing options...
PHP Monkeh Posted January 25, 2008 Share Posted January 25, 2008 setcookie("word","$_POST['word']", time()+3600); remove the " " around $_POST['word'] pretty please Quote Link to comment https://forums.phpfreaks.com/topic/87839-solved-parse-error/#findComment-449332 Share on other sites More sharing options...
magebash Posted January 25, 2008 Author Share Posted January 25, 2008 I did that and it comes up with this... Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/www/magebash.100webspace.net/classifieds2/index.php on line 11 Quote Link to comment https://forums.phpfreaks.com/topic/87839-solved-parse-error/#findComment-449333 Share on other sites More sharing options...
roopurt18 Posted January 25, 2008 Share Posted January 25, 2008 Try enclosing the string for the die in parens. die("message blah blah"); Quote Link to comment https://forums.phpfreaks.com/topic/87839-solved-parse-error/#findComment-449339 Share on other sites More sharing options...
magebash Posted January 25, 2008 Author Share Posted January 25, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/87839-solved-parse-error/#findComment-449343 Share on other sites More sharing options...
kenrbnsn Posted January 25, 2008 Share Posted January 25, 2008 On this line: <?php setcookie('word','$_POST['word']',time()+3600); ?> you were told to remove the double quotes, not to replace them with single quotes. Try: <?php setcookie('word',$_POST['word'],time()+3600); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/87839-solved-parse-error/#findComment-449350 Share on other sites More sharing options...
roopurt18 Posted January 25, 2008 Share Posted January 25, 2008 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); Quote Link to comment https://forums.phpfreaks.com/topic/87839-solved-parse-error/#findComment-449353 Share on other sites More sharing options...
magebash Posted January 25, 2008 Author Share Posted January 25, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/87839-solved-parse-error/#findComment-449362 Share on other sites More sharing options...
magebash Posted January 25, 2008 Author Share Posted January 25, 2008 nvm I have fixed the problem. Just a stupid error. Quote Link to comment https://forums.phpfreaks.com/topic/87839-solved-parse-error/#findComment-449367 Share on other sites More sharing options...
roopurt18 Posted January 25, 2008 Share Posted January 25, 2008 Just a stupid error. Most of them are Quote Link to comment https://forums.phpfreaks.com/topic/87839-solved-parse-error/#findComment-449369 Share on other sites More sharing options...
magebash Posted January 26, 2008 Author Share Posted January 26, 2008 Thanks for the help everybody! Quote Link to comment https://forums.phpfreaks.com/topic/87839-solved-parse-error/#findComment-449372 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.