I'm working on a basic site for a beginner-level web design class I'm in, and I'm using some of my php knowledge to go above and beyond, so to speak. However, I'm having some problems. My index page has a simple post form that asks the user's name. Simple enough. I have the page the submit button links to (welcome.php) set $_POST to $_COOKIE["name"]. That works well enough for my purposes. I then edited the index page to include an if/else statement so it doesn't display the input form if the cookie is set. This is where my problems start. Here's the offending chunk of code. <?php if (isset ($_COOKIE["name"])) { echo "Welcome back," . $_COOKIE["name"] . ". Click <a href="welcome.php">Here</a> to continue." ; } else { echo "<form action="welcome.php" method="cookie">Please enter your name: <input type="text" name="name" /><input type="submit"/></form>"; } ; ?> PHP: I know my syntax is probably really gross, but I'm a beginner. My problem is that I keep getting a parse error on the "Welcome back," line. The error is: I have a semicolon after the echo there, you can see it for yourself. Any ideas? I'm completely lost.
HI there, Try this: <?php if (isset ($_COOKIE["name"])) { echo "Welcome back," . $_COOKIE["name"] . ". Click <a href='welcome.php'>Here</a> to continue." ; } else { echo "<form action='welcome.php' method='cookie'>Please enter your name: <input type='text' name='name' /><input type='submit'/></form>"; } ?> PHP: You can't use quotes inside quotes. Instead, you could either escape them (as suggested by lp1051 below) or replace them with single quotes (').
You need to escape the quotes inside quotes, if you want do echo quote (visit php.net): echo "Welcome back," . $_COOKIE["name"] . ". Click <a href=\"welcome.php\">Here</a> to continue." ; echo "<form action=\"welcome.php\" method=\"cookie\">Please enter your name: <input type=\"text\" name=\"name\" /><input type=\"submit\"/></form>";
Another thing that PHP lets you do is allow you to close and re open php tags as if the script never ended. For example: <?php echo " ?> some html here <a href="#">link</a> <?php "; //continuing the script here if ($blabla) { echo "foobar"; } ?> PHP: Is the same as: <?php echo "some html here <a href=\"#\">link</a>"; //continuing the script here if ($blabla) { echo "foobar"; } ?> PHP:
Thanks. Escaping is a very good tool that I have recently thoguht of. I spent 5 hours of trying to debug something while in the book right next to me teaches escaping quotes in the second Chapter!