简体   繁体   English

如何从没有表单字段的表单传递数据? (PHP)

[英]How to pass data from form without a form field? (PHP)

I have a form for editing a users name and email. 我有一个用于编辑用户名和电子邮件的表格。 So when it updates the name and email, it needs the username to identify which row it should update. 因此,当更新名称和电子邮件时,它需要用户名来标识应更新的行。

So i wanted to know if there is any element which is passed with the form but without showing the value or being editable in the input tag. 所以我想知道是否有任何随表格传递的元素,但未显示值或在输入标签中不可编辑。

So i get the username from one script. 所以我从一个脚本获取用户名。 The edit user script gets the name and email from the database with the specified username. 编辑用户脚本使用指定的用户名从数据库获取名称和电子邮件。 Then it passes that new name and email with the username to another script which updates it. 然后,它将新名称传递并通过电子邮件将用户名发送给另一个脚本,以对其进行更新。

I believe you are looking for 我相信你正在寻找

 <input type='hidden' name='username' value='theusername' />

hidden - can only be seen in the source of your HTML document hidden -只能在HTML文档的源代码中看到
name - where it will be in the $_REQUEST/$_POST/$_GET ($_POST or $_GET depending on how you are submitting your form) variable on submit name -提交时$ _REQUEST / $ _ POST / $ _ GET($ _ POST或$ _GET,取决于您提交表单的方式)变量中的位置
value - the username you want this form to relate to value -您要与此表单相关的用户名

PRO TIP : Have a way to tell who is trying to update users so you don't have unauthorized people updating your user information. 专家提示 :有一种方法可以告诉谁在尝试更新用户,这样就不会有未经授权的人更新您的用户信息。 It would be very easy for someone to change the username in the form and try to update someone else. 某人更改表单中的用户名并尝试更新其他人非常容易。

您可以使用隐藏的输入类型

<input type="hidden" name = "username" value="<?php echo $username ?>">

use an: 使用:

 <input type="hidden" />

HIDDEN is a TYPE attribute value to the INPUT element for FORM s. HIDDENFORMINPUT元素的TYPE属性值。 It indicates a form field that does not appear visibly in the document and that the user does not interact with. 它指示一个表单字段,该表单字段在文档中不可见,并且用户未与之交互。 It can be used to transmit state information about the client or server. 它可用于传输有关客户端或服务器的状态信息。 Hidden fields often store a default value (egvia php), or have their value changed by a JavaScript. 隐藏字段通常存储默认值(例如,通过php),或者通过JavaScript更改其值。

more here 这里更多

使用隐藏的输入标签:

<input type='hidden' name='username' value='theusername' />

You can use a hidden form field: 您可以使用隐藏的表单字段:

<input type="hidden" name="originalUsername" value="something" />

This won't render on the form in the browser and will likely be ignored and unnoticed by the user. 这不会在浏览器中的表单上呈现,并且很可能会被用户忽略和忽略。

However , be aware that this is editable. 但是 ,请注意,这可编辑的。 Do not rely on this as a security measure. 不要以此为安全措施。 When the form is submitted, make sure that the user submitting the form (using whatever authentication and authorization mechanisms you have in place) is authorized to make this change before persisting it to the database. 提交表单后,请确保提交表单的用户(使用您已有的任何身份验证和授权机制)都有权进行此更改,然后再将其保存到数据库中。 Any form field being submitted can be edited. 提交的任何表单字段都可以编辑。

As all the others stated you need a hidden input. 正如所有其他人所述,您需要隐藏的输入。 It WILL be editable though, never trust it as you never trust any other data coming from outside. 但是它将是可编辑的,永远不要信任它,因为您永远不要相信来自外部的任何其他数据。

But I'd like to add that it would be nicer not to use the username for identifying a row, add an ID column as a primary key instead to your database (possibly auto incremented), and use that in your form. 但是我想补充一点,最好不要使用用户名来标识行,而是将ID列作为主键添加到数据库中(可能会自动递增),然后在表单中使用它。

Something like 就像是

<input type="hidden" name="userid" value="<?=$userid?>" />

Arun, you can use GET to pass variables from one page to another page. 阿伦,您可以使用GET将变量从一页传递到另一页。 Simply construct URLs as edituser.php?username=arun and so on. 只需将URL构造为edituser.php?username=arun等等。 This is the only possible way to pass on variables or data, of course apart from cookies, to other pages w/out using form tags. 这是将变量或数据(除了cookie之外)使用表单标签传递到其他页面的唯一可能方法。
Second method is to use JavaScript to create a hidden form field and update it with username. 第二种方法是使用JavaScript创建一个隐藏的表单字段,并使用用户名对其进行更新。
Third one is to simply add hidden input tags. 第三是简单地添加隐藏的输入标签。 But this and latter will require form tags. 但是,这和后者将需要表单标签。

A word of caution, filter user inputs, be JS, GET or hidden fields. 请注意,过滤用户输入,可以是JS,GET或隐藏字段。

Use this if you want to use it safely: 如果您想安全使用它,请使用它:

<input type='hidden' name='username' value='<?php echo encode("Please Encode Me!","This is a key"); ?>' />

wich will result into: 这将导致:

<input type='hidden' name='username' value='p3e4e4241674d2r4m4i5o464a4f2p3k5c2' />

and in the modification script you will have to use: 在修改脚本中,您将必须使用:

<?php $username = decode("p3e4e4241674d2r4m4i5o464a4f2p3k5c2","This is a key"); ?>

Below you have the PHP functions for the ENCODE/DECODE: 下面是用于ENCODE / DECODE的PHP函数:

<?php

function encode($string,$key) {
    $key = sha1($key);
    $strLen = strlen($string);
    $keyLen = strlen($key);
    for ($i = 0; $i < $strLen; $i++) {
        $ordStr = ord(substr($string,$i,1));
        if ($j == $keyLen) { $j = 0; }
        $ordKey = ord(substr($key,$j,1));
        $j++;
        $hash .= strrev(base_convert(dechex($ordStr + $ordKey),16,36));
    }
    return $hash;
}

function decode($string,$key) {
    $key = sha1($key);
    $strLen = strlen($string);
    $keyLen = strlen($key);
    for ($i = 0; $i < $strLen; $i+=2) {
        $ordStr = hexdec(base_convert(strrev(substr($string,$i,2)),36,16));
        if ($j == $keyLen) { $j = 0; }
        $ordKey = ord(substr($key,$j,1));
        $j++;
        $hash .= chr($ordStr - $ordKey);
    }
    return $hash;
}

?>

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

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