Jump to content
Larry Ullman's Book Forums

Chapter 10 Pursue Question #2 & # 3


Recommended Posts

Ok well you want the outputted code to look like this


<p><label>First Name: <input type="text" name="first_name" size="20"  /></label></p>

 

So call the function you have made and pass it 3 arguments.

 

The function is defined as this:

make_text_input($name, $label, $password)

 

In your previous code you have done this:

 

make_text_input('first_name', 'First Name', NULL);

 

Which would generate this otuput

 

<p><label>First Name: <input type="" name="first_name" size="20"  /></label></p>

 

Notice that the label is specified in the output, because you passed it as your 2nd argument to the function as 'First Name'.

 

Also notice that the name is specified as "first_name" because you specified it as your first argument in the function.

 

Then notice that the type="" ie there is no type specified, because you passed a value of NULL as the 3rd argument.

 

If you can't do it from this i'll gladly tell you how to do the first input field bit :)

Edited by Jonathon
Link to comment
Share on other sites

The source code you are aiming to achieve is this:


<p><label>First Name: <input type="text" name="first_name" size="20"  /></label></p>
<p><label>Last Name: <input type="text" name="last_name" size="20"  /></label></p>
<p><label>Email Address: <input type="text" name="email" size="20"  /></label></p>

<p><label>Enter your password: <input type="password" name="password" size="20"  /></label></p>
<input type="submit" name="submit" value="Register!" />

Link to comment
Share on other sites

AND I tried this:

<?php // Script 10.2 - sticky1.php

/* This script defines and calls a function that creates a sticky text input. */

 

// This function makes a sticky text input.

// This function requires three arguments be passed to it.

function make_text_input($name, $label, $password) {

 

// Begin a paragraph and a label:

print '<p><label>' . $label . ': ';

 

 

 

 

// Add the value:

if (isset($_POST[$name])) {

print ' value="' . htmlspecialchars($_POST[$name]) . '"';

} else {

if (isset($_POST[$password])) {

print ' value="' . htmlspecialchars($_POST[$password]) . '"';

 

 

}

}

 

 

 

// Complete the input, the label and the paragraph:

print ' /></label></p>';

 

} // End of make_text_input() function.

 

// Make the form:

print '<form action="" method="post">';

 

// Create some text inputs:

make_text_input('first_name', 'First Name', '<input type="text" name="fname" size="20"/>');

make_text_input('last_name', 'Last Name', '<input type="text" name="lname" size="20"/>');

make_text_input('email', 'Email Address','<input type="text" name="email" size="20"/>');

make_text_input ('password', 'Enter your password', '<input type="password" name="password" size="20"/>');

 

 

print '<input type="submit" name="submit" value="Register!" /></form>';

 

?>

Link to comment
Share on other sites

Nope the function that I gave you is absolutely fine in achieving the output that is required. All you need to do is put the right values for the 3rd argument of the function.

 

 

The 3rd argument is effectively stating is this a 'text' input field or a 'password' input field

Link to comment
Share on other sites

Yes I do. That code doesn't work though (I don't think). You're thinking too big. The only difference in the password input field compared to the standard 'text' input is that it's type is called 'password'.

 

Use the function code I gave you and call the first function like this

 

make_text_input('first_name', 'First Name', 'text');

Link to comment
Share on other sites

Jonathon!!! Is this it!!!???

<?php // Script 10.2 - sticky1.php

/* This script defines and calls a function that creates a sticky text input. */

 

// This function makes a sticky text input.

// This function requires three arguments be passed to it.

function make_text_input($name, $label, $password) {

 

// Begin a paragraph and a label:

print '<p><label>' . $label . ': ';

 

 

print '<input type="' . $password . '" name="' . $name . '" size="20" ';

 

 

 

// Add the value:

if (isset($_POST[$name])) {

print ' value="' . htmlspecialchars($_POST[$name]) . '"';

} else {

if (isset($_POST[$password])) {

print ' value="' . htmlspecialchars($_POST[$password]) . '"';

 

 

}

}

 

 

 

// Complete the input, the label and the paragraph:

print ' /></label></p>';

 

} // End of make_text_input() function.

 

// Make the form:

print '<form action="" method="post">';

 

// Create some text inputs:

make_text_input('first_name', 'First Name', 'text');

make_text_input('last_name', 'Last Name', 'text');

make_text_input('email', 'Email Address','text');

make_text_input ('password', 'Enter your password', 'password');

 

 

print '<input type="submit" name="submit" value="Register!" /></form>';

 

?>

  • Upvote 1
Link to comment
Share on other sites

Are you ready to help me with my next dilemma? I am to rewrite teh make_text_input() function so that it can be told whehte rto look for an existing value in either $_POST or $_GET. I think I've done this right... Have I?

 

This is my code...

<?php // pursue # 2

/* This script defines and calls a function that creates a sticky text input and can be told whether to look for an existing value in either _$POST or _$GET */

 

// This function makes a sticky text input.

// This function requires three arguments be passed to it.

