简体   繁体   English

使用ajax时返回空会话

[英]return empty session when using ajax

I have a problem with this code. 我对此代码有疑问。 I am trying to pass an input value to use it in another page using session but it returns nothing. 我正在尝试传递输入值以在另一个使用会话的页面中使用它,但是它什么也没有返回。 I am using ajax to prevent page reload. 我正在使用ajax来防止页面重新加载。 the contents of the table are the seminars' details from the database and each button handle the seminar_id. 该表的内容是数据库中研讨会的详细信息,每个按钮都处理了workshop_id。 the code is 该代码是

c.php c.php

<?php
   session_start();
?>                 
<div class="container">
<table class="semin">               
  <tr>                               
    <th>TITLE</th>
    <th>DESCRIPTION</th>
    <th>DATE</th>
    <th>START TIME</th>
    <th>END TIME</th>
    <th>AVAILABILITY</th>
    <th>BOOKING</th>                                
</tr>                   
 <?php     
  if(isset($_POST["ID"]))
   {
     $_SESSION["id"]=$_POST["sid"];
    }                       
      $con=mysqli_connect("localhost","test","test","test");
      $a= "select * from SEMINAR1 where DATE >= CURDATE() and VACANCY > 0";
        $result = $con->query($a);
        if ($result->num_rows > 0)
        {
          // output data of each row
            while($row = $result->fetch_assoc())
            {
            ?>
                <tr>
                    <td><?php echo $row['TITLE']?></td>
                    <td><?php echo $row['DESCRIPTION']?></td>
                    <td><?php echo $row['DATE']?></td>
                    <td><?php echo $row['START_TIME']?></td>
                    <td><?php echo $row['END_TIME']?></td>
                    <td><?php echo $row['VACANCY']?></td>
                    <td>   
                     <form class="regist" method="post" >
  <input type="submit" value="register" name="ID" class="reg1"/>
  <input type="hidden" id="sid" name="do" value="<?php echo $row['ID'];?>"/>
  <input type="hidden" name="vac" value="<?php echo $row['VACANCY'];?>"/>
                     </form>
                    </td>                                                                            
            <?php
            }
        }                           
    ?> 
 </table>
        <script type="text/javascript">
        $('form.regist').on('submit', function(){
            $('#content').load('a.php');
            return false;
            });
            </script>
 </div>

and the page that I am using to print the session is a.php 我用来打印会话的页面是a.php

<?php
   session_start();
   $val=$_SESSION["id"];
   echo $val;
?>  

First of all I think it should be $_SESSION['id'] = $_POST['do'] . 首先,我认为应该是$_SESSION['id'] = $_POST['do'] $_POST['sid'] doesn't exist , you have id="sid" and name="do" on that DOM element. $ _POST ['sid']不存在 ,您在该DOM元素上具有id =“ sid”和name =“ do”。

Also as I can see you don't submit your form via ajax (but I'm sorry if other part of your code does this, I just want to be sure to help you out). 同样,正如我所见,您没有通过Ajax提交表单(但是很抱歉,如果代码的其他部分这样做了,我只是想确保为您提供帮助)。

$('form.regist').on('submit', function(){
    $('#content').load('a.php');
    return false;
});

This code actually stops the form being submitted and loads a.php content. 该代码实际上停止了表单的提交并加载了a.php内容。

Try this code and see if it works: 试试下面的代码,看看它是否有效:

$('form.regist').on('submit', function() {
    // preventing form submission
    e.preventDefault();
    // getting form data
    var data = $(this).serialize();
    // sending form data via ajax post
    $.post('url', data, function() {
        // after request completed loading a.php content
        $('#content').load('a.php');
    });
});

Hope I helped. 希望我能帮上忙。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM