0

I am stuck on a problem for a PHP assignment I have. I created a PHP script to generate form inputs, and I must take the data from the input and insert it into a text document. So I wrote the code that generates two inputs and a function that will write the data to the text document. However, my text document appears blank. How would I properly define a function that takes multiple values from text inputs and inserts them into a text document? Here is my code so far:

<form method='post'> 
<table border='0' cellpadding='3' cellspacing='5'>
  <tr>
    <td><h2>Item</h2></td>
    <td><h2>Amount</h2></td>
  </tr>
<?php

for($row=1; $row<5;$row++)
    {

    $item_name='item_name'.$row;    
    $item_value = $_POST[$item_name];

    $amount_name='amount_name'.$row;    
    $amount_value = $_POST[$amount_name];



echo 
"<tr>
    <td><input type='text' name='$item_name' value='$item_value'  /></td>
    <td><input type='text' name='$amount_name' value='$amount_value' /></td>
";  

    if (!empty($amount_value))
    {
            if(is_numeric($amount_value))
            {
            $total = $total + $amount_value;
            }
            else {
        echo    
            "<td class='bad'>Amount Invalid: $amount_value</td>";
            $error_count++;
            }
    }


echo"   
  </tr>
";

    }
?>  

<tr>
    <td colspan='2'>
    <input name='' type='submit' value='Submit' />
    </td>
</table>
<?php
    if ($error_count > 0)
    {
    echo "<br /><span class='bad' />Errors: $error_count</span>";
    }
    else
    {
    echo "<br />Total: $total";
    }
    if ($error_count == 0)
    {
    addData();
    }


    //Function
    function addData()
    {
    $myFile = "bills.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    $stringData = $amount_value;
    fwrite($fh, $stringData);
    $stringData = $item_value;
    fwrite($fh, $stringData);
    fclose($fh);
    }   

?>
</form>
1
  • Add error_reporting(E_ALL); on top of your script. Then consider adding function parameters. Commented Oct 18, 2011 at 21:35

3 Answers 3

2

When you assign a value to $item_value at the top of the code, you are creating a variable called $item_value within the scope of your php tag. In the function addData(), there is no variable called $item_value, so $stringData = $item_value creates a variable $stringData with a value equal to the new variable item_value with a value of null. Pass the data to your method then you will be able to access it.

Try calling it like:

addData($item_value);

Then change the method to:

function addData($item_value){
Sign up to request clarification or add additional context in comments.

5 Comments

OK, I went ahead and tried this, but the text document is blank. Also, would I need to define both variables? ($item_value and $amount_value)?
Then I don't think you are ever getting to call the method addData because if ($error_count == 0) is returning false. Try adding echo 'ERROR COUNT = 0'; to the code right about the line addData($item_value). If nothing displays you know that you're never getting to call the method either.
Hmm, I added it in and it displayed
So basically I'm trying to figure out why these values are displaying as null
Try the same thing as before, except now try echo $item_value. If something is displayed then you know $item_value isn't empty. The scope of the variable might be ending in the first set of <?php ?> tags.
1

PHP's variable scope rules do not allow global variables to appear within a function, unless you explicitly declare them global. So within your addData() function, $amount_value and $item_value are both undeclared/undefined LOCAL variables, and get converted to nulls/empty strings for the fwrite() calls, so you code can only ever write out an empty file.

Either pass the values as function parameters:

addData($amount_value, $item_value);

or declare them as globals:

function addData() {
   global $amount_value, $item_value

Of the two, go with the parameter version. Global variables are messy.

2 Comments

I've tried both methods, but I just get a blank text document everytime
Don't forget that you're actually loading 5 different sets of POSt data into those two vars. Only the last set will "stick", and if those were submitted empty, then you get empty variables.
0

You'll need to pass $amount_value and $item_value in to the function, at least. And probably some carriage returns. e.g.

addData($item_value, $amount_value);

//Function
function addData($item_value, $amount_value)
{
    $myFile = "bills.txt";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, "$amount_value, $item_value\n");
    fclose($fh);
}

2 Comments

Well I did this and my text doc was empty. Any ideas? Will it be ok if the values are in quotes like this: fwrite($fh, "$amount_value, $item_value\n")?
You probably want the a mode for opening the file. That will append to the file (w will overwrite each time). See fopen for more information. The quotes are OK because PHP interpolates variables in double quotes (i.e. it replaces $amount_value with its actual value, such as 4).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.