Dynamic Number of Textarea's

Discussion in 'PHP' started by Shadow, Mar 13, 2006.

  1. #1
    I never figured out how to do it so I thought I'll just ask.

    I don't have any specific code, I have always worked around that problem but now I'm at a point where I have to somehow do it.

    Let's say I've got an array:

    $myarray = array('321', '54', '74', '13', '876');
    Code (markup):
    Now I'm putting every value into a textarea:

    
    foreach ($array as $key=>$val)
    {
    echo '<textarea>'.$val.'</textarea>';
    }
    
    Code (markup):
    Now my question: How can I make those values editable? With this array I've got 5 textareas. I can't give them the same name, so how to do it?

    It's for a shopping cart for editing the number of ordered items. I'm sure you know what I mean.

    PS. Yes, I know that I need to submit the forms and such, but didn't want to make a huge post.
     
    Shadow, Mar 13, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    $x = 1;
    foreach ($array as $key=>$val)
    {
    echo '<textarea name="Area'.$x.'">'.$val.'</textarea>';
    $x++
    }
    
    PHP:
     
    T0PS3O, Mar 13, 2006 IP
  3. Shadow

    Shadow Peon

    Messages:
    191
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, thank You, but how do I check for changes after submitting the form? :eek:

    
    if (???? != $val[$i]) {
    
    PHP:
    EDIT: It doesn't have to be <textarea>, can be input or whatever else works. Anything that could print an unknown number of input fields and save the changes.

    ADDED:
    Ok, did it that way:

    
    	foreach ($array as $key=>$val)
    	{
    			echo '
    				<form method="GET" name="form'.$key.'">
    				<input type="text" name="to" value="'.$val.'" />
    				<input type="submit" name="change" value="'.$key.'" />
    				</form>
    			';
    	}
    
    
    PHP:
     
    Shadow, Mar 13, 2006 IP