1 Replies - 148 Views - Last Post: 16 February 2014 - 02:21 PM Rate Topic: ***-- 2 Votes

#1 Ruddlesdin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 16-February 14

javascript odbc php

Posted 16 February 2014 - 02:12 PM

Hi all,
I am working on a HTML5 project to display some database data in a web page. I am not very experienced javascript but I was hoping that I might be able to connect to my Firebird database directly. After searching the web I know realise that I can't do it this way as Javascript is only client side. (Please prove me wrong as I would like to it this way if I can, even though I am aware of SQL injection attacks.) So, some more searching led me to PHP on Apache. I have experience of PHP yet so I must get some tutorials done. I wish to keep all files separate so will have a index.html, paul.css, paul.js, and now paul.php, (and also a paul.fdb).
I have 2 questions really, how do I call a php file from javascript and get it to return a value into a javascript variable? and how do I connect to my Firebird database and execute SQL and return that back into my PHP? As an aside, can I also open a connection to my Firebird database and leave it open and execute SQL on demand without having to make that connection everytime.
I hope someone can help

Here is my index.html -

<!DOCTYPE html>
<!--
QuickDesign Web Portal
-->
<html lang="en">
    <HEAD>
        <meta charset="utf-8" />
        <title>Paul Web Portal</title>
        <link rel="stylesheet" href="Paul.css">
        <script src="Paul.js"></script>
<body>
    <div id="all">
        <div id="main">
            <h1 id="text1">hello Paul</h1>
            <h1 id="text2">hello Paula</h1>
            <button type="button" id="button1">Get Data</button>
            <button type="button" id="button2">Reset</button>
        </div>
    </div>
</body>
</html>



Here is my paul.css -

main,test,button{
    display: block;
}
*{
    margin: 0px;
    padding: 0px; 
}
body,html{
    height: 100%;
    width: 100%;
}
#all
{
    height: 100%;
}
#main{
    display: block;
    -webkit-box-align: stretch;
    height: 70%;
    width:99.2%;
    text-align: center;
    color:white;
    border-bottom: 2px solid grey;
    border-top: 2px solid grey;
    background:url(img/main.jpg);
    background-size:100% 100%;
    background-repeat:no-repeat;
}
#text1{
    text-align: center;
    color: green;
}
#text2{
    text-align: center;
    color: green;
}
#button1{
    float: left;
    width: 100px;
    height: 40px;
    text-align: center;
    color: white;
    background: green;
    margin: 10px;
    padding: 5px;
}
#button2{
    float: left;
    width: 100px;
    height: 40px;
    text-align: center;
    color: white;
    background: green;
    margin: 10px;
    padding: 5px;
}




Here is my paul.js -

function doFirst()
{
    test1=document.getElementById('text1');
    test2=document.getElementById('text2');
    button1=document.getElementById('button1');
    button2=document.getElementById('button2');
    button1.addEventListener('click', getData, false);
    button2.addEventListener('click', reset, false);
}
function reset(){
    test1.innerHTML='Hello Paul';
    test1.style.color='green'; 
    test2.innerHTML='Hello Paula';
    test2.style.color='green'; 
}
function getData(){
    test1.innerHTML='Hi Paul';
    test1.style.color='blue'; 
    connectDB();
}
function connectDB()
{
    $.get("Paul.php");
    return false;
    test2.innerHTML='Hi Paula!';
    test2.style.color='red'; 
}
window.addEventListener('load',doFirst,false);



and here is my FAILED paul.php -

$conn=odbc_connect('qdweb','','');
if (!$conn)
  {exit("Connection Failed: " . $conn);}
$sql="SELECT description FROM tblware where warenr = '10001'";
$rs=odbc_exec($conn,$sql);
if (!$rs)
  {exit("Error in SQL");}
echo "<table><tr>";
echo "<th>Companyname</th>";
echo "<th>Contactname</th></tr>";
while (odbc_fetch_row($rs))
  {
  $compname=odbc_result($rs,"CompanyName");
  $conname=odbc_result($rs,"ContactName");
  echo "<tr><td>$compname</td>";
  echo "<td>$conname</td></tr>";
  }
odbc_close($conn);
echo "</table>";



Is This A Good Question/Topic? 0
  • +

Replies To: javascript odbc php

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: javascript odbc php

Posted 16 February 2014 - 02:21 PM

Quote

After searching the web I know realise that I can't do it this way as Javascript is only client side.

Node.js is a server-side JS technology, but you would need your server to be configured to support it. Otherwise, JS is strictly client-side.

Quote

So, some more searching led me to PHP on Apache. I have experience of PHP yet so I must get some tutorials done. I wish to keep all files separate so will have a index.html, paul.css, paul.js, and now paul.php, (and also a paul.fdb).

This is a good thing. However, your PHP code is poorly indented. It's hard to read and follow.

Quote

I have 2 questions really, how do I call a php file from javascript and get it to return a value into a javascript variable?

Don't think of it as "calling a PHP file from javascript." The way this works is that the client makes a request and sends it to the server. The server then processes it (this is your PHP code), and sends the result back to the client, which then displays it.

Quote

how do I connect to my Firebird database and execute SQL and return that back into my PHP? As an aside

http://www.php.net/m...do-firebird.php
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1