hassan ali
hassan ali

Reputation: 11

how to get specific data when i press a button

i have created a search page that gives results based on the entered values.What i want is to get specific details of the ad when the user presses the enroll button. But i am getting the details of all the ads instead of the one the user wants to enroll into. here is the code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="css/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="css/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="css/basic.css">
    <script src="js/jQuery/jquery.min.js"></script>
    <script src="css/bootstrap/js/bootstrap.min.js"></script>

    <script src="https://cdn.jsdelivr.net/jquery.validation/1.15.1/jquery.validate.min.js"></script>
    <script src="Forms/form-validation.js"></script>
</head>
<body>
<?php 
session_start();
require 'connection.php';
//include_once 'utlities.php';

$userEmail=$_SESSION['user'];
$form_string = 'd';

$sqlQuery ='SELECT * FROM `course` WHERE `course_title` LIKE "%'.$form_string.'%" OR `user_teacher` in (SELECT user_teacher FROM teacher where email in (SELECT email from person where fname like "%'.$form_string.'%" OR lname like "%'.$form_string.'%"));';
$queryExe=mysqli_query($connection,$sqlQuery);
$row = mysqli_fetch_assoc($queryExe)
 ?>
<div class="panel-body">
                    <?php while($row = mysqli_fetch_assoc($queryExe)) { ?>
                    <article class="row panel panel-default">

                        <div class="col-xs-6 col-xs-offset-3  col-sm-3 col-sm-offset-0">
                            <a href="#" title="Lorem ipsum" class="thumbnail"><img src="images/profile-default.jpg" alt="Lorem ipsum" /></a>
                        </div>
                        <div class="col-xs-12 col-sm-3">
                            <ul class="list-group">
                                <li class="list-group-item">
                                    <i class="glyphicon glyphicon-book"></i>
                                    <span>
                                        <?php
                                            echo " ".$row["description"];
                                        ?>
                                    </span>
                                    </li>
                                    <li class="list-group-item">
                                    <i class="glyphicon glyphicon-tags"></i>
                                    <span id="courseid" >
                                        <?php
                                            echo " Course ID ".$row["courseid"];
                                        ?>
                                    </span>
                                    </li>
                                <li class="list-group-item">
                                    <i class="glyphicon glyphicon-tags"></i>
                                    <span>
                                        <?php echo" Rs. ".$row["fees"];
                                        ?>
                                    </span>
                                </li>
                            </ul>
                        </div>
                        <div class="col-xs-12 col-sm-6">
                            <h3>
                                <a href="#" title="">
                                    <?php 
                                        $sql_profile = 
                                            "SELECT `fname`,`lname`,`email`
                                                FROM `person` 
                                                WHERE `email`=(
                                                    SELECT `email` 
                                                    FROM `teacher` 
                                                    WHERE `user_teacher`=".$row["user_teacher"]."
                                                    );   
                                                ";

                                        $result_sql=mysqli_query($connection,$sql_profile);
                                        $tName = mysqli_fetch_assoc($result_sql);
                                        echo $tName["fname"]." ".$tName["lname"];
                                        $_SESSION['temail']= $tName["email"];
                                    ?>
                                </a>
                            </h3>
                            <h6><?php echo $tName['email']; ?></h6>
                            <p class="hidden-xs"><?php echo $row["course_title"]; ?></p>
                            <span class="plus">
                                <a href="#" title="Enroll" class="btn btn-success" 
                                  <?php
                                    /*if(loggedIn()){

                                         echo 'data-toggle="modal" data-target="#enroll "';

                                    }else{

                                        echo 'data-toggle="modal" data-target="#LOGIN "';
                                    }
                                    /**/
                                  ?>>
                                  <i class="glyphicon glyphicon-plus" id="enroll" ></i><span>Enroll</span>
                                </a>
                            </span>
                        </div>          
                    </article>
                    <?php 
                     }
                    ?>
                </div>


<script type="text/javascript">
    $(document).ready(function() {
        $('#enroll').click(function() {
            alert($("article").find("#courseid,h6").text());
        });
    });
</script>

</body>
</html>

Upvotes: 1

Views: 100

Answers (2)

Satpal
Satpal

Reputation: 133403

Identifiers in HTML must be unique You are generating the element in a loop, which will create duplicate ID hence the generated HTML will be invalid.

Use CSS class with elements then attach event handlers using it. You can associate the arbitrary data using data-* custom attribute which can be fetched using .data().

HTML

<i class="glyphicon glyphicon-plus enroll" data-email="<?php echo $tName['email']; ?>" data-courseid="<?php echo $row['courseid']; ?>"></i><span>Enroll</span>

SCRIPT

$('.enroll').click(function() {
    var email = $(this).data('email');
    var courseid = $(this).data('courseid');            
});

Upvotes: 2

john Smith
john Smith

Reputation: 17906

set unique identifier to what you want to show

    <?php
        echo "<span id=\"courseid-".$row['courseid']." \" >";
        echo " Course ID ".$row["courseid"];
    ?>

and to the enroll button

    <?php
        echo "<i class=\"glyphicon glyphicon-plus\" id=\"enroll\" data-id=\"courseid-".$row["courseid"]." \">";

    ?>

and in your script you show the specific text

    $('#enroll').click(function() {
        courseid = $(this).attr('data-id');
        alert($("#"+courseid+",h6").text());
    });

Upvotes: 1

Related Questions