TinyPortal

Development => Resources => Topic started by: Sunbringer on January 07, 2008, 05:27:24 PM

Title: Prepopulating a form with variables in a phpbox.
Post by: Sunbringer on January 07, 2008, 05:27:24 PM
I'm trying to enter a variable as the default value in a form within a phpbox.

I've had a look around and believe something like the following:

<INPUT id="sign_id" name="sign_id" type="hidden" value="<?php $sign_idtemp ?>"/>

should work, however this will not work within a phpbox due to the <?php ?>.

according to some threads I read here I out a ?> as the first line of the box and a <?php as the last and get the code to work, but I'm concerned this might make a mess of the rest of my code.

is there another way of getting this input to accept a variable's value as its default value.
Title: Re: Prepopulating a form with variables in a phpbox.
Post by: JPDeni on January 07, 2008, 06:18:35 PM
I would use a php box and use echo statements to output the html form elements. I wouldn't use the <?php and ?> syntax at all.

Quote
according to some threads I read here I out a ?> as the first line of the box and a <?php as the last and get the code to work, but I'm concerned this might make a mess of the rest of my code.

Well, it doesn't work now, so what do you have to lose? If you're concerned about it, copy your code into a text editor. If it messes things up, all you'll have to do is to paste the old code back in.
Title: Re: Prepopulating a form with variables in a phpbox.
Post by: Sunbringer on January 07, 2008, 06:26:36 PM
it is already within an echo statment, removing the rest of the form to just leave this bit it looks like:

echo '
<form action="index.php?page='  . $this_article_id  . '" method="post">
<INPUT id="sign_id" name="sign_id" type="hidden" value=$sign_idtemp />
</form>
';

and $_POST['sign_id'] ends up with the its value as $sign_idtemp rather than the value of this variable.

if you can tell me whats wrong with the above I would be very grateful :)
Title: Re: Prepopulating a form with variables in a phpbox.
Post by: JPDeni on January 07, 2008, 06:38:09 PM
The problem is that you're using ' as the quotation marks in your echo statment. You need to use " instead and escape the "s in your form. Example:


echo "
<form action=\"index.php?page="  . $this_article_id  . "/\"method=\"post\">
<INPUT id=\"sign_id\" name=\"sign_id\" type=\"hidden\" value=$sign_idtemp />
</form>
";



Or you could use the same sytax as you use in the first line of your code:


echo '
<form action="index.php?page='  . $this_article_id  . '" method="post">
<INPUT id="sign_id" name="sign_id" type="hidden" value=' . $sign_idtemp . '/>
</form>
';
Title: Re: Prepopulating a form with variables in a phpbox.
Post by: Sunbringer on January 07, 2008, 06:46:21 PM
Thank you very much... should have know after spending all this time looking it would be something that simple.

Now you point it out I even remember reading a guide that indicated that, just couldn't see my own error.

Again thanks for pointing it out.
Title: Re: Prepopulating a form with variables in a phpbox.
Post by: JPDeni on January 07, 2008, 08:14:06 PM
No problem. Glad I could help. I've made similar errors myself. :-)