View Full Version : using printf() with forms

05-02-2009, 06:46 PM
Hello everybody.

I've been learning PHP from a few books and Lynda.com tutorial, and I've been doing pretty well until this completely stumped me:

I'm following an example in a book for making a simple address book.
The book has been pretty good up until this.

The code:

function GenerateHTMLForm ($formValues, $actionScript, $submitLabel) {
printf("<form method=post action=\"%s\"><PRE>\n", $actionScript);
<input type=text size=35 name=cn value=\"%s\">
<br>\n", ($formValues) ? $formValues["cn"] : " ");
<input type=text size=35 name=mail value=\"%s\">
<br>\n", ($formValues) ? $formValues["mail"] : " ");
<input type=text size=35 name=city value=\"%s\">
<br>\n", ($formValues) ? $formValues["city"] : " ");
<input type=text size=35 name=description value=\"%s\">
<br>\n", ($formValues) ? $formValues["description"] : " ");
<input type=text size=35 name=telephonenumber value=\"%s\">
<br>\n", ($formValues) ? $formValues["telephonenumber"] : " ");
// Generate a button to submit
printf("<input type=submit value=\"%s\">", $submitLabel );

My question is simple, I hope. The page runs fine until it hits this block of code, and then nothing happen. No error msg, no text boxes.

Should I rewrite the code using an html <input> form and pass the php variables that way?

In the lines above, I feel like the ternary should begin with (!$formValues) to see if the condition is not met before choosing between the two options.

Anyway, I am open to any advice.


05-02-2009, 07:00 PM
Are you actually calling the function, or did you just define it? Once you define the function, you need to call it, e.g.

function someFun($param) {
echo $param;

someFun("Hello, World!");

Keep in mind that the function definition can be anywhere in the script (even at the bottom) or in an included script. It doesn't need to literally be defined just before you call it.

Actually, I see no reason to use printf() (http://www.php.net/printf), you can accomplish the same thing with echo statements without the overhead.

Additionally, make sure you surround your markup element attributes in quotes, e.g.

<form method="post" action="script.php" id="theForm" name="theForm">

05-02-2009, 07:22 PM
sorry, I didn't explain that well

this code I quoted was from my includes file, which I call on another page.

I have tried variations on <input type = "text" and 'text' and \"text\", but nothing seems to work.

I'll give it another shot, though.