0

I've tried several different variations of concatenating the input from the text boxes, but none of them worked. Can anyone help by show me how to concatenate all of these on one line (back to back)?

HTML

<form method="post" action="sample.php">

<p>My Information
<br />City State  <input type="text" name="item1" size="30">
<br />State Name   <input type="text" name="item2" size="30">
<br />County Name    <input type="text" name="item3" size="30">

</p>

<input type="submit" value="Submit Information">

</form>

PHP

<?php

print "<h4>Geographic Location<b/h4>>";
$filename = 'data/'.'sample.txt';
$fp = fopen($filename, 'w');   //w opens the file for writing
$cntr = 1;
while(true)
{
    $item_name = 'item'.$cntr;
    $item = $_POST[$item_name];
    if (empty($item))
    {
        break;
    }
    $cntr++;
    print "Item: ".$item."<br />";
    $output_line = $item."\n";
    fwrite($fp, $output_line);
}
6
  • $output_line .= $item." "; i assume you ant a space between each Commented Oct 1, 2015 at 23:55
  • @Dagon No, that's not necessary. He's writing each line to the file inside the loop, so he doesn't need to concatenate them. Commented Oct 1, 2015 at 23:55
  • oh true that... although he should take the file write outside the loop Commented Oct 1, 2015 at 23:56
  • 1
    If you want them all on one line, why are you concatenating \n? Commented Oct 1, 2015 at 23:56
  • 2
    thre <br/> is only displayed, its not written to the file Commented Oct 1, 2015 at 23:58

1 Answer 1

1

assuming this is the whole form this can be greatly simplified, there is no need for any loop:

print "<h4>Geographic Location<b/h4>>";
$filename = 'data/'.'sample.txt';
$fp = fopen($filename, 'w');   //w opens the file for writing
$output_line = $_POST['item1'].' '.$_POST['item2'].' '.$_POST['item3']; //create the string to write to file 
print "Item: ".$output_line."<br />"; //display to the user
fwrite($fp, $output_line); //write to the file

changing item 1-3 to more descriptive names is recommended

Sign up to request clarification or add additional context in comments.

Comments

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.