最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - link to a pop up view via a php query - Stack Overflow

programmeradmin1浏览0评论

Hi I have a search function in my index.php page. When a user types in a field and enters 'search' it goes to jobsearch.php and prints out the job summary from a mysqli database.

I've created links that go on the end of the results. So when the user sees the job summary the user clicks on the link (click here) then it directs the user to a pop up view which shows more information (job _description) about the job. Is there a way of doing this? I don't want to have to create a html page for every job as there could be 100s of jobs in the database.

I would like the click-here link to take the ID of the desired job and print out the job description on a pop up page.

It looks like this:

this is jobs.php which prints out a list of jobs..

<?php
$servername = "*****";
$username = "root";
$password = "*****";
$dbname = "jobslist";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, job_title, job_description, job_location, job_category  FROM jobs_list";
$result = $conn->query($sql);

//display table
echo "<table border='1'>
<!--<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Location</th>
<th>Category</th>
</tr>-->";

if ($result->num_rows > 0) {
     // output data of each row

     while($row = $result->fetch_assoc()) {

 echo "<tr>";
       echo "<td min-height:'200' ><h2>". $row["id"] ."</h2></td>";
       echo "<td>". $row["job_title"] . "</td>";
       echo "<td min-width:'700' >". $row["job_description"] . "</td> " ;
       echo "<td>". $row["job_location"] . "</td>";
       echo "<td>". $row["job_category"] . "</td> " ;

//this prints out a clickable link for every job

echo "<td><a href='" . $row['id'] .  "'>Click Here</a></td>";



       echo "</tr>";
    }

}
 else {
     echo "0 results";
}

echo "</table>";
$conn->close();
?>

Hi I have a search function in my index.php page. When a user types in a field and enters 'search' it goes to jobsearch.php and prints out the job summary from a mysqli database.

I've created links that go on the end of the results. So when the user sees the job summary the user clicks on the link (click here) then it directs the user to a pop up view which shows more information (job _description) about the job. Is there a way of doing this? I don't want to have to create a html page for every job as there could be 100s of jobs in the database.

I would like the click-here link to take the ID of the desired job and print out the job description on a pop up page.

It looks like this:

this is jobs.php which prints out a list of jobs..

<?php
$servername = "*****";
$username = "root";
$password = "*****";
$dbname = "jobslist";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
     die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, job_title, job_description, job_location, job_category  FROM jobs_list";
$result = $conn->query($sql);

//display table
echo "<table border='1'>
<!--<tr>
<th>ID</th>
<th>Title</th>
<th>Description</th>
<th>Location</th>
<th>Category</th>
</tr>-->";

if ($result->num_rows > 0) {
     // output data of each row

     while($row = $result->fetch_assoc()) {

 echo "<tr>";
       echo "<td min-height:'200' ><h2>". $row["id"] ."</h2></td>";
       echo "<td>". $row["job_title"] . "</td>";
       echo "<td min-width:'700' >". $row["job_description"] . "</td> " ;
       echo "<td>". $row["job_location"] . "</td>";
       echo "<td>". $row["job_category"] . "</td> " ;

//this prints out a clickable link for every job

echo "<td><a href='" . $row['id'] .  "'>Click Here</a></td>";



       echo "</tr>";
    }

}
 else {
     echo "0 results";
}

echo "</table>";
$conn->close();
?>
Share Improve this question edited Mar 23, 2015 at 14:59 Superunknown asked Mar 19, 2015 at 10:11 SuperunknownSuperunknown 5113 gold badges10 silver badges30 bronze badges 3
  • 2 For this you can use 2 options. First is to create a page in php that takes one parameter and display all the job data in it, and then just call that page. The second option is to use jquery to make ajax request and get all the details you need and then just display them in your pop up window – Igor Ilic Commented Mar 20, 2015 at 6:58
  • What is $row['course_url'] ? Can you identify an unique job with it ? – Benjamin Poignant Commented Mar 23, 2015 at 14:55
  • Apologies...changed it to "id" – Superunknown Commented Mar 23, 2015 at 15:00
Add a ment  | 

5 Answers 5

Reset to default 3 +50

Change

//this prints out a clickable link for every job

echo "<td><a href='" . $row['id'] .  "'>Click Here</a></td>";

To

echo "<td><a target='jobdescriptionwindow' href='jobdescription.php?id=" . $row['id'] .  "'>Click Here</a></td>";

