【问题标题】:How to send values of Select button from Mysqli database and send to second pages?如何从 Mysqli 数据库发送 Select 按钮的值并发送到第二页?
【发布时间】:2020-06-28 12:00:58
【问题描述】:

我尝试对其进行编码。我仍然陷入困境。主要目标是如果用户从 mysqli 数据库中选择值选择它并将值发送到其他页面。我知道人们推荐 AJAX 使用它。我尝试使用它。还是行不通。我会在下面放详细代码。

主页代码(main.php)-

<?php
    session_start();

    $conn=mysqli_connect('localhost','root','','user');

    if(!$conn){
        die('Please check an Connection.'.mysqli_error());
    }

$resultset=$conn->query("SELECT name from newtable"); ?>

<!DOCTYPE html>
<head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
</head>
<body>
        <center>
            Select DataBase to Insert it<select name="tables" id="tables">
                <?php
                    while($rows=$resultset->fetch_assoc()){
                        echo'<option value='.$rows['name'].'>'.$rows['name'].'</option>';                            
                    }
                ?>
            </select>
            <a href="database1.php">click </a>
        </center>


    <script type="text/javascript">

    $(document).ready(function(){
    var search='';
    $("#tables option:selected").each(function() {
        if ($(this).attr('value') !== '') {
            search=$(this).attr('value');
        }
    });
    $("a").click(function() {
        $.ajax({
            method: 'post',
            url: 'database1.php',
            data: {key:search},
            beforeSend: function() {

                $('body').css("opacity", "0.3");
            },
            success: function(response) {
                 alert(response);
            },
            complete: function() {
                $('body').css("opacity", "1");
            }
        });
    });
});
     
    </script>   

</body>
</html>

作为警告框,我正在获取它的值,但第二页出现键值不存在的错误。这里是第二页(database1.php)-

<?php

    $conn=mysqli_connect('localhost','root','','user');

    session_start();

    if(!$conn){
        die('Please check an Connection.'.mysqli_error());
    }

            $database=$_POST['key'];
            echo'You Selected'.$database.'from table';
                $sql = "SELECT * FROM $database";
                $result=mysqli_query($conn,$sql);
                if($result){ 
                    echo'Worked';
                }else{
                    echo'ERROR!';
                }
?>

那么问题出在哪里?

更新答案

感谢@swati,她提到使用表单标签而不是 AJAX(我知道它的简单答案),顺便感谢您的回答。 :)

更新的代码已满 -

    <body>
    <form action="database1.php" method="GET">
    <center>
            Select DataBase to Insert it<select name="tables" id="tables">
                <?php
                    while($rows=$resultset->fetch_assoc()){
                        echo'<option 
              value='.$rows['name'].'>'.$rows['name'].'</option>';                            
                    }
                ?>
            </select>
         <input type="submit">
        </center>
</form>


</body>

第二页(database1.php)变化不大 -

$database=$_GET['tables'];

【问题讨论】:

  • 你有一个错误。 mysqli_error() 需要一个参数。请考虑打开错误模式。 How to get the error message in MySQLi?
  • @Dharman 它不需要关心数据库。一切都完美的mysqli。效果很好。
  • 我知道它对你很有效,但你看不到错误,这正是我想警告你这个问题的原因。

标签: php html ajax mysqli


【解决方案1】:

您在页面加载时调用每个loop,这将为您提供已选择的值,而不是用户选择的值。此外,不需要此循环,因为您只需传递一个值。

您的script 应如下所示:

<script type="text/javascript">
  $(document).ready(function() {
  //no need to add loop here
    var search = '';
    $("a").click(function() {
     search = $("#tables option:selected").val(); //getting selected value of select-box
      $.ajax({
        method: 'post',
        url: 'database1.php',
        data: {
          key: search
        },
        beforeSend: function() {

          $('body').css("opacity", "0.3");
        },
        success: function(response) {
          alert(response);
        },
        complete: function() {
          $('body').css("opacity", "1");
        }
      });
    });
  });
</script>

此外,由于您使用的是 ajax,因此无需将 href="database1.php" 提供给 a 标记,因为您正在使用 ajax 调用此页面。即:您的 a 标记应如下所示:

 <a>click</a>

无论您在 php 端将 echo 做什么,都将作为对您的 ajax 的响应返回。因此,您的 alert 内部成功函数将向您显示该值。

【讨论】:

  • 您好,感谢您的回答。至于你说a标签不应该href?那是行不通的。我的意思是如果没有href,那么我不会去第二页(database1.php)只有他们给出响应警报。
  • 因为通过 ajax 调用,您已经进入该页面,即:url: 'database1.php',为什么还要去?
  • 我试图删除你提到的。它仍然无法正常工作。只给出响应提醒
  • 什么没用?你能详细说明一下吗?是的,它会发出警报,因为您在 ajax 中的成功函数有 alert(response) ?对不起,我很困惑
  • 当你使用ajax时,你需要返回响应,然后你可以重定向到其他页面,所以如果你需要去那个页面你可以在你的成功函数下写window.location.href = 'database1.php';,但$_POST 值将不可用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-16
  • 2019-11-26
  • 2020-01-19
  • 2022-11-30
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
相关资源
最近更新 更多