在完成函数jquery上重新加载表单

时间:2018-03-13 13:29:40

标签: javascript php jquery html

我想将输入值传递给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"]; 
  }
?>

3 个答案:

答案 0 :(得分:2)

您可以围绕您的值创建analysis.cpp:408:33: error: the value of ‘sz2’ is not usable in a constant expression array<array<array<double,7>,sz2>,sz1> lnmat; ^~~ analysis.cpp:407:15: note: ‘sz2’ was not initialized with a constant expression const int sz2=idx2.size(); ^~~ analysis.cpp:408:36: error: the value of ‘sz2’ is not usable in a constant expression array<array<array<double,7>,sz2>,sz1> lnmat; ^ analysis.cpp:407:15: note: ‘sz2’ was not initialized with a constant expression const int sz2=idx2.size(); ^~~ analysis.cpp:408:36: note: in template argument for type ‘long unsigned int’ array<array<array<double,7>,sz2>,sz1> lnmat; ^ analysis.cpp:408:38: error: the value of ‘sz1’ is not usable in a constant expression array<array<array<double,7>,sz2>,sz1> lnmat; ^~~ analysis.cpp:406:15: note: ‘sz1’ was not initialized with a constant expression const int sz1=idx1.size(); ^~~ analysis.cpp:408:41: error: template argument 1 is invalid array<array<array<double,7>,sz2>,sz1> lnmat; ^ analysis.cpp:408:41: error: the value of ‘sz1’ is not usable in a constant expression analysis.cpp:406:15: note: ‘sz1’ was not initialized with a constant expression const int sz1=idx1.size(); ^~~ analysis.cpp:408:41: note: in template argument for type ‘long unsigned int’ array<array<array<double,7>,sz2>,sz1> lnmat; ,并在<div>

时进行更改

要获取会话值,您必须.done()并在echo回调中使用它:

done()

你的input.php:

<?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>

答案 1 :(得分:0)

如果您只想显示刚刚发送给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);
            }
          }); 
        }

答案 2 :(得分:0)

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