我想實現的是在html上打印php會話變量form.Above
是我試圖打印valid_firm Session
變量的形式,但它用於打印變量的方式不是工作... newFirm-form.php的php訪問會話變量值在html代碼
<!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文件與阿賈克斯後的方法調用提交表單
$(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;
});
});
和pH值P檔保存會話變量
<?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;
}
?>
啓動會話以獲取任何頁面中的會話變量。所以首先你需要添加session_start();在嘗試獲取會話變量的值之後在頁面頂部。 ajax請求成功後需要刷新頁面。 –
我做了那個..但是如果我想在點擊提交按鈕後檢查會話變量,並提示用戶輸入公司名稱輸入有效或無效的信息呢? – tsiro