-2

I tried to coding it. I am still getting stuck over it. The main goal was if user select value from mysqli database selected it and send the values to other pages. I know people recommend it use by AJAX. I tried to use it. still not working. I'll put details code below.

Main pages Code(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>

as alert box i am getting value of it but second pages get error that key value doesn't exist. here the second one pages (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!';
                }
?>

so what the problem occurred?

UPDATED ANSWER

Thanks to @swati which she mentioned that use form tag instead of AJAX (i know its simple answer) still by the way thanks for answer. :)

UPDATED CODE FULL -

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

SECOND PAGE(database1.php) CHANGES LITTLE -

$database=$_GET['tables'];
  • You have an error. [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Jun 28 '20 at 12:59
  • @Dharman Its doesn't need care about database. all things works perfect mysqli. It's worked perfectly. – Niraj Deshmukh Jun 28 '20 at 13:04
  • 2
    I know it works fine for you and you don't see the error, which is exactly why I wanted to warn you about the problem. – Dharman Jun 28 '20 at 13:05

1 Answers1

0

You are calling each loop on page load that will give you the already selected value not the value which is selected by user.Also , this loop is not need as you have to pass only one value .

Your script should look like below :

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

Also , as you are using ajax no need to give href="database1.php" to a tag because you are calling this page using ajax .i.e: Your a tag should be like below :

 <a>click</a>

And whatever you will echo in php side will be return as response to your ajax .So , your alert inside success function will show you that value.

Swati
  • 28,069
  • 4
  • 21
  • 41