2016-04-26 38 views
0

试图将变量传递到HTML模式,运行查询并显示基于ID的信息...我可以将此参数传递到HTML中,但我需要它在查询结束时的SQL WHERE语句...将变量传入模态和查询数据库

我的脚本就在这里。我想我需要一些Ajax来完成这一点。我创建了一个小提琴

http://jsfiddle.net/rbla/y7s5tf3p/

<script> 

$(document).on("click", ".open-AddBookDialog", function() { 
var myBookId = $(this).data('id'); 
//$(".modal-body #bookId").val(myBookId); 
document.getElementById("bookId").innerHTML = myBookId; 

}); 

</script> 

的变量被插入到HTML,但我需要获取数据-ID到一个PHP变量,而不是HTML,这样我可以运行一个查询...

这里是PHP文件,我有

<?php 

$id = (this is where the data-id needs to be); 

$sql = oci_parse($conn, select * from movies m where id=:id"); 

oci_bind_by_name($sql, ':id', $id); 

oci_execute($sql, OCI_DEFAULT); 

$objResult = oci_fetch_array($sql); 
?> 
+0

您使用的自举2或3? – gibberish

回答

0

是的,你需要AJAX做你想要什么。

注意$(this).data('id')会得到一个标签属性的值类似于:

<input type="text" data-id="mother" id="17" /> 

它会抢值mother,不17

这下面的代码示例可能是类似的,你需要建立什么:

使用Javascript/jQuery的:

$(document).on("click", ".openSlickModal-1", function() { 
    var myMovieId = $(this).data('id'); 
    $.ajax({ 
     type: 'post', 
     url: 'my_ajax_processor.php', 
     data: 'MovieID=' +myMovieId, 
     success: function(d){ 
      alert(d); ////Just for testing: disable after receiving the correct response 
      $("#movie").html(d); 
     } 
    }); 

}); 

my_ajax_processor.php:

<?php 
    $id = $_POST['MovieID']; 
    die('Received: ' .$id); //Just a test: disable this after receiving the correct response 

    //do your query using the $id 
    $out = 'Whatever you create (HTML or plain text) after your query 
    echo $out; 
?> 

个其他参考:

dynamic drop down box?

Prevent Page Load on Jquery Form Submit with None Display Button

+0

我用它来打开模式的链接 – Ronald

+0

VIEW MOVIE
Ronald

+0

噢,好吧。有点不寻常,但完全可以接受。你可以使用'id ='或'data-id =',但'id ='更常见。上面的代码是否能让你知道如何继续? *如果您还有其他问题,请留下另一条评论。* – gibberish