Jump to content
Larry Ullman's Book Forums

Code That Does Not Work!


Recommended Posts

I like Larry Ullman's book "PHP for the Web" fourth edition. However, there is some code that says in the book it will create a sticky form. However, the code does not do that. Below is the code. People that are more knowledgeable than me for PHP please examine this. This code is from pages 210 through 216. I left out the following line of code because I do not want to take the time to write an additional file. The line of code that I left out from page 212 is the following:

include('templates/header.html');

I also left out the include footer.html function/file, because I do not have the time to write the footer.html file.

 

I think the sticky form should work without the header and footer and in viewing this code in the Internet Explorer browser there were no error messages. From Larry's book on page 210, it says "A sticky form remembers values entered into it. A common example is a search engine, which often displays your terms in the search box, even when showing the results of the search."

 

Here is the code:

 

/*define('TITLE, 'Register');*/ // Line of code also not used because it causes a parameter error.It is likely from the include file.

print '<style type=text/css" media="screen">

.error { color: red;}

</style>';

 

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$problem = FALSE;

 

if (empty($_POST['first_name'])) {

$problem = TRUE;

print '<p class="error">Please enter your first name</p>';

}

 

if (empty($_POST['last_name'])) {

$problem = TRUE;

print '<p class="error">Please enter your last name</p>';

}

 

if (empty($_POST['email'])) {

$problem = TRUE;

print '<p class="error">Please enter your email address.</p>';

}

 

if (empty($_POST['password1'])) {

$problem = TRUE;

print '<p class="error">Please enter a password.</p>';

}

 

if ($_POST['password1'] != $_POST['password2']) {

$problem = TRUE;

print '<p class="error">Your password did not match your confirmed password.</p>';

}

 

if (!$problem) {

print '<p>You will soon be registered.</p>';

$_POST = array();

}

else {

print '<p class="error">Please try again.</p>';

}

}

?>

 

<form action="register.php" method="post">

 

<p>First Name: <input type="text" name="first_name" size="20" value="<?php if (isset($POST['first_name'])) {print htmlspecialchars($_POST['first_name']); } ?>" /> </p>

 

<p>Last Name: <input type="text" name="last_name" size="20" value="<?php if (isset($POST['last_name'])) {print htmlspecialchars($_POST['last_name']); } ?>" /> </p>

 

<p>Email address: <input type="text" name="email" size="20" value="<?php if (isset($POST['email'])) {print htmlspecialchars($_POST['email']); } ?>" /> </p>

 

<p>Password: <input type="password" name="password1" size="20" value="<?php if (isset($POST['password1'])) {print htmlspecialchars($_POST['password1']); } ?>" /> </p>

 

<p>Confirm Password: <input type="password" name="password2" size="20" value="<?php if (isset($POST['password2'])) {print htmlspecialchars($_POST['password2']); } ?>" /> </p>

 

<p> <input type="submit" name="submit" value="Register" /></p>

</form>

 

<?php /*include('templates/footer.html');*/ ?>

<!-- The footer.html file also not included.-->

Link to comment
Share on other sites

I have looked at this very quickly so I appologise if it's not the full solution. But here

 

<p>First Name: <input type="text" name="first_name" size="20" value="<?php if (isset($POST['first_name'])) {print htmlspecialchars($_POST['first_name']); } ?>" /> </p>
<p>Last Name: <input type="text" name="last_name" size="20" value="<?php if (isset($POST['last_name'])) {print htmlspecialchars($_POST['last_name']); } ?>" /> </p>
<p>Email address: <input type="text" name="email" size="20" value="<?php if (isset($POST['email'])) {print htmlspecialchars($_POST['email']); } ?>" /> </p>
<p>Password: <input type="password" name="password1" size="20" value="<?php if (isset($POST['password1'])) {print htmlspecialchars($_POST['password1']); } ?>" /> </p>
<p>Confirm Password: <input type="password" name="password2" size="20" value="<?php if (isset($POST['password2'])) {print htmlspecialchars($_POST['password2']); } ?>" /> </p>

 

value needs to be $_POST not $POST

 

:)

  • Upvote 2
Link to comment
Share on other sites

Thank you for catching my typing error, which I copied and pasted. Good job Jonathon. Now the code WORKS! I am finishing a PHP class at City College of San Francisco. The class has been challenging. That is the thing about programming languages like PHP. All your 't's must be crossed, your 'I's must be dotted and your commas and periods in the right place or the code will not work. Thanks again. It is good to work with people more knowledgeable than I am.

 

And sticky forms are important. Feel free to copy the above code, correct my error, make some content changes and additions and then reuse it on your web site.

Link to comment
Share on other sites

I have looked at this very quickly so I appologise if it's not the full solution. But here

 

<p>First Name: <input type="text" name="first_name" size="20" value="<?php if (isset($POST['first_name'])) {print htmlspecialchars($_POST['first_name']); } ?>" /> </p>
<p>Last Name: <input type="text" name="last_name" size="20" value="<?php if (isset($POST['last_name'])) {print htmlspecialchars($_POST['last_name']); } ?>" /> </p>
<p>Email address: <input type="text" name="email" size="20" value="<?php if (isset($POST['email'])) {print htmlspecialchars($_POST['email']); } ?>" /> </p>
<p>Password: <input type="password" name="password1" size="20" value="<?php if (isset($POST['password1'])) {print htmlspecialchars($_POST['password1']); } ?>" /> </p>
<p>Confirm Password: <input type="password" name="password2" size="20" value="<?php if (isset($POST['password2'])) {print htmlspecialchars($_POST['password2']); } ?>" /> </p>

 

value needs to be $_POST not $POST

 

 

Cool to see you back answering posts and helping people once again. :)
Link to comment
Share on other sites

Using the same basic code, the sticky form is not working for textarea. It works for input text, but not textarea. I don't know why PHP does not work. It has potential, but many minute problems. Below is the code with problem code in red. Thank you for your help.

 

<?php

print '<style type="text/css" media="screen">

.error { color: red;}

</style>';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$problem = FALSE;

 

if (empty($_POST['sender']) && ((str_word_count($_POST['sender']) != 2) || (str_word_count($_POST['sender']) != 3))) {

$problem = TRUE;

print '<p class="error">Please enter your first and last name.</p>';

}

 

if (empty($_POST['sender_email']) || (substr_count($_POST['sender_email'], '@', 0) != 1)) {

$problem = TRUE;

print '<p class="error">Please enter your email address.</p>';

}

 

if (empty($_POST['recipient']) && ((str_word_count($_POST['recipient']) != 2) || (str_word_count($_POST['recipient']) != 3))) {

$problem = TRUE;

print '<p class="error">Please enter the recipient\'s first and last name.</p>';

}

 

if (empty($_POST['recipient_email']) || (substr_count($_POST['recipient_email'], '@', 0) != 1)) {

$problem = TRUE;

print '<p class="error">Please enter the recipient\'s e-mail address.</p>';

}

 

if ( empty( $_POST['comments'] ) || ( strlen( $_POST['comments']) > 120 ) ) {

$problem = TRUE;

print '<p class="error">Please send the recipient a message that is 120 characters or less.</p>';

}

 

 

if (!$problem) {

print '<p>Need to go to another page to send an email with the following.</p>';

$sender = $_REQUEST['sender'];

print $sender . ' has sent you an ecard with the following message:' . '<br /><br />';

$comments = $_REQUEST['comments'];

print $comments;

}

else {

print '<p class="error">Please try again.</p>';

}

}

?>

 

<form action="form_improve.php" method="post">

<p>Sender:              

<input type="text" name="sender" size="25" value="<?php if (isset($_POST['sender'])) {print

htmlspecialchars($_POST['sender']); } ?>" /> </p>

 

<p>Sender e-mail:    

<input type="text" name="sender_email" size="35" value="<?php if (isset($_POST['sender_email'])) {print

htmlspecialchars($_POST['sender_email']); } ?>" /> </p>

 

<p>Recipient:           

<input type="text" name="recipient" size="25" value="<?php if (isset($_POST['recipient'])) {print

htmlspecialchars($_POST['recipient']); } ?>" /> </p>

 

<p>Recipient e-mail: 

<input type="text" name="recipient_email" size="35" value="<?php if (isset($_POST['recipient_email'])) {print htmlspecialchars($_POST['recipient_email']); } ?>" /> </p>

 

Message:<br />

<textarea name="comments" rows="7" cols="45" value="<?php if (isset($_POST['comments'])) {print htmlspecialchars($_POST['comments']); } ?>" >

</textarea>

 

<p>

<input type="submit" name="preview_button" value="Preview your ecard" style="background-color:#FFCC00;border-color:#3333FF" />

</p>

</form>

Link to comment
Share on other sites

<textarea> differs from <input> fields in that it does not have a value attribute. To make your textarea sticky, try

<textarea name="comments" rows="7" cols="45"><?php if (isset($_POST['comments'])) {print htmlspecialchars($_POST['comments']); } ?></textarea>

  • Upvote 1
Link to comment
Share on other sites

Hi Margaux:

 

Thank you. The code you stated works well. I just tried it out. Good to know that html textarea does not accept values. I did not know that. Do you know code that limits the amount of characters inside the textarea that is part of the form element?

 

