www.webdeveloper.com
Results 1 to 7 of 7

Thread: Why am I my losing my <textarea> data in the $_POST?

  1. #1
    Join Date
    Apr 2012
    Posts
    10

    Why am I my losing my <textarea> data in the $_POST?

    I am writing a script that will take some form data and present it in a PDF or some other printable format. At the bottom of the form is a "New Line" button. When you click this button a new line in the form appears, and the data in the previous line is added to an array named according to its label tag. My problem is that when the form is submitted the only data that ends up in the $desc[] array is what ever was entered in the last row. can someone please look over my code and see what I may have done wrong?

    Code:
    if (isset($_POST['new_line']) || isset($_POST['submit'])) {
    		
    		// Grab POST data
    		$invoice_quote = trim($_POST['invoice_quote']);
    		$sales_person = trim($_POST['sales_person']);
    		$job = trim($_POST['job']);
    		$due_date = trim($_POST['due_date']);
    		$first_name = trim($_POST['first_name']);
    		$last_name = trim($_POST['last_name']);
    		$address = trim($_POST['address']);
    		$city = trim($_POST['city']);
    		$state = trim($_POST['state']);
    		$zip = trim($_POST['zip']);
    		$phone = trim($_POST['phone']);
    		$email = trim($_POST['email']);
    		$qty = array_filter(array_map('trim', $_POST['qty']));
    		$desc = array_filter(array_map('trim', $_POST['desc']));
    		$unit_price = array_filter(array_map('trim', $_POST['unit_price']));
    		$line_total = array_filter(array_map('trim', $_POST['line_total']));
    		$line = trim($_POST['line']);
    		
    		
    			
    		if (isset($_POST['submit'])) {
    			$flag = 'FALSE';
    		
    			// This is only for test proposes, This is untimately where the 
    			// code will go that will generate the PDF
    			echo ('<div class= "info">'.$invoice_quote.' '.$sales_person.' '.$job.' '.$due_date.' '.$first_name.' '.$last_name.' '.$address.' '.$city.' '.$state.' '.$zip.' '.$phone.' '.$email.'<br />');
    			var_dump($qty);
    			echo '<br />';
    			var_dump($desc);
    			echo '<br />';
    			var_dump($unit_price);
    			echo '<br />';
    			var_dump($line_total);
    			echo('</div>');
    			exit();
    			
    		  }
    			
    	}
    
        <form id="invoiceform" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
        <fieldset class= "description">
        							<label for= "qty" class= "qty">Qty</label>
        							<label for= "desc" class= "desc">Description</label>
        							<label for= "unit_price" class= "unit_price">Unit Price</label>
        							<label for= "line_total" class= "line_total">Line Total</label>
        							<br/>
        					<?php
        						if ($flag == 'TRUE') {
        							$n = 0;
        							do {
        					?>
        								<input type= "text" class= "qtyinfo" name= "qty[]" maxlength "10" value= "<?php if (isset($qty[$n])) echo ($qty[$n]); ?>"/>
        								<textarea class= "descinfo" name= "desc[]" id="<?php echo('desc_'.$n); ?>" value= "<?php if (isset($desc[$n])) echo ($desc[$n]); ?>"></textarea>
        								<input type= "text" class= "unit_priceinfo" name= "unit_price[]" id="<?php echo('unit_price_'.$n); ?>" maxlength= "10" value= "<?php if (isset($unit_price[$n])) echo ($unit_price[$n]); ?>" />
        								<input type= "text" class= "line_totalinfo" name= "line_total[]" id= "<?php echo('line_total_'.$n); ?>" maxlength= "10" value= "<?php if (isset($line_total[$n])) echo ($line_total[$n]); ?>" />
        								<br/>
        					<?php
        								$n++;
        							}
        							while ($n < $line);
        						}
        					?>
        					
    
    												
    					<input type= "hidden" id= "line" name= "line" value= "<?php echo $line + 1; ?>"/>
    					<input type= "submit" id= "new_line" name= "new_line" value= "New Line" />
    				</fieldset><br />
    I can provide complete code on request.

  2. #2
    Join Date
    Apr 2012
    Posts
    10

    Why am I my losing my <textarea> data in the $_POST?

    Here is some more information

    when I do a var_dump() on the arrays in question here is what it shows

    array(2) { [0]=> string(1) "4" [1]=> string(1) "8" }
    array(1) { [1]=> string(19) "another description" }
    array(2) { [0]=> string(5) "75.00" [1]=> string(5) "75.00" }
    array(2) { [0]=> string(6) "300.00" [1]=> string(6) "600.00" }

  3. #3
    Join Date
    Apr 2012
    Posts
    10
    After more googling I have found my problem

    <textarea class= "descinfo" name= "desc[]" id="<?php echo('desc_'.$n); ?>" value= "<?php if (isset($desc[$n])) echo ($desc[$n]); ?>"></textarea>
    should have been

    Code:
    <textarea class= "descinfo" name= "desc[]" id="<?php echo('desc_'.$n); ?>" ><?php if (isset($desc[$n])) echo ($desc[$n]); ?></textarea>
    notice in the first example i gave my textarea tag a value attribute, this is invalid HTML and was being ignored and hence not set with the $_POST data

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,321
    Glad we could help.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    lol NogDog you're funny xD

    I guess all that matters is this person got their question answered

    I've had something similar happen to me. I'll struggle with something for hours, and then finally relent and go to a forum (usually this one) and post about the problem. Then I'll go back and fight with it some more. I hate it when I do manage to figure it out on my own cuz now I've wasted everybody's time
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,321
    It's sort of a manifestation of the "rubber duck effect", wherein just talking to someone about a problem and formulating the questions can often help you solve it yourself. (The name came from a programmer who supposedly put a rubber duck on top of his terminal so he could talk to it instead of bothering other developers.)
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357
    Writing does help.

    I tend to write down my questions before I go ask them, because I accumulate questions throughout the day and I think it's better to ask them all at once rather than a couple here, a couple there, etc. Some of the questions never make it to being asked because the sheer act of linguistically looking at the problem makes an idea pop in my head.

    Perhaps describing a problem causes us to look at it in a different way?
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles