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"];
}
?>