单击提交时如何将ID从一页发送到另一页?

时间:2019-05-31 10:04:48

标签: javascript php

我有一个由question_id,问题和选项组成的数据库。我将在其中显示问题和选项。当用户单击提交时,我要存储他们单击的选项和question_id。 我能够存储他们单击的选项,但想知道如何存储question_id。

$user_email = $_SESSION['email'];
$query1="SELECT * FROM votes WHERE user_email='$user_email'";
$query2="SELECT * FROM poll_question WHERE status='1'";
$fetch2 = mysqli_query($con,$query2);
<html>
  <head>   
    <body>
      <div class="container">  
        <br />  
        <br />
        <br />
        <div class="row">
          <div class="col-md-6">
          <?

            while( $row2 = mysqli_fetch_array($fetch2))
            {
             ?>
              <form method="post" class="poll_form">
                <h3><?echo $row2['question']?>?</h3>
                <br />
                <div class="radio">
                 <label><h4><input type="radio" name="poll_option" class="poll_option" value=<?echo $row2['option1']?> /><?echo $row2['option1']?></h4></label>
                </div>
                <div class="radio">
                 <label><h4><input type="radio" name="poll_option" class="poll_option" value=<?echo $row2['option1']?> /> <?echo $row2['option2']?></h4></label>
                </div>

                <br />
                <?php 
                  if( $count >0)
                    {  ?>
                      <button disabled="disabled" alt="<?php echo $row2['question_id']; ?>" rel="<?php echo $user_email; ?>" class="btn btn-primary poll_option" title="<?php echo $ip; ?>">Submit</button>
                       <h>You selected : <? echo $row1['vote_option']; ?></h>
                       <br>
                       <p id="demo"></p>
                       <?php
                    }
                  else 
                    { ?>
                        <button  alt="<?php echo $row2['question_id']; ?>" rel="<?php echo $user_email; ?>" class="btn btn-primary poll_option" title="<?php echo $ip; ?>">Submit</button>
                      <?
                    }
                    ?>
                </form>
           <? } 
          } ?>
      <br />
  </div>
 </div>
   <br />
   <br />
   <br />
</div>
</body>  
<script type="text/javascript">
    $(document).ready(function(){

        $(".poll_form").submit(function(event){
            event.preventDefault();   //prevents the form from submitting normally
                var poll_option = '';

                $('button.poll_option').each(function(){

                     if($(this).prop("checked"))
                         {
                             poll_option = $(this).val();

                             var option=poll_option;

                         }


                     var url = '<?php echo SITE_URL; ?>';
                     });
                $.post("poll_vote.php",$('.poll_form').serialize(),
                function(data)
                    {
                      if(data=="User Created Success"){                

                           window.setTimeout(function () {
                                location.href ="<?php echo SITE_URL; ?>poll.php";
                            }, 100);
                      }
                      else{

                          $("#result").html(data);
                      }
                     }
                  );
             //to reset form data
        }); 
    });
    <? } ?>
    </script>

</html>

使用下面的函数,我将poll_option发送给其他保留插入操作的位置。现在,我也要发送question_id

2 个答案:

答案 0 :(得分:1)

在表单中创建隐藏的输入文件

<input type="hidden" name="question_id" value="<?php echo $row2['question_id']; ?>">

答案 1 :(得分:0)

Create a hidden input field in your form.

<input type="hidden" id="question_id" name="question_id" value="<?= $row2['question_id'] ?>">

PHP solution:
In this case your question_id will be included in the POST.
You can then access it by calling $_POST['question_id'].

Jquery solution:
In Jquery you can also access it by:

$("#question_id").val();