Jump to content

Recommended Posts

Posted

Is it possible to generate page content through php where the actual default values are a variable?

 

What i'm hoping to do is something along the lines of this:

 

for($x=1;X<=5;x++)

{

echo "<input name='$variablename_".x."' type=text value='$_post['$variablename_".x."']'>";

}

 

 

I realise an array would make this completely easier, but i'm hoping to be able to pass to a function the variable name I want to be generated on a form as plain text and have yet to figure out how to accomplish this. Thanks in advance for any assistance I may receive.

 

-Moraff

 

 

 

  • 4 weeks later...
Posted

You can't really do that. There is an alternative. You can get what the user submitted and use a switch/case statement to get required variable. Since you don't seem have an infinite number of variables, this should work. Sorry if I'm not really clear. I can't think of a better way to explain this. Maybe with some code?

 

switch $_POST['input'] {
    case 'a':
        $var = $a;
        break;
    case 'b':
        $var = $b;
        break;
// Repeat for the other 3...
}

  • 2 weeks later...
Posted
Is it possible to generate page content through php where the actual default values are a variable?

 

What i'm hoping to do is something along the lines of this:

 

for($x=1;X<=5;x++)

{

echo "<input name='$variablename_".x."' type=text value='$_post['$variablename_".x."']'>";

}

 

 

I realise an array would make this completely easier, but i'm hoping to be able to pass to a function the variable name I want to be generated on a form as plain text and have yet to figure out how to accomplish this. Thanks in advance for any assistance I may receive.

 

-Moraff

 

 

I don't think I'm understanding you correctly, but yeah. You can use PHP and HTML interchangeably. So for example, say you have a variable X, and you want an HTML Input Form's default value to be X. You could do:

 

<?php
    for ($varX=1; $varX <= 5; $varX++) {
        echo "<input name='$varX' type='text' value='$varX'/>";
    }
?>

 

However, it would be easier, I think, to not use the echo, and just write the HTML:

 

<?php
for ($varX=1; $varX <= 5; $varX++) { ?>
	<input name='<?php $varX ?>' type='text' value='<?php $varX ?>'/>
<?}
?>

 

Even though the HTML is outside of the <?php ?> tags, it is still nested in the loop.

 

 

 

I'm not sure what you meant to do by using $_POST. Could you explain that briefly so I can understand the problem better?

 

Cheers,

~Derek

P.S. We really need a PHP code tag. I think it would be a great addition.

P.P.S. Yeah, I think this topic is dead...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...