Page 1 of 1

Iinserting default values by variable in html form

Posted: Sat Sep 24, 2011 3:46 pm
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

Re: Iinserting default values by variable in html form

Posted: Sat Sep 24, 2011 10:19 pm
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.

Re: Iinserting default values by variable in html form

Posted: Sun Sep 25, 2011 1:08 am
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"/>';

Re: Iinserting default values by variable in html form

Posted: Sun Sep 25, 2011 10:47 am
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

Re: Iinserting default values by variable in html form

Posted: Sun Sep 25, 2011 1:04 pm
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.


Re: Iinserting default values by variable in html form

Posted: Mon Sep 26, 2011 10:30 am
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

Re: Iinserting default values by variable in html form

Posted: Tue Sep 27, 2011 4:03 pm
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.