Passing a value using Ajax to an exe then back

For everything that's not in any way related to PureBasic. General chat etc...
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 279
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Passing a value using Ajax to an exe then back

Post by DeanH »

I am stumped and I am sure this should be simple for someone who knows about Ajax.

I am trying to pick up a value entered by a user on a basic webpage, pass it to an exe (written in PureBasic) and just have the value passed back to the page where it is displayed. I can do this using CGI with a form statement but I do not want a new page generated.
I have a very simple webpage called 'test.htm'.
I think the problem is with the html page. When I get this to work I then want to have the exe call a Sqlite database, do a query, then return data that can be placed at a spot on the page. But for now this is just a test.

Thanks to anyone who can straight me out.

The CGI program is testvalue.pb compiled to testvalue.exe.

Here is the testvalue.pb code:

Code: Select all

;compile to testvalue.exe
If Not InitCGI() Or Not ReadCGI()
	End
EndIf
Content$="?"+CGIVariable(#PB_CGI_QueryString)
Value$=GetURLPart(Content$,"itemnumber")
a$=Content$+"<br>"+"<br>"+Value$+"<br>"      ;this is what is to be passed back to the calling page
WriteCGIHeader(#PB_CGI_HeaderContentType,"text/html",#PB_CGI_LastHeader)
WriteCGIString(a$)
And here is the html code for the test.htm page:

Code: Select all

<html>
<body>

<label for="fname">Item number:</label>
<input type="text" id="fname" name="itemnumber"><br><br>
<button type="Submit" onclick="loadDoc(this.value,myFunction)">Try it</button>

<br>
<br>
<p id="demo">
</p>

<script>
function loadDoc(val,cFunction) {
  var xhttp;
  if (str=="") {
    document.getElementById("fname").innerHTML="";
    return;
  }
  xhttp=new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      cFunction(this);
    }
  };
  var a=="http:testvalue.exe?"+document.getElementById("fname")+"="+val
  xhttp.open("GET", a, true);
  xhttp.send();
}
function myFunction(xhttp) {
  document.getElementById("demo").innerHTML =
  xhttp.responseText;
}
</script>

</body>
</html>

User avatar
Kiffi
Addict
Addict
Posts: 1509
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Passing a value using Ajax to an exe then back

Post by Kiffi »

this one seems wrong:
DeanH wrote:

Code: Select all

var a=="http:testvalue.exe?"+document.getElementById("fname")+"="+val
Hygge
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 279
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: Passing a value using Ajax to an exe then back

Post by DeanH »

I suspect it isn't right but I can't seem to spot how to correct it. Brainfog or something. I am only just learning Ajax and anything beyond simple html.
User avatar
DeanH
Enthusiast
Enthusiast
Posts: 279
Joined: Wed May 07, 2008 4:57 am
Location: Adelaide, South Australia
Contact:

Re: Passing a value using Ajax to an exe then back

Post by DeanH »

Brainfog lifted!
The CGI program is fine. Below is the html that works.

Code: Select all

<html>
<body>
<input type="text" id="fname" name="item"><br><br>
<button type="button" onclick="loadDoc(document.getElementById('fname').value,'testvalue.exe?item=', myFunction)">Try it</button>
<br>
<br>
<p id="demo">
</p>

<script>
function loadDoc(val, url, cFunction) {
  var xhttp;
  xhttp=new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
      cFunction(this);
    }
  };
  url=url+val;
  xhttp.open("GET", url, true);
  xhttp.send();
}
function myFunction(xhttp) {
  document.getElementById("demo").innerHTML =
  xhttp.responseText;
}
</script>
</body>
</html>
Post Reply