Jump to content
Larry Ullman's Book Forums

Recommended Posts

To complete the requirements for Chapter 10's # 2 pursue question... Do we rewrite the form and submit one that looks like this?

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

 

// Check for values:

if ( is_numeric($_GET['quantity']) AND is_numeric($_GET['price']) ) {

 

// Call the function and print the results:

$total = calculate_total($_GET['quantity'], $_GET['price']);

print "<p>Your total comes to $<span style=\"font-weight: bold;\">$total</span>.</p>";

 

} else { // Inappropriate values entered.

print '<p style="color: red;">Please enter a valid quantity and price!</p>';

}

 

}

And then change the form method to GET, or is the author looking for something else? Maybe I've missed the whole concept here?

 

Also - for the 3rd question in the pursue - I understand that I need to create a new line item after line 38 called make_text_input('password', 'Password'); But I cannot connect the dots as to what goes above that? I am trying to process this through by looking at page 269. I have a feeling I need to add something in the if function. Can you direct me in the right direction.

 

Sorry for the dumb questions, but this is really above my scope of understanding and I'm trying to put it into a language I do understand!

Thanks!

April

Link to post
Share on other sites
To complete the requirements for Chapter 10's # 2 pursue question... Do we rewrite the form and submit one that looks like this?

And then change the form method to GET, or is the author looking for something else? Maybe I've missed the whole concept here?

 

Your confusion probably stems from the fact that you're looking at the wrong example. The recommendation is to rewrite the make_text_input() function, not the calculate_totals() function.

 

Also - for the 3rd question in the pursue - I understand that I need to create a new line item after line 38 called make_text_input('password', 'Password'); But I cannot connect the dots as to what goes above that? I am trying to process this through by looking at page 269. I have a feeling I need to add something in the if function. Can you direct me in the right direction.

 

Yes, that's the premise for how you would invoke it, except you'd add a third argument which would be "password". Then you add a third argument to the function definition, perhaps called "type". Then you use an IF within the function to create a text input or password depending upon the value of type.

 

Sorry for the dumb questions, but this is really above my scope of understanding and I'm trying to put it into a language I do understand!

 

No need to apologize. That's what the forum is for and we all have to learn somehow!

Link to post
Share on other sites

Hi Larry - thank you for your quick response! I see what you are referring to- so if I understand it correctly then, and if I sue page 269 as an example, I should rewrite line 14 to read function make_text_input(_POST, _GET)

 

then line 36 should read make_text_input ('_POST', '_GET');

 

I know that's not right and that won't work - but I just can't get the dots to connect in my head. I can understand in English that you want the function to ultimately read what ever is in line 23... Why is writing code so difficult to comprehend? I'm finding that I'm having troubles even knowing "how' to ask you my question....

Link to post
Share on other sites

I don't have the book April, but it seems that you have taken type to be the GET and POST. Instead, remember that the function is trying to create different types of inputs for forms. Password is just another type of input as is text or textarea. Hope that helps.You will figure it out I'm sure :-)

Link to post
Share on other sites

Ok, well from what Larry says and you. You are trying to invoke a function called make_text_input(). But currently it doesn't have the ability to make a input type of password. It seems that Larry wants you to rewrite the make_text_input() function to be able to do so.

Link to post
Share on other sites

This might help... here is what I have done.... But to be more specific, have I met the requirements? I am supposed to look for an existing value in either $_POST or $_GET... Have I done that?

<?php // Script 10.3 - 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, $size = 20) {

 

// Begin a paragraph and a label:

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

 

// Begin the input:

print '<input type="text" 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');

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

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

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

 

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

Link to post
Share on other sites

This is getting confusing - there are two different pursue questions I am questioning.... The two questions are getting jumbled. Here is what I have for the Pursue # 3. I am to create a variation of the make_text_input() function that can create a text input or a password input depending upon how the function is called. Is this right? Have I met the requirements?

<?php // Script 10.3 - Pursue # 3.php

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

 

// This function makes a sticky text input.

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

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

 

// Begin a paragraph and a label:

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

 

// Begin the input:

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

 

// Add the value:

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

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

}

 

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

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

}

 

// 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', 'Enter your password', 30);

 

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

 

Obviously this script gives me a whole slew of errors that looks like this:

 

Warning: Missing argument 4 for make_text_input(), called in C:\xampp\htdocs\sticky2.php on line 40 and defined in C:\xampp\htdocs\sticky2.php on line 14

First Name:

Notice: Undefined variable: type in C:\xampp\htdocs\sticky2.php on line 27

/>

 

Warning: Missing argument 4 for make_text_input(), called in C:\xampp\htdocs\sticky2.php on line 41 and defined in C:\xampp\htdocs\sticky2.php on line 14

 

Last Name:

Notice: Undefined variable: type in C:\xampp\htdocs\sticky2.php on line 27

/>

 

Warning: Missing argument 4 for make_text_input(), called in C:\xampp\htdocs\sticky2.php on line 42 and defined in C:\xampp\htdocs\sticky2.php on line 14

 

Email Address:

Notice: Undefined variable: type in C:\xampp\htdocs\sticky2.php on line 27

/>

 

Warning: Missing argument 4 for make_text_input(), called in C:\xampp\htdocs\sticky2.php on line 43 and defined in C:\xampp\htdocs\sticky2.php on line 14

 

Enter your password:

Notice: Undefined variable: type in C:\xampp\htdocs\sticky2.php on line 27

/>

 

Now - the second question says to rewrite the make_text_input() function so that it can be told whether to look for an existing value in either $_POST or $_GET... I have no idea how to do that.... Can you give me a little bit more of a hint on the steps to complete this question?

Link to post
Share on other sites

I can see why that is confusing, but you are close to solving this April. Note that your function takes 4 arguments, So when you invoke this new function like so

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

 

You only specify 2 of the arguments, the $size argument is optional but optional arguments have to be declared last in the function. Then as Larry suggested you need to write an IF clause into the function body to help you generate either a 'text' or 'password' input type.

 

We'll leave $_POST and $_GET till after this parts done.

Link to post
Share on other sites

Okay Jonathon - so I have this now - and no errors! BUT does this meet the requirements? I actually think it might! The instructions say to create a variation on teh make_text_input() function that can create a text input or a password input, depending on upon how the function is called.... Here is my script...

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

 

// 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]) . '"';

}

 

 

 

// 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', NULL);

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

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

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

 

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

Link to post
Share on other sites

