[英]Concatenating in php code
我嘗試了幾種不同的方式來合並來自文本框的輸入,但是沒有一種有效。 誰能幫我示范如何將所有這些連接在一起(背對背)?
的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);
}
假設這是整體形式,可以大大簡化,則不需要任何循環:
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
建議將項目1-3更改為更具描述性的名稱
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.