function make_text_input($name, $label) {

 

// Begin a paragraph and a label:

print '<p><label>' . $label . ': ';

 

// Begin the input:

print '<input type="text" name="' . $name . '" size="20" ';

 

// Add the value:

if (isset($_POST[$name])) {

print ' value="' . htmlspecialchars($_POST[$name]) . '"';

} else {

if (isset($_GET[$name])) {

print ' value="' . htmlspecialchars($_GET[$name]) . '"';

}

}

 

 

 

// Complete the input, the label and the paragraph:

print ' /></label></p>';

 

} // End of make_text_input() function.

 

// Make the form:

print '<form action="" method="post">';

 

// Create some text inputs:

make_text_input('first_name', 'First Name');

make_text_input('last_name', 'Last Name');

make_text_input('email', 'Email Address');

 

 

print '<input type="submit" name="submit" value="Register!" /></form>';

 

?>

Link to comment
Share on other sites

I am not sure...The instructions say Rewrite the make_text_input() function so that it can be told whether to look for an existing value in either $_POST or $_GET. This one seemed to come to easy for me so I was thinking it wasn't right....

 

By the way - I really, really appreciate your fast prompt help - you should be a teacher :) This stuff seems to come so easy for you! I am struggling my way through this book - and I am feeling like I need to be babysat step by step.... I can read it, but I can't write it!

Link to comment
Share on other sites

Well I think the form was sticky when I tried it. So I would say that would be a success, you could use $_REQUEST and just one if statement. But your method doesn't appear to be wrong. I am not at my PC so can't check. I am lucky that I bought a PHP book by Larry that is all really, if I had perhaps gone another route I might not have stuck with it. Who knows. Starting out you shouldn't expect to get everything, the real learning begins where you want to modify or expand on the books material I find. Then when you write it and rewrite it yourself it will all come together. I'm glad Larry actually has put these pursue parts in because they compound the chapters well.

 

So in short, persevere, we've all got to start somewhere.

Link to comment
Share on other sites

Would this be correct for the pursue question #3

 

<body>

<?php // Script 10.2 - sticky1.php

/* This script defines and calls a function that creates a sticky text input. */

// This function makes a sticky text input.

// This function requires two arguments be passed to it.

function make_text_input($name, $label, $type, $size = 20) {

 

// Begin a paragraph and a label:

print '<p><label>' . $label . ': ';

 

print '<input type="'. $type . '" name="' . $name . '" size="' . $size . '"';

 

// Add the value:

if (isset($_POST[$name])) {

print ' value="' . htmlspecialchars($_POST[$name]) . '"';

}

 

// Complete the input, the label and the paragraph:

print ' /></label></p>';

 

} // End of make_text_input() function.

// Make the form:

print '<form action="" method="post">';

// Create some text inputs:

make_text_input('first_name', 'First Name', 'text', '');

make_text_input('last_name', 'Last Name', 'text', '50');

make_text_input('email', 'Email Address', 'text', '50');

make_text_input('password', 'Password', 'password', '');

print '<input type="submit" name="submit" value="Register!" /></form>';

?>

</body>

Link to comment
Share on other sites

  • 1 month later...

Wow, just got through reading all of the back and forth here. Now my brain hurts :) I got the same answer as phpRob for the pursue #3. However, it looks to me that April, you did not correctly answer the Pursue #2 question. (also phpRob, your above script doesn't answer Pursue #2 question, but perhaps you were just intending to answer #3 and you left the code out for the #2 answer for simplicity, not sure). Anyway the code that I came up with that answers Pursue #2 and #3 is this:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Sticky Text Inputs</title>
</head>
<body>
<?php  //script 10.2  - sticky1.php	- sticky2.php
//this script defines and calls a function that creates a sticky text input.
//this function makes a sticky text input
//this function requires two arguments be passed to it.
function make_text_input ($name, $label, $text_or_pass = 'text', $size = 20) {

//begin a paragraph and a label:
print '<p><label>' . $label . ': ';

//Begin the input
print '<input type="' . $text_or_pass . '" name="' . $name . '" size="' . $size . '" ';

//add the value
if (isset($_POST[$name])) {
 print ' value="' . htmlspecialchars($_POST[$name]) . '"';
} elseif (isset($_GET[$name])) {
 print ' value="' . htmlspecialchars($_GET[$name]) . '"';
}

//complete the input, the label and the paragraph:
print ' /></label></p>';

} //end of make_text_input() function.
//make the form
print '<form action="" method="POST">';
//create some text inputs
make_text_input('first_name', 'First Name');
make_text_input('last_name', 'Last Name', '', 30);
make_text_input('email', 'Email Address', '', 50);
make_text_input('password', 'Password', 'password');
print '<input type="submit" name="submit" value="Register!" /></form>';


?>
</body>
</html>

 

April, you need to have this code:

 

elseif (isset($_GET[$name])) {

print ' value="' . htmlspecialchars($_GET[$name]) . '"';

}

 

 

to check for $_GET values. Otherwise if the form method = GET your form won't be sticky.

 

Did anyone else get a similar answer?

 

Tim

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...