我想将输入值传递给php会话变量。使用下面的代码,我可以将输入值发送到会话,但只出现在警告框中或者我刷新页面,这是我不想要的。我添加了行$ ("# form1") [0] .reset ();
来刷新表单,因此会在屏幕上打印会话的值,但它不起作用。
index.php文件:
<?php
@session_start();
error_reporting(0);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>input value to php session</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<form method="post" id="form1">
<input type="text" name="field1" id="field1" onblur="run(this)">
</form>
<br /><br />
<?php echo $_SESSION['field1'];?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script language="javascript">
function run(sel) {
var text = $("#field1").val();
if (text != "") {
$.ajax({
type: "POST",
url: "input.php",
data: {
field1: text
}
}); //.done(function() {alert(text)});
}
if (done.(function() {
$("#form1").reset()
}));
}
</script>
</body>
</html>
input.php文件:
<?php
@session_start();
error_reporting(0);
if (isset($_POST["field1"])){
$_SESSION['field1'] = $_POST["field1"];
}
?>
您可以围绕您的值创建一个<div>
并在.done()
时更改它:
要获得会话值,您必须使用echo
并在done()
回调中使用它:
<?php
@session_start();
error_reporting(0);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>input value to php session</title>
</head>
<body>
<form method="post" id="form1">
<input type="text" name="field1" id="field1" onblur="run(this)">
</form>
<br /><br />
<!-- HERE wrap into a div with an ID -->
<div id="session-field"><?php echo $_SESSION['field1'];?></div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script language="javascript">
function run(sel) {
var text = $("#field1").val();
if (text != "") {
$.ajax({
type: "POST",
url: "input.php",
data: { field1: text}
})
// done callback using data (wha
.done(function(data) {
$('#session-field').html(data);
$("#field1")[0].form.reset();
});
}
}
</script>
</body>
</html>
而你的input.php:
<?php
@session_start();
error_reporting(0);
if(isset($_POST["field1"])){
$_SESSION['field1'] = $_POST["field1"];
echo $_SESSION['field1'] ; // echo outputs
}
?>
如果您只是想显示刚刚发送给PHP的值,您可以通过几种方式完成。
首先,您可以使用JS放入页面
function run(sel) {
var text = $("#field1").val();
$('#some_div').append(text);
if (text != "") {
$.ajax({
type: "POST",
url: "input.php",
data: {
field1: text
}
});
}
或者如果你想确定它被保存为SESSION
变量,你可以从PHP返回它然后用JS将它放在页面上
<?php
@session_start();
error_reporting(0);
if (isset($_POST["field1"])){
$_SESSION['field1'] = $_POST["field1"];
echo $_SESSION['field1'];
}
?>
然后在你的JS中
function run(sel) {
var text = $("#field1").val();
$('#some_div').append(text);
if (text != "") {
$.ajax({
type: "POST",
url: "input.php",
data: {
field1: text
},
success: function(data) {
$('#some_div').append(data);
}
});
}
只需在id输出会话中为你的HTML添加一个div,然后在input.php中打印$ _POST [“field1”],这样它就会被附加到该div。
的index.php
<script language="javascript">
function run(sel) {
var text = $("#field1").val();
if (text != "") {
$.ajax({
type: "POST",
url: "input.php",
data: { field1: text},
success: function(msg){
if (msg != ''){
$("#output-session").html(msg);
}
}
});
}
</script>
<div id="output-session"></div>
input.php
<?php
@session_start();
error_reporting(0);
if(isset($_POST["field1"])){
$_SESSION['field1'] = $_POST["field1"];
print $_POST["field1"];
}
?>