Мне нужно вызвать другие данные из моей базы данных, но мой var для id находится в javascript, есть ли способ для меня это сделать? или я должен попробовать использовать другой метод?
Мой php
<?php
$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('ts_php');
$query = "SELECT * FROM job_posted"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table class='table'>
<thead>
<th>JOB</th>
<th>STATUS</th>
<th>APPLICATIONS</th>
<th>EDIT</th>
<th>DELETE</th>
</thead>
"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<tr>
<th>" . $row['job_title'] . "</th>
<td>" . $row['status'] . "</td>
<td>" . $row['applications'] . "</td>
<td><a class='openModal' data-id='".$row['post_ID']."' >edit</a></td>
<td><a href='#'>delete</a></td>
</tr>";
}
echo "</table>"; //Close the table in HTML
?>
И мой сценарий
<script>
$(".openModal").click(function(){
var job_posted_id = $(this).data('id');
$(".modal-body").html("This is your post id " + job_posted_id + "<br> This is where i want all my data");
$(".modal").modal("show");
});
</script>
Вы можете использовать ajax:
$(".openModal").click(function(){
var job_posted_id = $(this).data('id');
$.get('your_function.php', {'job_posted_id':job_posted_id}, function(data) {
$(".modal-body").html(data);
$(".modal").modal("show");
}, 'json');
});
Ваш php:
if (isset($_GET['job_posted_id']) {
$connection = mysql_connect('localhost', 'root', ''); //The Blank string is the password
mysql_select_db('ts_php');
$query = "SELECT * FROM job_posted WHERE `job_posted_id` = ". $_GET['job_posted_id']; //You don't need a ; like you do in SQL
$result = mysql_query($query);
$str = "<table class='table'>
<thead>
<th>JOB</th>
<th>STATUS</th>
<th>APPLICATIONS</th>
<th>EDIT</th>
<th>DELETE</th>
</thead>
"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
$str .= "<tr>
<th>" . $row['job_title'] . "</th>
<td>" . $row['status'] . "</td>
<td>" . $row['applications'] . "</td>
<td><a class='openModal' data-id='".$row['post_ID']."' >edit</a></td>
<td><a href='#'>delete</a></td>
</tr>";
}
$str .= "</table>"; //Close the table in HTML
echo $str;
}
Других решений пока нет …