how to submit php session varibles into form in hidden fields?

守給你的承諾、 提交于 2019-12-11 07:53:16

问题


I have a script that creates server session variables.

I need to insert 2 of these variables into a simple form (on a php page) using hidden form fields.

<?php
session_start();
?><pre><?php
print_r($_SESSION);
?> 

I created a simple php page with the code above. I can see the session variables easily on the page, as you can see in this part of the code I copied from the page:

[login] => andrew8855
[pass] => $P$BIsPUTQ/e4mJSlaDsRLA48mB20xxIC1
[email] => andrew@mysiteaddress.com
[name_f] => Andrew
[name_l] => 
[street] => 
[street2] => 
[city] =>

In the custom form, I need to get/print the variables from above into the form as so:

<input type="hidden" name="login_email" id="login_email" value="[email]" />
<input type="hidden" name="login_user" id="login_user" value="[login]" />

So when the form is submitted, the variables that exist in the session will be submitted in the hidden form fields.

After searching for hours, I found that I might be able to do this somehow with session_start();....but I'm not clear about how to accomplish this. Thank you for any suggestions.


回答1:


A session is started with the session_start() function.

Session variables are set with the PHP global variable: $_SESSION.

if you are not using any automatic session handling framework ...if you use your own script then you have to must start session before it use.you can try

<?php
// Start the session<br/>
session_start();
?>

which is set beginning of your script.after that you can use session variables.

Change your html template as such :

<input type="hidden" name="login_email" id="login_email" value="<?php echo $_SESSION['email'];?>" />
<input type="hidden" name="login_user" id="login_user" value="<?php echo $_SESSION['login'];?>" />

Since PHP 5.4 the inline echo short tags are always enabled regardless of the short_open_tag (php.ini) setting.

if it's not enable then please Set

short_open_tag=On in php.ini

after that restart your Apache server then you can use

<?=$_SESSION['email']?>

instead of :

<?php echo $_SESSION['email'];?>

you can try http://www.w3schools.com/php/php_sessions.asp for session.i think it's helpful for you.

and How to enable PHP short tags? for enable php short tags.




回答2:


Change your html template as such :

<input type="hidden" name="login_email" id="login_email" value="<?php echo $_SESSION['email'];?>" />
<input type="hidden" name="login_user" id="login_user" value="<?php echo $_SESSION['login'];?>" />

Note that if you have short_open_tags enabled you can do :

<?=$_SESSION['email']?>

instead of :

<?php echo $_SESSION['email'];?>



回答3:


<?php
 // I suppose (and I can tell by the code you wrote that it is so) that you already have some values 
 // in the $_SESSION array

 session_start();

 echo "<input type=\"hidden\" name=\"login_email\" id=\"login_email\" value=\"" . $_SESSION['email'] . "\"";

?>

Maybe there's something wrong with the "" and backslashes, but the idea is that you have to put as value $_SESSION['mail'] (or login)



来源:https://stackoverflow.com/questions/27048855/how-to-submit-php-session-varibles-into-form-in-hidden-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!