Page 1 of 1

Having a little trouble with PHP.

Posted: Sun Jan 11, 2009 8:43 pm
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.

Re: Having a little trouble with PHP.

Posted: Sun Jan 11, 2009 8:47 pm
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).

Re: Having a little trouble with PHP.

Posted: Sun Jan 11, 2009 9:17 pm
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.

Re: Having a little trouble with PHP.

Posted: Mon Jan 12, 2009 1:25 am
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.

Re: Having a little trouble with PHP.

Posted: Mon Jan 12, 2009 11:12 am
by pickle
We have a forum for PHP Code.

Moving there.

Re: Having a little trouble with PHP.

Posted: Mon Jan 12, 2009 11:19 am
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.

Re: Having a little trouble with PHP.

Posted: Mon Jan 12, 2009 1:16 pm
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.

Re: Having a little trouble with PHP.

Posted: Mon Jan 12, 2009 1:56 pm
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.

Re: Having a little trouble with PHP.

Posted: Mon Jan 12, 2009 2:41 pm
by jaoudestudios
I know php6 is going to be more strict. Going to have namespaces too :)