du958642589 2018-03-13 13:29
浏览 123
已采纳

在完成函数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"]; 
  }
?>
  • 写回答

3条回答 默认 最新

  • dpr77335 2018-03-13 13:45
    关注

    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
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站