简体   繁体   English

从表单中提取数据到文本文件

[英]Extracting data from a form into a text file

i want to place the contents of values from a form into a text file. 我想将值的内容从表单放到文本文件中。 When an option is changed in the form and submit button is clicked, the user is taken to another page where info about the company's address, website and so are displayed line after line like so: 更改表单中的选项并单击“提交”按钮后,用户将被带到另一个页面,其中逐行显示有关公司地址,网站等的信息,如下所示:

Member Search Results: 会员搜索结果:

Company 公司
800-555-555 800-555-555
Address Line 1 地址第一行
Address Line 2 地址行2
Website: http://example.com 网站: http//example.com

Though the text file shows up blank. 虽然文本文件显示为空白。 How do i go about achieving my desired result? 我该如何达到我想要的结果? My code: 我的代码:

<html>
<form action="http://*****.com/business_detail_framed.asp" method="post" name="b1">
<Select Name="select_business" onChange="b1.input1.value=b1.select_business[this.selectedIndex].text">
<option value="5102" name="float">Company One</option>
<option value="5053" name="float">Company Two</option>
<option value="5091" name="float">Company 3</option>
</select>
<input value="Submit" type="submit" onchange="this.form.submit()">;
</form>

<?php

file_put_contents("businessinfo.txt", $_POST['float'], FILE_APPEND);

$g = file_get_contents("businessinfo.txt");
echo $g;

$r = $_REQUEST['float'];
$r .= $_POST['float'];

echo $r;


?>

The name attribute of your <select> element is select_business . 您的<select>元素的name属性是select_business You should be using $_POST['select_business'] rather than $_POST['float'] . 您应该使用$_POST['select_business']而不是$_POST['float']

An HTML <select> element will pass its value in the form post by its own name attribute, rather than by any name attributes on its associated <option> s. HTML <select>元素将通过其自己的name属性而不是其关联的<option>上的任何name属性在表单发布中传递其值。

You should also check if the the $_POST has been submitted before doing the actual file write. 您还应该在实际写入文件之前检查$_POST是否已提交。 I've also added a line break for readability in the output file. 我还为输出文件的可读性添加了换行符。

if (isset($_POST['select_business'])) {
  file_put_contents("businessinfo.txt", $_POST['select_business'] . "\n", FILE_APPEND);
}

You are not really writing to file. 您并不是真正在写文件。 Here is an example on how to 这是有关如何

$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);

You also need to able to tell if you have submitted before you write. 在编写之前,您还需要知道是否已提交。

So I would have something like this: 所以我会有这样的事情:

<html>
<form method="post" name="b1">
<Select Name="select_business" onChange="b1.input1.value=b1.select_business[this.selectedIndex].text">
<option value="5102" name="float">Company One</option>
<option value="5053" name="float">Company Two</option>
<option value="5091" name="float">Company 3</option>
</select>
<input value="Submit" type="submit" onchange="this.form.submit()">;
</form>

<?php

if ($_REQUEST['Submit']) {

$fp = fopen('businessinfo.txt', 'w');
fwrite($fp, 'selected_business: ' . $_REQUEST['select_business']);
fclose($fp);

header="location http://*****.com/business_detail_framed.asp";

?>

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

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