Why is my PHP code wrong?

CorrieDeBeer

Expert Member
Joined
Apr 14, 2015
Messages
1,568
Reaction score
15
I'm trying to teach myself PHP, but something seems to not be working


HTML:
<html>
<head>
<title> 305 Using the TextArea tag</title>
<link rel="stylesheet" type="text/css" href="css/basic_2.css" />
</head>
<body>
<h3>Using the Text Area tag</h3>
<form method="post" action="0305_Using_TextArea.php">
<p>
First Name:<br  />
<input type="text" name="firstname" size="35"
</p>
<p>
Comments:<br  />
<textarea name="comments" rows="8" cols="65">
</textarea>
</p>
<p>
<input type="submit" Value="Submit Information">
</p>
</form>
</body>
</html>

The second part is.

HTML:
<html>
<head>
<title> 305 Using the TextArea tag</title>
<link rel="stylesheet" type="text/css" href="css/basic_2.css" />
</head>
<?php
$firstname = S_POST['firstname']
$comments = $_POST['comments']
print "<p>You are <span class='textblue'> "$firstname" . </span> and";
print "your comments about the College are </p>"
print "<p> <span class='textblue'>. "$comments"</span></p>";
?>
</html>

The first menu works fine, but the second one gives me this...

bandicam 2016-07-20 14-02-43-626.jpg
 
You are missing . to combine the strings.

Eg $string3 = $string1." ".$string2
 
PHP:
print "<p>You are <span class='textblue'>".$firstname."</span> and";
print "your comments about the College are </p>";
print "<p><span class='textblue'>".$comments."</span></p>";
 
PHP:
print "<p>You are <span class='textblue'>".$firstname."</span> and";
print "your comments about the College are </p>";
print "<p><span class='textblue'>".$comments."</span></p>";

You can also do:
PHP:
<p>You are <span class='textblue'><?php echo $firstname;?></span> 
and your comments about the College are </p>
<p><span class='textblue'><?php echo $comments;?></span></p>
 
You can also do:
PHP:
<p>You are <span class='textblue'><?php echo $firstname;?></span> 
and your comments about the College are </p>
<p><span class='textblue'><?php echo $comments;?></span></p>

I do the
PHP:
<?php echo $var; ?>
Otherwise it ends up like a site I saw that is basically just:

PHP:
print <html stuff>
print <html stuff>
print <html stuff>
print <html stuff>
print <html stuff>
print <html stuff>
print <html stuff>
print <html stuff>
print <html stuff>
print <html stuff>

500 lines later

print <html stuff>
print <html stuff>
print <html stuff>
print <html stuff>

Hurts my brain.
 
if you are using php 5.4+ you can use

PHP:
<?= $var ?>

without enabling short tags
 
<?php
$firstname = S_POST['firstname'];
$comments = $_POST['comments'];
print "<p>You are <span class='textblue'> ".$firstname." . </span> and";
print "your comments about the College are </p>";
print "<p> <span class='textblue'>. ".$comments."</span></p>";
?>
 
Or

print "<p>You are <span class='textblue'> " . $firstname . " . </span> and";

To

print "<p>You are <span class='textblue'> $firstname . </span> and";

Variables inside a double quoted string are replaced with the values.
 
Or

print "<p>You are <span class='textblue'> " . $firstname . " . </span> and";

To

print "<p>You are <span class='textblue'> $firstname . </span> and";

Variables inside a double quoted string are replaced with the values.

I use echo mosty, apparently there's a small difference. Only realized that now..
Also I tend to use single quotes and break up the line.

Essentially does the same, but just reads differently.
Code:
echo '<p>You are <span class="textblue">' . $firstname .  '</span> and';
 
Just to avoid confusion, the last posts are correct. Gregorgy pointed out the real problem of the line endings missing. And Biometrics is correct: "text $variable more text". Only in single quotes do you need to do: 'text ' . $variable . ' more text'. But you can do the same with double quotes, no harm done.

Edit: another error: $firstname = S_POST['firstname'] should be $firstname = $_POST['firstname']

Tip. Get yourself an IDE like Netbeans or Eclipse etc. , it will highlight errors.
 
Last edited:
I know it's a bit off topic here and your question has been resolved but I thought it might be worth mentioning now while you're learning.

Clean your inputs

Code:
$firstname = striptags(stripslashes($_POST['firstname']));

or put it into a function:

Code:
function clean($var){
   return striptags(stripslashes($var));
}

$firstname = clean($_POST['firstname']);
$comments = clean($_POST['comments']);

There are other ways and more advanced things/ways you can do it but it's best to get used to cleaning everything from the start.
 
Last edited:
Why doesn't anyone use a framework? Functions already written for you to do all of the basic stuff
 
Could be something small that doesn't need a framework?
 
Why doesn't anyone use a framework? Functions already written for you to do all of the basic stuff

When last did you fire up laravel for a a little brochure site.

I use a framework only if it's a scalable project

Otherwise I make my thing as streamlined as possible no need to have 500 functions if I need two.
 
When last did you fire up laravel for a a little brochure site.

I use a framework only if it's a scalable project

Otherwise I make my thing as streamlined as possible no need to have 500 functions if I need two.
CodeIgniter ftw!
 
Top
Sign up to the MyBroadband newsletter
X