473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Is there a way to make a form name a variable?

I'm building a chart, and it's going through a for loop, which creates new text boxes. I want to be able to save the information put in each text box in some sort of variable array.

Right now it's only letting me save the very last one in a variable I've named $memo. I thought that if I echoed $memo within the loop it would print out each text box's input, but it's only taking the last one and printing that to the screen the amount of textboxes that I have.

This is basically the code in the for loop which is in a form:
Expand|Select|Wrap|Line Numbers
  1. {
  2. <input type="text" size="5" name="memo" id="memo" value="" />
  3.  
  4. $memo = $_POST['memo']; 
  5. echo $memo;
  6.  
  7. }
  8.  <input type="submit" name="Save" value="Save" />
Is there a way to set up my text input so that I can do some sort of variable array, like
Expand|Select|Wrap|Line Numbers
  1. <input type="text" size="5" name=$memo[$i] id="memo" value="" />
  2. $i++;
Any suggestions? Thanks!

-Coco
Nov 27 '07 #1
5 2656
Lumpy
69
I am not sure if I completely understand what you are wanting to do, but anyway to answer what I think your question is, yes you can do something like that.

You would want to setup your memo variable something like this....

Expand|Select|Wrap|Line Numbers
  1.  
  2. $memo[] .= $_POST['memo'];
  3.  
  4.  
And that will create the array variable $memo and add each entry after the last one. So the first key in the array would be 0, or $memo[0].

Then to call the variables in the input section, you would simply use an echo statement something like....

Expand|Select|Wrap|Line Numbers
  1.  
  2. echo '<input type="text" size="5" name="'.$memo[$i].'" id="memo" value="" />';
  3.  
  4.  
Than each loop through, you will get a different name for that input, as long as you continue to increase $i.

Hope this helps!
Nov 27 '07 #2
pbmods
5,821 Expert 4TB
To nitpick on Lumpy's post:

Expand|Select|Wrap|Line Numbers
  1. $memo[] .= $_POST['memo'];
  2.  
is technically not correct and will generate an E_NOTICE error, because this statement attempts to concatenate a variable that does not exist.

Expand|Select|Wrap|Line Numbers
  1. $memo[] = $_POST['memo'];
  2.  
is the correct method (and produces the expected result).
Nov 27 '07 #3
That's exactly what I want to do, but for some reason it isn't working. I want to echo out the $memo[] array of what was typed in each text box. Any suggestions to what I have done up? Right now it's not echoing out anything.


[PHP]<?
$memo[] = $_POST['memo'];

for($j=0;$j<=6;$j++){
echo '
<form name="saving" method="post" action="">
<td valign="top" height="50" class="Text"><p align="center">
<input type="text" size="5" name="'.$memo[$j].'" id="memo" value="" /></td>';

if(isset( $Save )){

echo $memo[$j];


}

}
echo '</tr>

<input type="submit" name="Save" value="Save" />
</form>';
?>[/PHP]
Nov 28 '07 #4
pbmods
5,821 Expert 4TB
Heya, Coco.

Try this instead:
Expand|Select|Wrap|Line Numbers
  1. ?>
  2. <form name="saving" method="post" action="">
  3. <table>
  4.     <tr>
  5. <?php
  6.     for( $j = 0; $j < 6; ++$j )
  7.     {
  8. ?>
  9.         <td valign="top" height="50" class="Text">
  10.             <p align="center">
  11.                 <input type="text" size="5" name="memo[]" value="" />
  12.             </p>
  13.         </td>
  14. <?php
  15.     }
  16. ?>
  17.     </tr>
  18. </table>
  19.  
When the User submits the form, $_POST['memo'] will contain an array.
Nov 28 '07 #5
Thank you! That made my program work perfectly!

JOSH
Nov 28 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that...
13
by: genetic.error | last post by:
I'm moving from Vb6 to VB.Net. I have a feeling this has come up before... The VS.Net MSDN file seems to state that the following should work: Form1.Show Form1.Visible = True Form1.Hide...
14
by: Oleg | last post by:
Hello there: I've been trying to create two different sets of required fields in one form and to use a radiobutton as sort of a switcher between these sets. In my HTML form there are two...
2
by: umashd | last post by:
Hi, I am doing a web based project for my graduation. I studied bit of java for backend processesing and javascript for the client. Here is the scenario. In the FORM, I use INPUT TYPE=text...
11
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
8
by: Johnny | last post by:
I'm a rookie at C# and OO so please don't laugh! I have a form (fclsTaxCalculator) that contains a text box (tboxZipCode) containing a zip code. The user can enter a zip code in the text box and...
21
by: Just Me | last post by:
I've tried in a few places using a variable name Form and it appears to be OK. For example: Public Shared Sub WritePositionsInRegistry(ByVal Form As Form, ByVal SubkeyName As String) Is it OK...
6
by: Leszek | last post by:
Hi. I wrote a script: function zmiana(ile){ while(document.getElementById('accomp').childNodes.length>1){ ostatni=document.getElementById('document.dane.accomp').lastChild;...
4
by: jedimasta | last post by:
Good evening all, I'm a relatively new to javascript, but I've been working with ColdFusion and PHP for years so I'm not necessarily ignorant, just stuck and frustrated. Using ColdFusion I'm...
4
by: Greg Scharlemann | last post by:
I'm trying to setup a dyamic dropdown list that displays a number of text fields based on the selected number in the dropdown. The problem I am running into is capturing the data already entered...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.