简体   繁体   English

串联php代码

[英]Concatenating in php code

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 的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

<?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);
}

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 建议将项目1-3更改为更具描述性的名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM