【问题标题】:passing variable into Modal and Query Database将变量传递到模态和查询数据库
【发布时间】:2016-04-26 21:13:01
【问题描述】:

尝试将变量传递到 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 中,但我需要将 data-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);
?>

【问题讨论】:

  • 您使用的是引导程序 2 还是引导程序 3?

标签: jquery ajax modal-dialog bootstrap-modal simplemodal


【解决方案1】:

是的,你需要 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

【讨论】:

  • 我将它用于打开模式的链接
  • 观看电影
  • 哦,那好吧。只是有点不寻常,但完全可以接受。您可以使用id=data-id=,但id= 更常见。上面的代码是否让您知道如何进行? 如果您还有其他问题,请发表评论。
  • 我刚刚更新了我的整个问题,添加了一些 php 并创建了一个 jsfiddle。我试过你说的没有成功。如果您能提供帮助 - 我将不胜感激...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-23
  • 1970-01-01
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 2021-01-28
相关资源
最近更新 更多