您可以使用 PHP 设置表单字段值吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2930396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:10:38  来源:igfitidea点击:

Can you set a form field value with PHP?

phpforms

提问by dcp3450

I use the $_GET function to pull a value with php from the url (http:..../index.html?variable=value), basic stuff. However, I need to use that value in my form.

我使用 $_GET 函数从 url (http:..../index.html?variable=value) 中提取带有 php 的值,基本的东西。但是,我需要在表单中使用该值。

Typically, I would set

通常,我会设置

<?php echo 'value="'.$variable.'"' ; ?>

or something to that effect. However, I can't use php inside my form using the editor I'm working with.

或类似的东西。但是,我无法使用我正在使用的编辑器在表单中使用 php。

Can I set a value in the form without using PHP in my form? If so, how?

我可以在表单中设置一个值而不在我的表单中使用 PHP 吗?如果是这样,如何?

Edit: with the CMS editor I'm using you can't store php code to the server. (I'm using FCKeditor. The same used by Dubral). This is a security measure all CMS's use even Wordpress. Basically I want to send/set the value in the form with php. Similar to how javascript does it: document.form.field.value

编辑:使用我正在使用的 CMS 编辑器,您无法将 php 代码存储到服务器。(我正在使用 FCKeditor。Dubral 使用的相同)。这是所有 CMS 甚至 Wordpress 都使用的安全措施。基本上我想用php发送/设置表单中的值。类似于 javascript 的做法:document.form.field.value

采纳答案by Luke Stevenson

The best way to set default values (ie pre-existing values) within an HTML form is as you mentioned in your question value="'.$variable.'"'.

在 HTML 表单中设置默认值(即预先存在的值)的最佳方法是您在问题中提到的value="'.$variable.'"'

If you are unable to use markup like that, then you may be able to use javascript (whether referencing a secondary file/feed using AJAX, or using the same variable echoing technique) to replace the values in the specific fields after the page has loaded.

如果您无法使用这样的标记,那么您可以使用 javascript(无论是使用 AJAX 引用辅助文件/提要,还是使用相同的变量回显技术)在页面加载后替换特定字段中的值.

ie

IE

<script type="text/javascript">
document.form[0].name.value = "<?php echo $name; ?>";
</script>

回答by Matchu

That looks completely valid to me. If you're using something like Dreamweaver, which is the only valid interpretation I can come up with, use a plain text editor instead of the editor you currently use. Problem solved.

这对我来说完全有效。如果您使用的是 Dreamweaver 之类的东西,这是我能想到的唯一有效解释,请使用纯文本编辑器而不是您当前使用的编辑器。问题解决了。

There might be a more specific fix if you were to say what editor you're using, but since you don't, this is the best I can do. Just use Notepad or whatever.

如果您要说您使用的是什么编辑器,可能会有更具体的修复,但既然您没有,这是我能做的最好的。只需使用记事本或其他任何东西。

回答by Mikulas Dite

You can set the value without php, but it has to be static value hardcorcoded in the html itself. For dynamic pages you have to use some programming language.

您可以在没有 php 的情况下设置该值,但它必须是在 html 本身中硬编码的静态值。对于动态页面,您必须使用某种编程语言。