Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using EVAL for dynamic variable names 1

Status
Not open for further replies.

jacktripper

Programmer
Dec 5, 2001
124
US

I've read a lot of posts relating to this, but I'm pretty new and not really getting anywhere.

I have a form with <textarea> fields with dynamic names such as "field1", "field2", etc. Those numbers could be anything, because they are indexes coming from a database.

When I post that form data to the next page, I know what the number is, but all I seem to be able to do is generate a string with the right variable name. Can't I use eval() to get the data being posted with that variable name? Eval just keeps saying its being used incorrectly.

What am I doing wrong here?
 
I wouldn't use eval() in this instance.

Why not just use $_POST['field1'], $_POST['field2'], etc? It's easy to dynamically build a string that can be used as the index to an associative array.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
What does your code that generates the form look like? If we saw it, we might be able to suggest a better way of getting to your data.

Ken
 
I guess this is what I am trying to say. My form on page one is something like:

<form action="updateDB.php" method="post">
<input type="hidden" name="thisOne" value="<? echo $row['thisOne']; ?>">
<textarea name="body<? echo $row['thisOne']"><? echo $row['mainBody']; ?></textarea>
</form>


ON the next page my attempted script currently looks something like this:

<?
$new_body = "\$body" . $thisOne;
UpdateDB($thisOne, $new_body));
?>

but instead of putting in my blob of textarea text, it just updates the database with text of the variable name itself.

I'm sure this is a pretty stupid thing on my part, but any help would be great.
 
Assuming that the variable $thisOne has a value, if your script performs:

<?
$new_body = $_POST['body' . $thisOne];
UpdateDB($thisOne, $new_body));
?>

Should do it.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks! That worked. I was looking for that $_POST command somewhere in the on-line manual. Thanks for explaining what that actually does....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top