简体   繁体   English

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

[英]How to sent id from one page to another when click on submit?

I have a database which consists of question_id, question, and options. 我有一个由question_id,问题和选项组成的数据库。 Where I'll display the question and options. 我将在其中显示问题和选项。 When the user clicked on submit I want to store the option they clicked and the question_id. 当用户单击提交时,我要存储他们单击的选项和question_id。 I am able to store the option they clicked but want to know how to store the 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>

Using th below function I am sending the poll_option to other where I kept the inset operation. 使用下面的函数,我将poll_option发送到其他保留插入操作的位置。 Now I want to send the question_id also 现在我也要发送question_id

创建表单中隐藏的输入

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

Create a hidden input field in your form. 在表单中创建一个隐藏的输入字段。

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

PHP solution: PHP解决方案:
In this case your question_id will be included in the POST . 在这种情况下,您的question_id将包含在POST
You can then access it by calling $_POST['question_id'] . 然后,您可以通过调用$_POST['question_id']来访问它。

Jquery solution: jQuery解决方案:
In Jquery you can also access it by: Jquery您还可以通过以下方式访问它:

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

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

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