1

I am trying to have the input field add text into a file that is on the server and add it to the select element on the webpage dynamically but it is not working out so well. Currently I am getting an error that e1 is null and the text is not getting appended to my testdoc.txt text file. If I take out the code to try and write to the text file this works without issue. I also tried without the dynamic adding to the select field to see if it would append to file and that does not go. In firebug I do see the post and I see a response but it does not get appended to the file. Below is a snippet of my current code.

function submitxhr(){
 var e1=document.getElementById('item');
 var e2=document.getElementById('items');
 var o=document.createElement('option');
 var xhr = new XMLHttpRequest();
 xhr.overrideMimeType("text/plain; charset=x-user-defined");
 o.value=e1.value;
 o.text=e1.value;
 e2.options.add(o);
 xhr.open("POST","testdoc.txt");  
 xhr.send(e1);
};

 <form id="myForm"> 
   <input name="item" type="text" value="" />  
   <select size="3" id="items" name="items">
   <input type="button" value="submit"  ONCLICK="submitxhr()">   
 </form>

My next step is then to create a remove button for the options in the select form but I have not gotten there yet but any help would be much appreciated.

[UPDATE] If I am able to pass the XHR POST into a python CGI script I could figure out both adding and removing. Currently, I am unable to pass the POST into the python CGI script. It cannot find the e1 variable when passed.

import cgi
import cgitb
cgitb.enable()

form = cgi.FieldStorage()

value1 = form.getvalue[e1]

f = open(r'testdoc.txt', 'a')
f.write(e1)
f.close()

I even checked out this question but when I make a test script using this, I also get a null value, "message = 'writelines() requires an iterable argument'". In my Python CGI script, how do I save to disk a file uploaded via POST request of data entered by the user in a form?

It does look like the value is getting passed but maybe I am not pulling the data out correctly. f = , f.writelines = , fileitem = FieldStorage('userfile', None, 'sghjsjh')

Thanks

4

2 回答 2

1

您忘记id在第一个input字段中添加 !它应该是:

<input id="item" name="item" type="text" value="" />

代替

<input name="item" type="text" value="" />
于 2013-10-27T01:45:27.107 回答
0

只是对于任何来看这个并想在玩了几个小时后知道答案的人,我终于能够通过 python CGI 脚本更新我的文本文件,感谢 kol 他得到了它,所以我可以输入一个选项我的选择字段,因为我完全错过了一个关键字段。

我需要将我的原始网页更新为以下内容:

function submitxhr(form){
 var e1=document.getElementById('item');
 var e2=document.getElementById('items');
 var o=document.createElement('option');
 var xhr = new XMLHttpRequest();
 xhr.overrideMimeType("text/plain; charset=x-user-defined");
 o.value=e1.value;
 o.text=e1.value;
 e2.options.add(o);
 var formElement = form;
 formData = new FormData(formElement);
 xhr.open("POST","appendtest3.cgi");
 xhr.send(formData);
};

 <form id="myForm" name="myForm" action="" method="POST">  
 <input id="item" name="item" type="text" value="" />
 <select size="3" id="items" name="items">
 <input type="button" value="submit"  ONCLICK="submitxhr(this.form)">
 </form>

并将其添加为我的 python CGI 脚本,以将输入字段传递到我的文本文件中。

fs = cgi.FieldStorage()
payload = fs.getvalue('item', 'unknown')

f = open(r'testdoc.txt', 'a')
f.writelines(payload + "\n")
f.close()

这适用于我需要它做的事情。我确信有更好的方法,但这就是我现在所拥有的。

于 2013-10-27T21:45:41.003 回答