Iinserting default values by variable in html form

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
kc11
Forum Commoner
Posts: 73
Joined: Mon Sep 27, 2010 3:26 pm

Iinserting default values by variable in html form

Post by kc11 »

Hi All,

I have an HTML form that i would like to preload with default values by setting variables rather than hard coding the form itself. I have not been able to figure out how to do this though. Here is my function:

Code: Select all

 function render_form()
	    {
        
        
        $word	= "dog";
	$limit 		= "100";
	$site 		= "dogs.com";

        
        
		    echo '<h2>QUERY FORM:</h2><form id="form1" name="form1" method="post" action="">
		    <table   border="0" cellpadding="5">
		      <tr>
			    <td>Site Name </td>
			    <td>
			      <label>
				    <input name="site" type="text" id="site" value=$site size="60"/>
				    </label>
		       
			    </td>
		      </tr>
		      <tr>
			    <td>Keywords</td>
			    <td><label>
			      <input name="keywords" type="text" id="keywords" value= $word size="60"/>
			    </label></td>
		      </tr>
		      <tr>
			    <td>Limit</td>
			    <td><label>
			      <input name="limit" type="text" id="limit" value=$limit />
			    </label></td>
		      </tr>
		      <tr>
			    <td>&nbsp;</td>
			    <td>&nbsp;</td>
		      </tr>
		      <tr>
			    <td>&nbsp;</td>
			    <td><label>
			      <input type="submit" name="Submit" value="Submit" />
			    </label></td>
		      </tr>
		    </table> </form>';
        
        
        
	    }

Does anyone have any thoughts on how to do this.

Thank you,

KC
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Iinserting default values by variable in html form

Post by twinedev »

You are using single quotes to wrap what you are echoing out. Anything in there is passed directly. You are wanting double quotes which will evaluate variables and things like \n \t, etc.

See http://www.php.net/manual/en/language.types.string.php for more info.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Iinserting default values by variable in html form

Post by Neilos »

Also

Code: Select all

$site = "dogs.com";

echo "value=$site"
will echo value=dogs.com and not value="dogs.com"

you could use...

Code: Select all

$site = '"dogs.com"';

echo "value=$site"
Which would echo value="dogs.com".

Furthermore simply changing the echo ' ... ' to echo " ... " will cause you all sorts of problems. I would stick with the single quotes and use concatenation. eg...

Code: Select all

$site           = "dogs.com";

echo '<input name="site" type="text" id="site" value="' . $site . '" size="60"/>';
kc11
Forum Commoner
Posts: 73
Joined: Mon Sep 27, 2010 3:26 pm

Re: Iinserting default values by variable in html form

Post by kc11 »

Thanks Guys. Its working.

I have to admit , I am still confused. From reading the attached link, Twindev, why does for example " '. $variable . ' " get expanded but " $variable" not get evaluated. after all the latter is enclosed in double quotes and I would think should be expanded.

Thanks again,

KC
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Iinserting default values by variable in html form

Post by Neilos »

Single quotes are literal strings and double quotes aren't. Literal strings print the literal contents of a string (with a few exceptions).

Code: Select all

$variable = "This is a string."

echo $variable . " And some more.";
// prints This is a string. And some more.

echo "$variable And some more.";
// prints This is a string. And some more.

echo '$variable And some more.';
// prints $variable And some more.

$variable2 = 'This is a string with "quotation" marks.';

echo "$variable2 And some more.";
// prints This is a string with "quotation" marks. And some more.

echo '$variable2 And some more.';
// prints $variable2 And some more.

kc11
Forum Commoner
Posts: 73
Joined: Mon Sep 27, 2010 3:26 pm

Re: Iinserting default values by variable in html form

Post by kc11 »

Thanks Neilos.

But by following your logic shouldn't

"value=$site" or value=$site ( from my original form ) get processed to value=dogs.com ( after all they are wrapped in double quotes ). But this is not what happens.

Best regards,

KC
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Iinserting default values by variable in html form

Post by Neilos »

Code: Select all

"value=$site"
Returns a string; for instance

Code: Select all

$site = "dogs.com";
$string = "value=$site";
echo $string;
will echo: value=dogs.com

Code: Select all

value=$site
Will generate an error and is not what is in your original form

Code: Select all

'value=$site'
is in your original form and returns a string: value=$site

Besides <input value=dogs.com /> is bad markup it should be <input value="dogs.com" /> which is the point I was trying to make.

If this doesn't clear things up for you then I don't understand your question and you will have to be more specific.
Post Reply