Thanks again.

Link to comment
Share on other sites

In researching the above, it looks like the HTML textarea element does not allow you to limit the number of characters for a textarea. In contrast the text input does allow HTML code that limits the number of characters. Maybe HTML5 will add more coding attributes, but I have not studied HTML5.

 

However, with the above PHP code I did add to an if/else statement and limited the number of characters to 120. Maybe that is the best that I can do now.

 

Thank you for your help.

Link to comment
Share on other sites

Traditional html does not support a maxlength attribute the way other input fields do, however HTML5 does

<textarea maxlength="120">

You can use this in browsers which support HTML5 which of course excludes IE and I think Opera. For these browsers you will need a javascript solution. If you search online, I'm sure you'll find one. Initially though you probably want to alert your users - so perhaps you should set your textarea to be smaller and display an initial message that a minimum of 120 characters is allowed.

  • Upvote 1
Link to comment
Share on other sites

Thank you Margaux. Good basic fundamental information from you and I appreciate it. I don't have time to study JavaScript now. I will just add maxlength into the textarea and specify HTML5 code. And if it reads it great. If it doesn't, I am not going to be concerned. I do have another problem now though. I am thinking I may need to add some string code for the new $comment variable because coming from the textarea form it is not reading the CSS that is part of this file. Below is the revised code. Any help is greatly appreciated.

 

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset=utf-8>

<title>Mark Spaghetti PHP</title>

<style type="text/css">

 

#ecardform {

float:left;

width:420px;

margin: 1.2em 0em 1.2em 1.2em;

font-size: 1.2em;

}

 

#message {

float:left;

width:420px;

margin: 1.2em 0em 1.2em 1.2em;

font-size: 1.2em;

}

 

#comments {

float:left;

width:400px;

margin: 1.2em 0em 1.2em 1.2em;

font-size: 1.2em;

}

 

body {

font-size: 1.2em;

}

 

</style>

</head>

 

<body>

 

 

<?php

 

print '<style type="text/css" media="screen">

.error { color: red;}

</style>';

 

 

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$problem = FALSE;

 

if (empty($_POST['sender']) && ((str_word_count($_POST['sender']) != 2) || (str_word_count ($_POST['sender']) != 3))) {

$problem = TRUE;

print '<p class="error">Please enter your first and last name.</p>';

}

 

if (empty($_POST['sender_email']) || (substr_count($_POST['sender_email'], '@', 0) != 1)) {

$problem = TRUE;

print '<p class="error">Please enter your email address.</p>';

}

 

if (empty($_POST['recipient']) && ((str_word_count($_POST['recipient']) != 2) || (str_word_count($_POST['recipient']) != 3))) {

$problem = TRUE;

print '<p class="error">Please enter the recipient\'s first and last name.</p>';

}

 

if (empty($_POST['recipient_email']) || (substr_count($_POST['recipient_email'], '@', 0) != 1)) {

$problem = TRUE;

print '<p class="error">Please enter the recipient\'s e-mail address.</p>';

}

 

if ( empty( $_POST['comments'] ) || ( strlen( $_POST['comments']) > 328 ) ) {

$problem = TRUE;

print '<p class="error">Please send the recipient a message that is 328 characters or less.</p>';

}

 

 

if (!$problem) {

$comments = $_REQUEST['comments'];

$sender = $_REQUEST['sender'];

print '<div id="message">' . '<p>Need to go to another page to send an email with the following.</p>' . $sender . ' has sent you an ecard with the following message:' . '<br /><br />' . $comments . '</div>';

//$_POST = array();

}

else {

print '<p class="error">Please try again.</p>';

}

}

?>

<div id="ecardform">

<form action="form_improve.php" method="post">

 

<p>Sender:                

<input type="text" name="sender" size="25" maxlength="30" value="<?php if (isset($_POST['sender'])) {print

htmlspecialchars($_POST['sender']); } ?>" /> </p>

 

<p>Sender e-mail:     

<input type="text" name="sender_email" size="35" maxlength="40" value="<?php if (isset($_POST['sender_email'])) {print

htmlspecialchars($_POST['sender_email']); } ?>" /> </p>

 

<p>Recipient:            

<input type="text" name="recipient" size="30" maxlength="25" value="<?php if (isset($_POST['recipient'])) {print

htmlspecialchars($_POST['recipient']); } ?>" /> </p>

 

<p>Recipient e-mail: 

<input type="text" name="recipient_email" size="35" maxlength="40" value="<?php if (isset($_POST['recipient_email'])) {print htmlspecialchars($_POST['recipient_email']); } ?>" /> </p>

 

Message:<br />

<textarea name="comments" rows="7" cols="41" maxlength="328"><?php if (isset($_POST['comments'])) {print htmlspecialchars($_POST['comments']); } ?><textarea>

 

<p>

<input type="submit" name="preview_button" value="Preview your ecard" style="background-color:#FFCC00;border-color:#3333FF" />

</p>

 

</form>

</div>

 

</body>

</html>

Link to comment
Share on other sites

I like this web site, but suddenly the text editor got turned off on my computer for this web site. I used to be able to click, for example, the color button and change the color of some text on one of my posts. It got turned off. I don't know why. I noticed this right after I clicked the Tab button. It should not have been turned off.

Link to comment
Share on other sites

Testing the code again. Maybe the code is O.K. afterall. When I tested it in the textarea, I just did not put any spaces in between letters so it ran the whole thing across the page. When I write regular words, it seems to work. First time I have been wrong for being right. Ha ha ha ha.

Link to comment
Share on other sites

Not sure if you still have a question? However if you do, please will you put your code between code tags

[code]

[/code]

 

Also is your college course really teaching you to write code this way? why have you put css inside php tags, why don't you just put it in the head section with the rest of the styling? Better yet, put it in an external stylesheet and provide a link to it in the head section.

<link rel="stylesheet" href="mystyles.css">

Link to comment
Share on other sites

Hi Margaux:

 

I agree with you 100% on the CSS. Combining different language code together in one line does make it more difficult. Part of the problem is our assignments have been very difficult for first semester PHP students. Also it is difficult to use an external CSS stylesheet with their server. It is difficult to figure out what folder to place the css file in and then when I use a regular path to it, it still does not read the CSS. Usually, the CSS has gone in the head section for my file work, but not always.

Link to comment
Share on other sites

I have another fundamental difficulty with PHP and I have not gotten any good instruction on this. I simply want to place a link on a page and when someone on the web page clicks on the link have it go to a certain file named web page. Are there any simple instructions for this? With html it is simple, but not with PHP, at least that I have seen.

Link to comment
Share on other sites

You haven't seen any good instructions for doing that in PHP because it isn't something you do in PHP. You can only do this in HTML. However, you can have PHP print the HTML, just as you can have PHP print anything:

echo '<a href="somepage.php">link</a>';

Link to comment
Share on other sites

That is a good point that I had not considered. Of course, you can have php variables as part of html links and this is what I am trying to learn. But I can re-evaluate this now with the fact that this is primarily html. Thank you for your comments. Any others will be appreciated. And I like your book Larry. It is good for us non-Geek speaking programming wannabees. I hope all is well in PA.

 

And reading your post again, I like your fundamental explanation. You are actually getting PHP to print the HTML for the link using PHP variables.

Link to comment
Share on other sites

You haven't seen any good instructions for doing that in PHP because it isn't something you do in PHP. You can only do this in HTML. However, you can have PHP print the HTML, just as you can have PHP print anything:

echo '<a href="somepage.php">link</a>';

Larry, I have seen

echo

used in some other tutorials, but it does not come up in your book (as far as I remember). What is the difference between it and

print

? What are the costs and benefits of using each?

Link to comment
Share on other sites

For all intents and purposes, print and echo are virtually the same. I tend to use print in the beginner's book, because it's most obvious. Personally I use echo in my own code, for very trivial reasons.

Link to comment
Share on other sites

  • 5 months later...

Hi:

I'm really glad that htmlspecialchars is mentioned here; isn't it a better(safer) way to code than to just use declaring variables like $title=$_POST['title']); ?

 

here's how I went about adjusting the handle_form.php code for Ch.3 exercises:

 

 

<p>Thank you, <?php echo htmlspecialchars($_POST['title']);echo htmlspecialchars($_POST['name']); ?>, for your comments.</p>

 

<p>You stated that you found this example to be '<?php echo htmlspecialchars($_POST['response']); ?>' and added:<br /><?php echo htmlspecialchars($_POST['comments']); ?>

</p>

 

Is there any other reason why one way would be better than the other?

 

cheers,

 

Vickie

 

<textarea> differs from <input> fields in that it does not have a value attribute. To make your textarea sticky, try

<textarea name="comments" rows="7" cols="45"><?php if (isset($_POST['comments'])) {print htmlspecialchars($_POST['comments']); } ?></textarea>

Link to comment
Share on other sites

First, you should distinguish between what htmlspecialchars() does and what declaring variables is about. Separate concepts. Second, using htmlspecialchars() (or strip_tags() or whatever) is a good security measure. But in this trivial example in which the user's same information is just reprinted back to them, it makes no difference.

 

Also note that your example is going to run the title up against the name as you have no spaces between them.

Link to comment
Share on other sites

 Share

×
×
  • Create New...