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条)

报告相同问题?

悬赏问题

  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页
  • ¥20 如何通过代码传输视频到亚马逊平台
  • ¥15 php查询mysql数据库并显示至下拉列表中
  • ¥15 freertos下使用外部中断失效
  • ¥15 输入的char字符转为int类型,不是对应的ascall码,如何才能使之转换为对应ascall码?或者使输入的char字符可以正常与其他字符比较?
  • ¥15 devserver配置完 启动服务 无法访问static上的资源
  • ¥15 解决websocket跟c#客户端通信