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 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