douchi0638 2016-08-04 15:47
浏览 225

使用php在html中进行数据库连接

I have a HTML page index2.html. In this page, I have a DIV which I am using to call a PHP page. The Php page has DB connection parameters, an SQL to fetch values from the DB. However, when the PHP is called from the HTML, I am getting redirected to the PHP page. All I want is to use this stored procedure to get the data from the database.

HTML Code Snippet

</head>

<body>
    <div id="background">
        <div id="Layer0"><img src="images/Layer0.png"></div>
        <div id="Layer2"><img src="images/Layer2.png"></div>
        <div id="parceldeliveryservic"><img src="images/parceldeliveryservic.png"></div>
        <div id="Layer10">
            <form  action="insert4.php" method="post">
            <input type="image" src="images/Layer10.png"/>
            </form>
        </div>

The PHP Code Snippet:

<?php    include("connect.php");
//$q = intval($_GET['q']);
try {

    $proc_rate ='rtPreston';
    $proc_price = 0.0;
    $conn = new PDO("mysql:host=$servername;dbname=testdb", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    //$sql = "GetPrice(?, ?)";
    $sql = "Call GetPrice(:input, @output_price)";
    $stmt = $conn->prepare($sql);
    echo $proc_price;
    $stmt->bindParam(':input',$proc_rate, PDO::PARAM_INT);
    $stmt->execute();
    $stmt->closeCursor();
    $proc_price  = $conn->query("SELECT @output_price AS output_price")->fetch(PDO::FETCH_ASSOC);
    if ($proc_price) {          
        echo sprintf('Price for %s is %lf', $proc_rate, $proc_price['output_price']);
    }


    } catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

//$conn = null; ?>

can you please let me know what needs to be done to display the result in the calling HTML page?

Many thanks

  • 写回答

1条回答 默认 最新

  • dsy19890123 2016-08-04 15:53
    关注

    HTML

    <div onclick="callPHP();">Click me!</div>
    

    Javascript

    <script>
    function callPHP() {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                // xmlhttp.responseText contains the result of the PHP
                alert( xmlhttp.responseText );
            }
        };
    
        // Call the PHP
        xmlhttp.open("GET", "insert4.php", true);
        xmlhttp.send();
    }
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog