在完成函数jquery上重新加载表单
I want to pass an input value to a php session variable.
With the code below, I can send the input value to the session but only appears in the alert box or if I refresh the page, which I do not want.
I added the line $ ("# form1") [0] .reset ();
to refresh the form and thus have the value of the session printed on the screen, but it is not working.
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>
The input.php file:
<?php
@session_start();
error_reporting(0);
if (isset($_POST["field1"])){
$_SESSION['field1'] = $_POST["field1"];
}
?>
You could create a <div>
around your value and change it when .done()
:
To get your session value, you have to echo
it and use it inside your done()
callback:
<?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>
And your input.php:
<?php
@session_start();
error_reporting(0);
if(isset($_POST["field1"])){
$_SESSION['field1'] = $_POST["field1"];
echo $_SESSION['field1'] ; // echo outputs
}
?>
If you are trying to simply display the value that was just sent to PHP, you can do it in a couple of ways.
First, you could just put in on the page with JS
function run(sel) {
var text = $("#field1").val();
$('#some_div').append(text);
if (text != "") {
$.ajax({
type: "POST",
url: "input.php",
data: {
field1: text
}
});
}
Or if you want to be sure it was saved as the SESSION
variable, you can return it from PHP and then put it on the page with JS
<?php
@session_start();
error_reporting(0);
if (isset($_POST["field1"])){
$_SESSION['field1'] = $_POST["field1"];
echo $_SESSION['field1'];
}
?>
and then in your 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);
}
});
}
Just add a div to your HTML with id output-session and then print the $_POST["field1"] in input.php so it get's appended to that 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"];
}
?>