Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6695139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:11:08+00:00 2026-05-26T06:11:08+00:00

I am stuck on a problem for a PHP assignment I have. I created

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:11:09+00:00Added an answer on May 26, 2026 at 6:11 am

    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){
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite stuck with a problem sending mail from a PHP script. Some data:
I have a server-side php-script that fetches results from a MySQL table. I am
Level: PHP Learner I'm stuck with a checkbox problem. I have a db that
i have a weird problem where css generated via php (stuck with a weird
I have a bit of a problem with my PHP code, I am assigning
I am stuck with a semi-hard problem with 'simple html dom' (php class used
I have been stuck on this problem for days and am about the crack...
I have been stuck on this problem for almost 2 days now, consider the
I'm still stuck on my problem of trying to parse articles from wikipedia. Actually
I am stuck on a little jQuery problem. I have a collection of links

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.