不使用表单设置POST变量 [英] Setting POST variable without using form

查看:97
本文介绍了不使用表单设置POST变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有方法可以在不使用表单相关字段(no type ='hidden')并仅使用PHP的情况下设置 $ _ POST ['var'] >。
类似于

  $ _ POST ['name'] =Denniss; 

有没有办法做到这一点?



<编辑:
有人问我一些这方面的细节。因此,例如,我有页面上有一个表单,表单看起来像这样

 < form method ='post 'action ='next.php'> 
< input type ='text'name ='text'value =''/>
< input type ='submit'name ='submit'value ='Submit'/>
< / form>

点击提交按钮后,我想重定向到next.php。有没有办法让我将$ _POST ['text']变量设置为另一个值?我如何使这个持久化,以便当我点击另一个提交按钮(例如)$ _POST ['text']将是我在next.php上设置,而不使用隐藏的字段。



让我知道这是否仍然不清楚,并感谢您的帮助。

解决方案

只需将其设置为另一个值即可:

  $ _ POST ['text'] ='其他值'; 

这将覆盖与文字数组的键。 $ _ POST 是超全局关联数组,您可以像常规PHP数组那样更改值。



警告:此更改仅在相同的PHP执行范围内可见。一旦执行完成并且页面已经加载,则清除 $ _ POST 数组。新的表单提交将生成一个新的 $ _ POST 数组。



如果您想在表单提交中保留该值,您需要将它作为输入标记的属性放入窗体中,或者从数据存储中检索它。

Is there a way to set a $_POST['var'] without using form related field (no type='hidden') and using only PHP. Something like

$_POST['name'] = "Denniss";

Is there a way to do this?

EDIT: Someone asked me for some elaboration on this. So for example, I have page with a form on it, The form looks something like this

<form method='post' action='next.php'>
<input type='text' name='text' value='' />
<input type='submit' name='submit' value='Submit'/>
</form>

Once the submit button is clicked, I want to get redirected to next.php. Is there a way for me to set the $_POST['text'] variable to another value? How do I make this persistent so that when I click on another submit button (for example) the $_POST['text'] will be what I set on next.php without using a hidden field.

Let me know if this is still not clear and thank you for your help.

解决方案

Yes, simply set it to another value:

$_POST['text'] = 'another value';

This will override the previous value corresponding to text key of the array. The $_POST is superglobal associative array and you can change the values like a normal PHP array.

Caution: This change is only visible within the same PHP execution scope. Once the execution is complete and the page has loaded, the $_POST array is cleared. A new form submission will generate a new $_POST array.

If you want to persist the value across form submissions, you will need to put it in the form as an input tag's value attribute or retrieve it from a data store.

这篇关于不使用表单设置POST变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