You are close, no errors is a thumbs up, the only problem you have here is that your password field will still be a text. If you try it out you will see that it accepts just plain text. So tweak your function in order for it to produce either text inputs or a password input field and it looks ok to me. (But you must remember I don't have the book so I cant explicitly say that).

Link to post
Share on other sites

Thanks Jonathon! But what do you mean "tweak your function" Can you tell me which line item you are referring to? I have looked in the book - and I know you don't have it - but I cannot find anything specific for that... I"m sure it's there, I just don't know what I am looking for. Are you referring to the if statement or the user defined function itself? What should I be looking at doing to those lines?

Link to post
Share on other sites

um - I think I'm getting a little excited here :) Am I onto something with this script?

// 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 . ': ';

 

// 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($_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', NULL);

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

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

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

 

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

 

?>

Link to post
Share on other sites

Well the first way to check it is, does the password field let you type plain text. The answer is probably yes. So in that case look at the source code, what does it return? All inputs are assigned a text value because off this line here:

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

 

So you need to make the above line be able to output either a password or text input. Currently you have hard coded "text" into the print statement so it will always return "text" as the input type. In short, you're not actually using the $password argument inside your function.

  • Upvote 1
Link to post
Share on other sites

Your function is:

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

 

So you need to take the values you provide to that function and use them within the code of the function itself.

Currently this line:

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

Will always print you an input field with type="text" so edit that line so it can print either type="text" or type="password" depending on what $password argument you pass to the function?

 

Follow?

  • Upvote 1
Link to post
Share on other sites

Yes, I follow - but I don't know how to make the 3 fields prior to that take "text inputs" and the password field take just "password" inputs. If I add a second line that looks like this:

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

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

 

I get two input boxes.... That's not what I want, but it does give me the code source that you are referring to for the second box. I need to figure out how to make just the password box be an input type of "password" I don't know what to or what to change to make that happen.

Link to post
Share on other sites

Here is a pointer, All you have to do now is change the arguments you use to invoke the function in order to create the appropriate inputs.

<?php
// 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 . ': ';

// Begin the input:
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:
// Invoke functions here

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

?> 

  • Upvote 1
Link to post
Share on other sites

you probably think I'm a real nut job here... LOL But don't get me wrong - I really appreciate your help!

 

When you say "invoke Functions here" Do you mean that I have to create an if statement or something there? I've been googling this - can't I just change my text input to "hidden" or "password"? But then I suppose it will change ALL of my input boxes.... I'm stuck!

Link to post
Share on other sites

To invoke a function is to do this

make_text_input('first_name', 'First Name', NULL); // This calls the function "make_text_input()" and passes it 3 arguments, 1 => 'first_name', 2 => 'First Name' and 3 => 'NULL'

 

That is an example of how to invoke a function. If you ran the code I gave you in the last post, you should see no input boxes, because you haven't invoked the function.

 

A clue is that the invocation i just showed you above, which is something you wrote earlier, will not work with those arguments.

 

Just look at the code and notice that you need to pass an argument as $password.

 

That argument is then used to help create the input which is this line here:

 


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

Edited by Jonathon
Link to post
Share on other sites

Jonathon - here is what I have.... From what I can see.... I did that.

<?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 . ': ';

 

// Begin the input:

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

print '<input type="password" name="' . $password . '" 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', NULL);

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

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

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

 

 

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

 

?>

If I add two lines:

 

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

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

 

I get two input boxes, but the second line goes with my make_text_input ('password', 'Enter your password', '$password'); line. I just don't understand why this isn't working. I have the argument $password and I am referring to it in my make_text_input function and I am also clarifying the input type that the $password variable should be....

Link to post
Share on other sites

Hello April,

 

Look at the source code this generates, it will show you that the password field

 

<input type="password" name="$password" size="20" />

 

So that should straight away tell you there is a problem.

 

This is your problem invocation

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

 

You never specify $password in the script. Password is in the function as an argument but you give it no value in the script where you use it.The function i gave you before will work. So use this code


<?php
// 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 . ': ';

// Begin the input:
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>'."\n";

} // End of make_text_input() function.

// Make the form:
print '<form action="" method="post">';


// Create some text inputs:

/******INVOKE FUNCTION TO MAKE INPUT FIELD HERE********
* first name input
* last name input
* email input
* password input
***************************************************/

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

?>

 

Though you are probably frustrated with this, persevere you are a lot closer to fixing this than you think. You just need to call the functions with the right arguments for it to work and its done. Make sure you use the code I have left for you as it wont work with your previous attempt.

 

I've also tidying the printing up so when you view the page source it will be over several lines not just one long line of code that's awkward to read.

 

So to clarify at this stage all you need to do is call the functions (Where I have left lots of big comments in the code) and pass the correct arguments to them to make the input fields and you will be complete. Then if you are struggling how it fits together we'll walk through the code so you know how it all fits together.

Edited by Jonathon
Link to post
Share on other sites

Maybe if you could show me what the input for just the name should look like - I'm having trouble differentiating at how these custom functions should look when they are doing the same thing.

 

By the way - I appreciate you not just "giving me the answer" I am learning as I go....

Link to post
Share on other sites

By the way - I'm actually trying things throughout our conversations - and this is my latest, which didn't give me errors but my source doesn't look the way you want it to:

// Create some text inputs:

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

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

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

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

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...