Having a little trouble with PHP.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
1337soldier
Forum Newbie
Posts: 3
Joined: Sun Jan 11, 2009 8:31 pm

Having a little trouble with PHP.

Post by 1337soldier »

Hello everyone,
I'm trying to learn PHP and I'm hoping I've found a good forum for doing so. I'm currently reading a book called PHP/MySQL Programming for the Abolute Beginner, which is probably by far the best book I've picked up yet for my method of learning.

I was trying to tinker with some simple form values and the "post" function to change the background color of the resulting php page, but I was having issues with it not showing my choice. So I did a copy/paste directly from the ebook and even it doesn't work for some reason. I'm using XAMPP, which installs Apache, PHP5, and MySQL for my test environment on my PC, which according to the PHP info, everything is working fine. I can run a blog, forum, etc. on it just fine, but this simple project isn't working. So I decided to reach out and ask someone to check the code for me and see if there's something wrong with it and if that's what's causing the problem and not my server setup.

Here's the code for the html document, which I just name it whatever:

Code: Select all

<html>
<head>
<title>Font Choices</title>
</head>
<body>
<center>
<h1>Font Choices</h1>
<h3>Demonstrates how to read HTML form elements</h3>
 
<form method = "post"
      action = "borderMaker.php">
 
 
<h3>Text to modify</h3>
<textarea name = "basicText"
          rows = "10"
          cols = "40">
Four score and seven years ago our fathers brought forth on this
continent a new nation, conceived in liberty and dedicated to the
proposition that all men are created equal. Now we are engaged in a
great civil war, testing whether that nation or any nation so
conceived and so dedicated can long endure.
</textarea>
 
<table border = 2>
<tr>
  <td><h3>Border style</h3></td>
  <td colspan = 2><h3>Border Size</h3></td>
</tr>
 
<tr>
<td>
<select name = borderStyle>
  <option value = "ridge">ridge</option>
  <option value = "groove">groove</option>
  <option value = "double">double</option>
  <option value = "inset">inset</option>
  <option value = "outset">outset</option>
</select>
</td>
<td>
 
<select size = 5
        name = borderSize>
  <option value = "1">1</option>
  <option value = "2">2</option>
  <option value = "3">3</option>
  <option value = "5">5</option>
  <option value = "10">10</option>
</select>
</td>
 
<td>
<input type = "radio"
       name = "sizeType"
       value = "px">pixels<br>
<input type = "radio"
       name = "sizeType"
       value = "pt">points<br>
<input type = "radio"
       name = "sizeType"
       value = "cm">centimeters<br>
<input type = "radio"
       name = "sizeType"
       value = "in">inches<br>
</td>
</tr>
</table>
 
<input type = "submit"
       value = "show me">
 
</form>
 
</center>
</body>
</html>
 
Here's the code for the resulting PHP document:

Code: Select all

<html>
<head>
<title>Your Output</title>
</head>
<body>
<h1>Your Output</h1>
<center>
<?
$theStyle = <<<HERE
"border-width:$borderSize$sizeType;
border-style:$borderStyle;
border-color:green"
HERE;
 
print "<div style = $theStyle>";
print $basicText;
print "</span>";
 
?>
</center>
 
</body>
</html>
 
 
Thank you in advance.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Having a little trouble with PHP.

Post by Syntac »

Use $_POST["borderStyle"], not $borderStyle — you probably have register_globals turned off. Leave it off.

Also: You're closing the <div> tag with </span>, and all HTML attribute values should be quoted (otherwise any whitespace will break them).
1337soldier
Forum Newbie
Posts: 3
Joined: Sun Jan 11, 2009 8:31 pm

Re: Having a little trouble with PHP.

Post by 1337soldier »

Wow,
Thank you for the fast response!

I'm not quite sure what you mean by using $_POST["borderStyle"] instead, as I don't know how to correctly type it. And if that code is not grammatically correct, can you recommend a good beginners book that's similarly structured?

Here's a link to the book I was using if you want to take a glance at it to see. I'm on chapter 2.
http://www.myownserver.info/PHP-MySQL%2 ... ginner.chm

I've got a few ebooks a friend emailed me and that one had a good learning structure to it in my opinion, but if it's not correct, it will only give me headaches instead of help.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Having a little trouble with PHP.

Post by califdon »

In addition to what syntac said, don't form the bad habit of using the "short tag" for php. Always use <?php never just <?.

If you don't recognize the $_POST[] syntax, read about it in your book. It's the most basic thing about processing forms. If your form says the method='post', that means that the form inputs will be sent to the action script as en environment array named $_POST and you have to assign the values to variables like $borderStyle before you can use them as you did.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Having a little trouble with PHP.

Post by pickle »

We have a forum for PHP Code.

Moving there.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Having a little trouble with PHP.

Post by jaoudestudios »

Many books use the shorten php tags - dont why! lazy?

I was reading the cakephp book (2 different ones) before Christmas and they did the same.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Having a little trouble with PHP.

Post by califdon »

jaoudestudios wrote:Many books use the shorten php tags - dont why! lazy?

I was reading the cakephp book (2 different ones) before Christmas and they did the same.
My take, after consulting the manual, is that in php.ini configuration file, you can set short_open_tag to recognize the short tag or not, also the <?= instead of <? echo. So I guess some authors just do it, as you said, because they're lazy. To me, it's a terrible practice, since it depends on the PHP installation being configured in a specific way. Sure causes a lot of work if you try to run your script on a different installation or when it your site gets updated and no longer supports it.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Having a little trouble with PHP.

Post by requinix »

One of the bothersome things about short opening tags is an <?xml declaration (for those of us who us it). More common in XML output, of course.

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Naturally PHP complains about the bad syntax, so you're stuck with echo/print-ing it.

I thought I remembered something about short_open_tag and PHP 6 but I can't find anything about it.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Having a little trouble with PHP.

Post by jaoudestudios »

I know php6 is going to be more strict. Going to have namespaces too :)
Post Reply