Why is my PHP code wrong?

CorrieDeBeer

Expert Member
Joined
Apr 14, 2015
Messages
1,569
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
 

creeper

Executive Member
Joined
Nov 18, 2010
Messages
5,463
You are missing . to combine the strings.

Eg $string3 = $string1." ".$string2
 

CrazyBob

Senior Member
Joined
Jun 29, 2005
Messages
541
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>";
 

rward

Senior Member
Joined
Oct 26, 2007
Messages
865
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>
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
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.
 

_kabal_

Executive Member
Joined
Oct 24, 2005
Messages
5,923
if you are using php 5.4+ you can use

PHP:
<?= $var ?>

without enabling short tags
 

GreGorGy

BULLSFAN
Joined
Jan 18, 2005
Messages
15,289
<?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>";
?>
 

biometrics

Honorary Master
Joined
Aug 7, 2003
Messages
71,858
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.
 

HideInLight

Expert Member
Joined
Oct 31, 2006
Messages
4,350
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';
 

Max Headroom

Active Member
Joined
Nov 16, 2010
Messages
63
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:

GreGorGy

BULLSFAN
Joined
Jan 18, 2005
Messages
15,289
Edit: another error: $firstname = S_POST['firstname'] should be $firstname = $_POST['firstname']

LOL - no matter how many times I looked at that, I couldn't see a problem until...

Tip. Get yourself an IDE like Netbeans or Eclipse etc. , it will highlight errors.

...that showed the $_ vs S_

Nice catch Max
 

flippakitten

Expert Member
Joined
Aug 5, 2015
Messages
2,486
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:

creeper

Executive Member
Joined
Nov 18, 2010
Messages
5,463
Why doesn't anyone use a framework? Functions already written for you to do all of the basic stuff
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
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.
 

biometrics

Honorary Master
Joined
Aug 7, 2003
Messages
71,858
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