This will open a new window (called 'jobdescriptionwindow') when you click on the link passing the ID of the job clicked through to jobdescription.php. You will also have to make sure that your browser isn't stopping this window from opening.

Create a new PHP document called jobdescription.php with these contents:

<?php

if (isset($_GET['id'])) { // Check ID is is present in parameters

    $servername = "*****"; // <-- Enter your details
    $username = "root";
    $password = "*****"; // <-- Enter your details
    $dbname = "jobslist";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // I do: (int)$_GET['id'] to type cast the value of $_GET['id'] to protect against injection. http://php/manual/en/language.types.type-juggling.php

    $sql = "SELECT id, job_title, job_description, job_location, job_category FROM jobs_list WHERE id = ".(int)$_GET['id'];

    $result = $conn->query($sql); // <-- Added new line
    $row = $result->fetch_assoc();


    echo $row['job_title'] . '<br />';
    echo $row['job_description']; //<-- Does now show your description?

    var_dump($result); // Lay out this data as required
    // Outputs object(mysqli_result)#2 (5) { ["current_field"]=> int(0) ["field_count"]=> int(5) ["lengths"]=> array(5) { [0]=> int(1) [1]=> int(23) [2]=> int(102) [3]=> int(4) [4]=> int(2) } ["num_rows"]=> int(1) ["type"]=> int(0) } 



} else {

  echo "No ID provided";

}

This will get the details for the job ID sent in the GET parameters.

You do not need to generate a page for every job. You could generate one php that gets a job id as a GET parameter (getJob.php?id=5), fetches the data from data base on that id and returns the data as HTML page.

In addition to beingalex's code, you could also use fancybox to load content in the popup via ajax instead of a new window. See this page: http://fancyapps./fancybox/

Take a look at examples specifically ajax. Search for 'ajax' on this page.This is what you need exactly.

jobs.php href-->

echo "<td><a href='something.php?id=".$row['id']. "'>Click Here</a></td>";

Something.php:

if(isset($GET_['id'])){
  $getid = $_GET['id'];

  $sql = "SELECT job_title, job_description, job_location, job_category  FROM jobs_list WHERE id = '".$getid."'";

  $result = $conn->query(sql);

  while($row = $result->fetch_assoc()){

     echo $row['job_title'].'<br />';
     echo $row['job_description'].'<br />;
     echo $row['job_location'].'<br />;
     echo $row['job_category'];
  }
}

But this code is attackable with SQL Injection! You need to escape the $_GET param, or you need to learn / use PDO.

Include jQuery,Bootstrap and show information on modal popup. The modal windows opens on job title link click.

 <?php
    $servername = "*****";
    $username = "root";
    $password = "*****";
    $dbname = "jobslist";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
         die("Connection failed: " . $conn->connect_error);
    }
    $sql = "SELECT id, job_title, job_description, job_location, job_category  FROM jobs_list";
    $result = $conn->query($sql);
    $tbdata = "";
    $modals = "";
    if ($result->num_rows > 0) {
    // display table if results exist
    echo "<table class=\"table table-striped\"><tr><th>Job Title</th><th>Location</th><th>Category</th></tr>";

     while($row = $result->fetch_assoc()) {
     $tbdata.="<tr><td><a href=\"#\" data-toggle=\"modal\" data-target=\"#jbinfo_". $row["id"] . "\">". $row["job_title"] . "</a></td><td>". $row["job_location"] . "</td><td>". $row["job_category"] . "</td></tr>";
     $modals.="<div class=\"modal fade\" id=\"#jbinfo_". $row["id"] . "\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"jbLabel". $row["id"] . "\" aria-hidden=\"true\"><div class=\"modal-dialog\"><div class=\"modal-content\"><div class=\"modal-header\"><h4 class=\"modal-title\" id=\"jbLabel". $row["id"] . "\">". $row["job_title"] . "</h4></div><div class=\"modal-body\">". $row["job_description"] . "</div><div class=\"modal-footer\"><button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button></div></div></div></div>";
     }
    echo $tbdata;
    echo "</table>";
    echo $modals;
    } else {
         echo "No results";
    }

    $conn->close();
    ?>  

Before your closing body tag place the following:

<!-- Latest piled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.4/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn./bootstrap/3.3.4/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<!-- Latest piled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn./bootstrap/3.3.4/js/bootstrap.min.js"></script>
发布评论

评论列表(0)

  1. 暂无评论