关于表单和 php 的问题(清除表单、实时刷新、多行)
我有几个关于表单和 PHP 的问题,但如果我应该将它们放入不同的帖子中,那么我会的。
这是我的表单代码:
<form id="t-form" name="tForm" action="translate.php" method="POST">
<div id="t-bar">
<div class="t-select">
<select name="start-lang" id="choice-button">
<option value="english">English</option>
</select>
<label>into</label>
<select name="end-lang" id="choice-button" onChange="document.forms['tForm'].submit();">
<option value="caps"<?php if ($resLang == 'caps') echo ' selected="selected"'; ?>>CAPS</option>
<option value="lowercase"<?php if ($resLang == 'lowercase') echo ' selected="selected"'; ?>>lowercase</option>
</select>
<input type="submit" id="t-submit" value="Translate">
</div>
</div>
<div id="t-main">
<textarea id="txt-source" name="t-src" autofocus="autofocus" placeholder="Type in what you would like to convert…" onChange="document.forms['tForm'].submit();"><?php echo $source; ?></textarea>
<input type="button" id="t-clear" onclick="this.form.elements['t-src'].value=''">
<textarea id="txt-result" name="txt-result" readonly disabled="disabled" placeholder="result..."><?php echo $result; ?></textarea>
<input type="button" id="t-copy" name="t-copy">
</div>
</form>
问题 1: 我目前有 onclick="this.form.elements['t-src'].value=''"
可以清除一个按下按钮时的文本框。是否可以使用相同的属性清除表单中的两个文本区域?我似乎无法在任何地方找到用 1 个按钮清除 2 个元素的答案。我不想清除表单,因为我想保留选定的下拉值,这就是我这样做的原因。
问题 2:我将如何实现结果文本区域的实时刷新,以便用户只需键入并查看结果?我查看了所需的 ajax 和 jquery,但很困惑,因为大多数都没有显示如何输出到表单元素,而只显示到 div。 (类似于谷歌翻译)
问题 3: 我意识到,如果用户在文本区域中换行,当他们提交翻译时,就会出现 php 标头错误。我有什么想法可以避免这种情况吗?这是我在表单中使用的 translate.php 文件的标题:
header("location: /?txt-result=$result&t-src=$textSource&end-lang=$outputLang");
我只是尝试将其作为学习练习,并且非常感谢对这三个问题的任何指导或答案。非常感谢您的帮助!
I have several questions regarding forms and PHP but if I should put them into different posts then I will.
Here is my form code:
<form id="t-form" name="tForm" action="translate.php" method="POST">
<div id="t-bar">
<div class="t-select">
<select name="start-lang" id="choice-button">
<option value="english">English</option>
</select>
<label>into</label>
<select name="end-lang" id="choice-button" onChange="document.forms['tForm'].submit();">
<option value="caps"<?php if ($resLang == 'caps') echo ' selected="selected"'; ?>>CAPS</option>
<option value="lowercase"<?php if ($resLang == 'lowercase') echo ' selected="selected"'; ?>>lowercase</option>
</select>
<input type="submit" id="t-submit" value="Translate">
</div>
</div>
<div id="t-main">
<textarea id="txt-source" name="t-src" autofocus="autofocus" placeholder="Type in what you would like to convert…" onChange="document.forms['tForm'].submit();"><?php echo $source; ?></textarea>
<input type="button" id="t-clear" onclick="this.form.elements['t-src'].value=''">
<textarea id="txt-result" name="txt-result" readonly disabled="disabled" placeholder="result..."><?php echo $result; ?></textarea>
<input type="button" id="t-copy" name="t-copy">
</div>
</form>
Question 1: I currently have onclick="this.form.elements['t-src'].value=''"
which clears one textbox when the button is pressed. Is it possible to have the same attribute clear both textareas in my form? I can't seem to find an answer anywhere for clearing 2 elements with 1 button. I do not want to clear the form as I would like to keep the selected dropdown values so that is why I'm doing it this way.
Question 2: How would I go about implementing a live refresh of the results textarea so they user can simply type and see the result? I've look at the ajax and jquery required and am confused as most don't show how to output to a form element and only to a div. (Similar to google's translate)
Question 3: I realized that if a user does a new line in the textarea, when they submit for translate, it gives them a php header error. Any ideas how I can avoid this? This is my header for the translate.php file used in the form:
header("location: /?txt-result=$result&t-src=$textSource&end-lang=$outputLang");
I am merely trying to do this as a learning excersise and would really appreciate any guidance or answers to the three questions. Many thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题 1
你应该有:
在 javascript 中,类似:
问题 2
太广泛了,无法在这里作为答案,你应该真正看看 jQuery 的 $.ajax,并创建一个带有特定疑问的不同问题。
问题3
使用PHP urlencode()函数
question 1
you should have:
and in javascript something like:
question 2
it is far too broad to put here as an answer, you should really look at jQuery's $.ajax, and create a different question with specific doubts.
question 3
use the PHP urlencode() function
答案 1: 让您的
onclick
事件调用一个函数来为您清除这些值:答案 2: 添加一个
onkeydown
源文本区域中的事件执行翻译(或任何需要执行的操作),然后将结果放入结果文本区域中:答案 3: 也许是一个
onsubmit
事件这form
元素将清理文本区域的输入。查看 JavaScript 的 encodeURIComponent。也许这对你有用:Answer 1: Have your
onclick
event call a function which clears those values for you:Answer 2: Add an
onkeydown
event in the source textarea that peforms the translation (or whatever it needs to do) and then puts the result in the result textarea:Answer 3: Perhaps an
onsubmit
event in theform
element that will sanitize the input from the text area. Have a look at JavaScript's encodeURIComponent. Perhaps this will work for you: