Jump to content



Photo
- - - - -

Chapter 10 Pursue Question #2 & # 3

pursue questions

  • Please log in to reply
42 replies to this topic

#1 AprilSwenby

AprilSwenby

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 2 November 2011 - 9:12 PM

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
  • 0

#2 Larry

Larry

    Administrator/Writer

  • Administrators
  • 3,783 posts
  • LocationState College, PA (USA)

Posted 4 November 2011 - 6:27 PM

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!
  • 0

#3 AprilSwenby

AprilSwenby

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 4 November 2011 - 10:13 PM

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....
  • 0

#4 Jonathon

Jonathon

    Advanced Member

  • Members
  • PipPipPip
  • 1,020 posts

Posted 5 November 2011 - 7:01 AM

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 :-)
  • 0

#5 AprilSwenby

AprilSwenby

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 5 November 2011 - 7:29 PM

I'm Sorry Jonathon - but that didn't help. I am so confused at this point. I think I just need to be pointed in the right direction? Larry do you have anymore input for me?
  • 0

#6 Jonathon

Jonathon

    Advanced Member

  • Members
  • PipPipPip
  • 1,020 posts

Posted 5 November 2011 - 8:49 PM

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.
  • 0

#7 AprilSwenby

AprilSwenby

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 6 November 2011 - 12:27 PM

Okay - well that makes sense... But I haven't the foggiest idea on how to do that. I having such a mental block with this one. Seems like I can read code and understand what it is trying to do, but I can't write it.....
  • 0

#8 AprilSwenby

AprilSwenby

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 6 November 2011 - 12:39 PM

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>';
  • 0

#9 AprilSwenby

AprilSwenby

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 6 November 2011 - 12:50 PM

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?
  • 0

#10 Jonathon

Jonathon

    Advanced Member

  • Members
  • PipPipPip
  • 1,020 posts

Posted 6 November 2011 - 1:32 PM

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.
  • 0

#11 AprilSwenby

AprilSwenby

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 6 November 2011 - 1:49 PM

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>';
  • 0

#12 Jonathon

Jonathon

    Advanced Member

  • Members
  • PipPipPip
  • 1,020 posts

Posted 6 November 2011 - 1:59 PM

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).
  • 0

#13 AprilSwenby

AprilSwenby

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 6 November 2011 - 2:22 PM

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?
  • 0

#14 AprilSwenby

AprilSwenby

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 6 November 2011 - 2:27 PM

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>';

?>
  • 0

#15 Jonathon

Jonathon

    Advanced Member

  • Members
  • PipPipPip
  • 1,020 posts

Posted 6 November 2011 - 2:50 PM

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.
  • 1

#16 AprilSwenby

AprilSwenby

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 6 November 2011 - 3:00 PM

I am really sorry that - but I just am not connecting here...
So you think I should change this line? print '<input type="text" name="' . $name . '" size="20" '; or add another line like it that will print input types of "password"?
  • 0

#17 Jonathon

Jonathon

    Advanced Member

  • Members
  • PipPipPip
  • 1,020 posts

Posted 6 November 2011 - 3:08 PM

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?
  • 1

#18 AprilSwenby

AprilSwenby

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 6 November 2011 - 3:15 PM

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.
  • 0

#19 Jonathon

Jonathon

    Advanced Member

  • Members
  • PipPipPip
  • 1,020 posts

Posted 6 November 2011 - 3:20 PM

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

  • 1

#20 AprilSwenby

AprilSwenby

    Advanced Member

  • Members
  • PipPipPip
  • 85 posts

Posted 6 November 2011 - 3:28 PM

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!
  • 0