Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    using printf() with forms

    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:

    Code:
    function GenerateHTMLForm ($formValues, $actionScript, $submitLabel) {
    	printf("<form method=post action=\"%s\"><PRE>\n", $actionScript);
    	printf("Name: 
    			<input type=text size=35 name=cn value=\"%s\">
    			<br>\n", ($formValues) ? $formValues["cn"] : " ");
    	printf("E-mail:
    		<input type=text size=35 name=mail value=\"%s\">
    		<br>\n", ($formValues) ? $formValues["mail"] : " ");
    	printf("City:
    			<input type=text size=35 name=city value=\"%s\">
    			<br>\n", ($formValues) ? $formValues["city"] : " ");
    	printf("Description:
    			<input type=text size=35 name=description value=\"%s\">
    			<br>\n", ($formValues) ? $formValues["description"] : " ");
    	printf("Telephone:
    			<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 );
    	printf("</PRE></form>");
    	}
    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.

    Thanks!
    Last edited by jjinto; 05-02-2009 at 05:50 PM.

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Are you actually calling the function, or did you just define it? Once you define the function, you need to call it, e.g.
    PHP Code:
    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(), 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.
    Code:
    <form method="post" action="script.php" id="theForm" name="theForm">
    Last edited by bdl; 05-02-2009 at 06:02 PM. Reason: Clarification

  • #3
    New to the CF scene
    Join Date
    May 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •