displaying html form data in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
charlie12345
Forum Newbie
Posts: 14
Joined: Wed Nov 09, 2005 5:35 am

displaying html form data in php

Post by charlie12345 »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I've read (a lot) in the PHP manual and can't see why/how this occurs. I know that php variables can't contain spaces, if I read the manual correctly.

I have an .html file that passes form data (with spaces in the html form field), to a .php file that also has a form, but I can get only the first piece of data ("x") displayed in the php form.

Here's what I have:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="en-us">
</head>
<body>

<?

// DEBUG

   $fname = $_POST['fname'];
   echo "fname:  ".$fname."<BR><BR>";
   $len=strlen($fname);
   echo "   len:  ".$len;

// value of $fname is  displayed as   x x   and the value of strlen($fname) is displayed as 3  

//END DEBUG
?>

	<form id="newdata" name="newdata" method="post" action="xxxx.cfm?sid=AF065"> 
	      <input type="text" name="fname" value= <? echo $fname ?>>
	      <input type="text" name="fname2" value=<? echo $_POST['fname'] ?>>
	</form> 
</body>
</html>
Is is possible to get the entire field to completely display in the php form?

Regards,
Charlie


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Does it have to be html

Post by churt »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


When I put your test code into a php files it works fine.  But when I run it from the html file it has problems.  I'll look into the reason a little more but thought this might solve the issue for the moment.  Unless you already know this and are just curious why it doesn't work the other way.  I find I am curious about that myself.

Code: Select all

<?
echo '<html>
<head>
<title>Test Code</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="en-us">
</head>
<body>';
// DEBUG

   if($_POST['fname']) $fname = $_POST['fname'];
   echo "fname:  ".$fname."<BR><BR>";
   $len=strlen($fname);
   echo "   len:  ".$len;

// value of $fname is  displayed as   x x   and the value of strlen($fname) is displayed as 3 

//END DEBUG
echo "
   <form method='post' action='$php_self'>
         <input type=text name=fname value='$fname'>
         <input type=text name=fname2 value='$fname'>
         <input type=submit name=test value='test'>
   </form>";
?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

There was quite a bit at issue there. Try this:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="en-us">
</head>
<body>

<?php
// You need some defaults
$fname = isset($_POST['fname']) ? $_POST['fname'] : '';
$fname2 = isset($_POST['fname2']) ? $_POST['fname2'] : '';

// To see debug information change this to true
$debug = false;
// DEBUG
if($debug) {
   echo "fname:  ".$fname."<BR>";
   echo "   len:  ".strlen($fname) . '<BR><BR>';
   
   echo "fname2:  ".$fname2."<BR>";
   echo "   len:  ".strlen($fname2) . '<BR><BR>';
}
//END DEBUG
?>
   <form id="newdata" name="newdata" method="post" action="<?php echo basename(__FILE__); ?>">
         <input type="text" name="fname" value= <?php echo $fname; ?>>
         <input type="text" name="fname2" value=<?php echo $fname2; ?>>
         <input type="submit" name="submit" value="submit"><br />
         <?php if (!empty($fname)) echo 'For fname you entered: ' . $fname . '<br />'; ?>
         <?php if (!empty($fname2)) echo 'For fname2 you entered: ' . $fname2 . '<br />'; ?>
   </form>
</body>
</html>
charlie12345
Forum Newbie
Posts: 14
Joined: Wed Nov 09, 2005 5:35 am

Post by charlie12345 »

Thank you. Rewrite 1 works fine, rewrite 2 has the same problem I did. Thanks for the help; as you can tell I'm learning and only know enough to be dangerous.

Regards,
Charlie
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Quote the php echo block in value= ...
Otherwise, it is not valid (strict) HTML code
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
churt
Forum Commoner
Posts: 39
Joined: Wed Oct 04, 2006 9:59 am

Found the issue

Post by churt »

After a bit of searching I found that you may have an issue with parsing php in an html file. The following link has a good discussion about proper configuration and overriding default settings for apache. If your running your web server on windows I'm not sure. I tried a number of the changes suggested and one of them worked. I can now get your original code to display properly at least.

http://www.webmasterworld.com/forum88/603.htm
charlie12345
Forum Newbie
Posts: 14
Joined: Wed Nov 09, 2005 5:35 am

Post by charlie12345 »

Thanks, I'll read up on it.
Post Reply