hasib Hasan
hasib Hasan

Reputation: 37

How to get value from html select option using GET

I am creating a student login site for tutoring center. After a student logged in tutor can see student information and tutor also can submit student information along with tutor name. My problem is whatever i select the tutor name it always insert the last tutor name. I am using PDO for database connection. please can anybody give me an idea how can i solve this problem.Thanks..

Here is my html form code:

<form action="" method="post">
    <table border="1">
        <tr>
            <th>Student Name</th>
            <th>course</th>
            <th>Tutor</th>
            <th>Actions</th>
        </tr>

        <?php
        //select all users from database
        $rows = $database->selectFromOnline();

        foreach($rows as $row){
            $time = $row['time_out'];
            ?>
            <tr>
                <td> <?php echo $row['user_name'] ?></td>
                <td> <?php echo $row['course'] ?> </td>
                <td>
                    <select name='tutor'>
                        <?php
                            //select all instructor from instructor table
                            $sqls = $database->selectFromTutor();
                            foreach($sqls as $sql){
                                echo"<option value='$sql[tutor_ln]'> $sql[tutor_ln] </option>";
                            }
                        ?>
                    </select>
                </td>

                <?php
                     echo" <td> <a href='tutor.php?user_name=$row[user_name]&&course=$row[course]&&tutor=$sql[tutor_ln]'>Delete</a></td>";
                ?>
            </tr>
       <?php
        }
        ?>

    </table>
</form>

This is my inserting code:

if(isset($_GET['user_name'])){

    $user_name = $_GET['user_name'];

    $course = $_GET['course'];

    // if i use $_POST['tutor'] it gives me undefined variable error
    $tutor = $_GET['tutor'];

    //insert into report database table
    $database->insetIntoReport($user_name, $course, $tutor);

    //Redirect to current page
    header('Location: tutor.php');
    exit;
}
?>

Upvotes: 0

Views: 463

Answers (5)

Manoj Purohit
Manoj Purohit

Reputation: 3453

I guess you are not willing to use post then you should write a javascript function like

<script type="text/javascript">
    function DeleteTutor(name,course){
    var tutor=document.getElementById('tutor').value;
    window.location.href = 'tutor.php?username='+name+'&course='+course+'&tutor='+tutor;
    }
</script> 

capture name and course in variables $name and $course and your delete link modified to

<?php echo"<td> <a onclick=\"DeleteTutor('$name', '$course')\">Delete</a></td>"; ?>

Don't forget to add id to select for this to work

<select name='tutor' id="tutor">

Upvotes: 0

feeela
feeela

Reputation: 29932

In addition to the other answers, you could also use $_REQUEST if do not want to bother, whether a form was sent via POST or GET.

Upvotes: 1

Marc B
Marc B

Reputation: 360562

You're outputting multiple <select name='tutor'> in your form. One select for EVERY time that inner ->selectFromTutor() is executed. As such, you'll get MULTIPLE instances of the tutor select, and only the LAST one will be submitted with the rest of the form.

Upvotes: 2

JunM
JunM

Reputation: 7150

If you are using method="post" on your form, you should use $_POST[]

Upvotes: 1

DevZer0
DevZer0

Reputation: 13525

Your form says <form action="" method="post"> you should be using $_POST variables in your code.

Upvotes: 1

Related Questions