PHP代码中的访问会话变量值 [英] php access session variable value in html code
问题描述
我想实现的是在html form上打印php会话变量。是我试图打印
valid_firm Session
变量,但它用于打印变量的方式不起作用...
newFirm-form.php
<!DOCTYPE>
< html>
< head>
< meta charset =utf-8>
< meta content =no-cachehttp-equiv =cache-control>< / meta>
< / head>
< body>
< form id =firm-formmethod =post/>
< p>公司名称< / p>< input class =firm-nametype =textname =firm-name/>
< p>电子邮件< / p>< input type =textname =firm-email/>
< p>电话< / p>< input type =textname =telephone/>
< p>地址< / p>< input type =textname =address/>
< p>城市< / p>< input type =textname =city/>
< p>公司代码< / p>< input type =textname =firm-code/> <峰; br />
< / form>
<?php print $ _SESSION [valid_firm]; ?>
< input class =submittype =submitvalue =Submit>< br />
< span class =errorstyle =display:none>请填写空字段< / span>
< span class =successstyle =display:none>表单提交成功< / span>
< / body>
< footer>
< script type =text / javascriptsrc =../ js / libs / jquery-1.11.3-min.js> < /脚本>
< script type =text / javascriptsrc =../ js / newFirm.js>< / script>
< / footer>
< / html>
使用ajax post方法调用提交表单的JavaScript文件
$ b ()。$ b pre $ $ c $ $ $(document).ready(function(){
var empty_fields = 0;
$(。submit)。 ()函数(){
$(#firm-form *)。filter(:input)。 ){
empty_fields + = 1;
}
});
if(empty_fields> 0){
$(。error)。fadeIn(800 ).show();
$(。success)。fadeOut(800).hide();
}
else {
var firm_name = $(。firm -name)。val();
$ .ajax({
type:POST,
url:../ registration-control / register-firm.php,
data:firm_name =+ firm_name,
success:function(){
$(。success)。fadeIn(800).show();
$( )。fadeOut(800).hide();
}
});
}
empty_fields = 0;
});
});
以及用于保存会话变量的php文件
<?php
if(isset($ _ POST [firm_name])){
validateName($ _ POST [firm_name]);
$ _SESSION [firm] = $ _POST [firm_name];
// echo $ _SESSION [firm]。from register-firm;
}
函数validateName($ firm_name){
var_dump($ firm_name);
if(preg_match(/ ^ [A-ZA-Ω] {1} [a-zA-Zα-ωA-Ω0-9\。] * /,$ firm_name)&&!ctype_digit ($ firm_name)){
$ _SESSION [valid_firm] = 1;
}
$ _SESSION [valid_firm] = 0;
}
?>
$ c> session_start()这个函数应该首先在你想访问会话的每个页面中调用
EDITED
我可以说感谢你的-1。
当我说你的 session_start()
我这样做是因为能够访问php脚本中的会话,您必须在调用之前调用 session_start()
$ _ SESSION
。在<!DOCTYPE html>
之前的消息响应之前,应调用 session_start()
/ p>
what i would like to achieve is to print php session variable on html form.Above
is the form where i am trying to print the valid_firm Session
variable but the way it is used to print the variable is not working...
newFirm-form.php
<!DOCTYPE>
<html>
<head>
<meta charset="utf-8">
<meta content="no-cache" http-equiv="cache-control"></meta>
</head>
<body>
<form id="firm-form" method="post" />
<p>Firm Name</p><input class="firm-name" type="text" name="firm-name" />
<p>Email</p><input type="text" name="firm-email" />
<p>Telephone</p><input type="text" name="telephone" />
<p>Address</p><input type="text" name="address" />
<p>City</p><input type="text" name="city" />
<p>Firm Code</p><input type="text" name="firm-code" /> <br/>
</form>
<?php print $_SESSION["valid_firm"]; ?>
<input class="submit" type="submit" value="Submit"><br />
<span class="error" style="display:none"> Please Fill The Empty Fields</span>
<span class="success" style="display:none"> Form Submitted Success</span>
</body>
<footer>
<script type="text/javascript" src="../js/libs/jquery-1.11.3-min.js"> </script>
<script type="text/javascript" src="../js/newFirm.js"></script>
</footer>
</html>
javascript file with ajax post method call to submit the form
$(document).ready(function() {
var empty_fields = 0;
$(".submit").on("click", function(){
$("#firm-form *").filter(":input").each(function(){
if(!$(this).val()){
empty_fields += 1;
}
});
if(empty_fields > 0){
$(".error").fadeIn(800).show();
$(".success").fadeOut(800).hide();
}
else {
var firm_name = $(".firm-name").val();
$.ajax({
type:"POST",
url:"../registration-control/register-firm.php",
data: "firm_name=" + firm_name,
success: function(){
$(".success").fadeIn(800).show();
$(".error").fadeOut(800).hide();
}
});
}
empty_fields = 0;
});
});
and the php file to save the session variable
<?php
if (isset($_POST["firm_name"])){
validateName($_POST["firm_name"]);
$_SESSION["firm"] = $_POST["firm_name"];
//echo $_SESSION["firm"]." from register-firm";
}
function validateName($firm_name){
var_dump($firm_name);
if (preg_match("/^[A-ZΑ-Ω]{1}[a-zA-Zα-ωΑ-Ω0-9\.]*/", $firm_name) && !ctype_digit($firm_name)){
$_SESSION["valid_firm"] = 1;
}
$_SESSION["valid_firm"] = 0;
}
?>
I don't see where is your session_start()
this function should be first called in every page where you want to access session
EDITED
What can I say thank you for your -1.
When I said where is your session_start()
I did it because to be able to access session in a php script you must call session_start()
before to call $_SESSION
. More of that the session_start()
should be called before any response that mesans before <!DOCTYPE html>
.
这篇关于PHP代码中的访问会话变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!