AJAX function doesn't fire from button input form

J

jr

Is "onsubmit" a valid event for an object of type submit? I thought it
was aformevent.

I'm not sure whether you want to submit data from thisformto the php
page that triggers the print operation or not. Clarification of that
might help. If you do, then to use anajaxrequest, you will have to use
javascript to assemble a full post or get request from theformdata
when thebuttonis clicked. It may be better to use a traditionalform
submit and generate a new page with the printer response message on the
server.

If the data is already on the server, you don't need to use aformfor
this, although how does the server know which data to use? Session
cookies? Job id from theform(which you might need to append to the get
request using ?field=value)? In either case, you probably shouldn't be
triggeringajaxrequests from aform'ssubmitbuttonusing an onsubmit
handler.

You could use theform'sonsubmit handler to specify javascript to
execute when the submitbuttonis pressed. Perhaps that's what you want
to do? If you do that, and don't want to execute theform'saction, make
sure your javascript returns false.

Your submitbuttonis probably trying to call it's parentform'saction,
unless your "onsubmit='blah'" is confusing it completely.

Perhaps you should use a normalbutton(rather than a submit) and give
it a value of, for example, "Send to Printer" and, perhaps, use an
appropriate event handler to call the javascript code.

If I had a better understanding of what you were attempting, I might be
able to give better and / or less confusing pointers to possible solutions.

The following javascript and html, if you understand what it shows,
might be useful to you, or it might not. If you don't understand it, but
instead just blindly copy parts, or all, of it, and things still don't
work, well you obviously didn't take the time to try and understand it
first!

<script type="text/javascript">
function clanger() {
  // visit the soupdragon
  var soupdragon;
  if (window.XMLHttpRequest) { // ie7+ etc
    soupdragon=new XMLHttpRequest();
  }else{ // IE6, IE5
    soupdragon=new ActiveXObject("Microsoft.XMLHTTP");
  }
  soupdragon.onreadystatechange = function()
  {
    if(soupdragon.readyState == 4)
    {
      if(soupdragon.status == 200)
        document.getElementById('printer_response').innerHTML=
          "Received: " + soupdragon.responseText;
      else
        document.getElementById('printer_response').innerHTML=
          "Error code: " + soupdragon.status;
    }
  }
  soupdragon.open(GET, "print_test.php", true);
  soupdragon.send();
  return false;}

</script>

<formaction="blah" method="whatever" onsubmit="clanger()">
..... fields .....
<input type="submit">
</form>

Rgds

Denis McMahon
Is "onsubmit" a valid event for an object of type submit? I thought it
was aformevent.

Sorry, this is not a form. This is only a button. The event should
be onClick.
I ran the page through the validator and fixed the tables. This
button is not part of the form.
I'm not sure whether you want to submit data from thisformto the php
page that triggers the print operation or not. Clarification of that
might help. If you do, then to use anajaxrequest, you will have to use
javascript to assemble a full post or get request from theformdata
when thebuttonis clicked. It may be better to use a traditionalform
submit and generate a new page with the printer response message on the
server.

If the data is already on the server, you don't need to use aformfor
this, although how does the server know which data to use? Session
cookies? Job id from theform(which you might need to append to the get
request using ?field=value)? In either case, you probably shouldn't be
triggeringajaxrequests from aform'ssubmitbuttonusing an onsubmit
handler.
Sorry for not explaining it. The search form pulls barcode labels.
The printer is an FTP printer. There is no live connection. The
script opens
a file and writes the labels to a template. The template gets dropped
on the printer.
The limit is 200 per page. So the button just sends the template to
the printer per page.
You could use theform'sonsubmit handler to specify javascript to
execute when the submitbuttonis pressed. Perhaps that's what you want
to do? If you do that, and don't want to execute theform'saction, make
sure your javascript returns false.

Your submitbuttonis probably trying to call it's parentform'saction,
unless your "onsubmit='blah'" is confusing it completely.

Perhaps you should use a normalbutton(rather than a submit) and give
it a value of, for example, "Send to Printer" and, perhaps, use an
appropriate event handler to call the javascript code.

I think it is a normal button.
If I had a better understanding of what you were attempting, I might be
able to give better and / or less confusing pointers to possible solutions.

The following javascript and html, if you understand what it shows,
might be useful to you, or it might not. If you don't understand it, but
instead just blindly copy parts, or all, of it, and things still don't
work, well you obviously didn't take the time to try and understand it
first!
I would like to get this AJAX to work first and then the other one.
It still isn't firing. I ran it through the Validator.
I'm still not getting the javascript to fire.
There is some info about the FTP connection on the print_test.php
page.
The var is $response. I would like $response to be printed between
the div. I just echoed the var $response on the print template page.
----code from print_test.php------
if(!upload) {
echo "FTP upload has failed!";
$response.="FTP upload has failed";
}else{
echo "uploaded <br /> $source_file <br /> to $ftp_server <br /> as
as <br />$destination_file";

}
---------------
Button:

echo "<tr>\n";
echo "<td><input type=\"button\" name=\"print\" value=\"Print current
page\" onClick=\"printFromHere();\"></td>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<div id='printer_response'>";
echo "</div>";
echo "</table>\n";


It never makes it to the alert.
---------javascxript---------

<script type="text/javascript">
function printFromHere(){
alert(hello);
if ( window.XMLHttpRequest ) {// code for IE7+, Firefox, Chrome,
Opera, Safari
xhr=new XMLHttpRequest();
}else{ // code for IE6, IE5
xhr=new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)

document.getElementById( 'printer_response').innerHTML="Received:" +
xhr.responseText;
else
document.getElementById('printer_response').innerHTML
="Error code " + xhr.status;

}
}
xhr.open(GET, 'print_test.php', true);
xhr.send(null);
}

Rds,
Janis
 
J

jr

Is "onsubmit" a valid event for an object of type submit? I thought it
was aformevent.

I'm not sure whether you want to submit data from thisformto the php
page that triggers the print operation or not. Clarification of that
might help. If you do, then to use anajaxrequest, you will have to use
javascript to assemble a full post or get request from theformdata
when thebuttonis clicked. It may be better to use a traditionalform
submit and generate a new page with the printer response message on the
server.

If the data is already on the server, you don't need to use aformfor
this, although how does the server know which data to use? Session
cookies? Job id from theform(which you might need to append to the get
request using ?field=value)? In either case, you probably shouldn't be
triggeringajaxrequests from aform'ssubmitbuttonusing an onsubmit
handler.

You could use theform'sonsubmit handler to specify javascript to
execute when the submitbuttonis pressed. Perhaps that's what you want
to do? If you do that, and don't want to execute theform'saction, make
sure your javascript returns false.

Your submitbuttonis probably trying to call it's parentform'saction,
unless your "onsubmit='blah'" is confusing it completely.

Perhaps you should use a normalbutton(rather than a submit) and give
it a value of, for example, "Send to Printer" and, perhaps, use an
appropriate event handler to call the javascript code.

If I had a better understanding of what you were attempting, I might be
able to give better and / or less confusing pointers to possible solutions.

The following javascript and html, if you understand what it shows,
might be useful to you, or it might not. If you don't understand it, but
instead just blindly copy parts, or all, of it, and things still don't
work, well you obviously didn't take the time to try and understand it
first!

<script type="text/javascript">functionclanger() {
  // visit the soupdragon
  var soupdragon;
  if (window.XMLHttpRequest) { // ie7+ etc
    soupdragon=new XMLHttpRequest();
  }else{ // IE6, IE5
    soupdragon=new ActiveXObject("Microsoft.XMLHTTP");
  }
  soupdragon.onreadystatechange =function()
  {
    if(soupdragon.readyState == 4)
    {
      if(soupdragon.status == 200)
        document.getElementById('printer_response').innerHTML=
          "Received: " + soupdragon.responseText;
      else
        document.getElementById('printer_response').innerHTML=
          "Error code: " + soupdragon.status;
    }
  }
  soupdragon.open(GET, "print_test.php", true);
  soupdragon.send();
  return false;}

</script>

<formaction="blah" method="whatever" onsubmit="clanger()">
..... fields .....
<inputtype="submit">
</form>

Rgds

Denis McMahon

Sorry wait, I ran it through Firebug and it is firing. The problem
must be in the javascript. I will find it. Sorry, for the email.
 
J

jr

Is "onsubmit" a valid event for an object of type submit? I thought it
was aformevent.

I'm not sure whether you want to submit data from thisformto the php
page that triggers the print operation or not. Clarification of that
might help. If you do, then to use anajaxrequest, you will have to use
javascript to assemble a full post or get request from theformdata
when thebuttonis clicked. It may be better to use a traditionalform
submit and generate a new page with the printer response message on the
server.

If the data is already on the server, you don't need to use aformfor
this, although how does the server know which data to use? Session
cookies? Job id from theform(which you might need to append to the get
request using ?field=value)? In either case, you probably shouldn't be
triggeringajaxrequests from aform'ssubmitbuttonusing an onsubmit
handler.

You could use theform'sonsubmit handler to specify javascript to
execute when the submitbuttonis pressed. Perhaps that's what you want
to do? If you do that, and don't want to execute theform'saction, make
sure your javascript returns false.

Your submitbuttonis probably trying to call it's parentform'saction,
unless your "onsubmit='blah'" is confusing it completely.

Perhaps you should use a normalbutton(rather than a submit) and give
it a value of, for example, "Send to Printer" and, perhaps, use an
appropriate event handler to call the javascript code.

If I had a better understanding of what you were attempting, I might be
able to give better and / or less confusing pointers to possible solutions.

The following javascript and html, if you understand what it shows,
might be useful to you, or it might not. If you don't understand it, but
instead just blindly copy parts, or all, of it, and things still don't
work, well you obviously didn't take the time to try and understand it
first!

<script type="text/javascript">functionclanger() {
  // visit the soupdragon
  var soupdragon;
  if (window.XMLHttpRequest) { // ie7+ etc
    soupdragon=new XMLHttpRequest();
  }else{ // IE6, IE5
    soupdragon=new ActiveXObject("Microsoft.XMLHTTP");
  }
  soupdragon.onreadystatechange =function()
  {
    if(soupdragon.readyState == 4)
    {
      if(soupdragon.status == 200)
        document.getElementById('printer_response').innerHTML=
          "Received: " + soupdragon.responseText;
      else
        document.getElementById('printer_response').innerHTML=
          "Error code: " + soupdragon.status;
    }
  }
  soupdragon.open(GET, "print_test.php", true);
  soupdragon.send();
  return false;}

</script>

<formaction="blah" method="whatever" onsubmit="clanger()">
..... fields .....
<inputtype="submit">
</form>

Rgds

Denis McMahon

If someone is still reading this thread, I found the problem. I get
an error in Firebug. It says "GET is undefined".
Why is the GET undefined. I simply want to call the print_test.php
page and get the responseText which is what is echoed on the
print_test.php page.
Rgds,
Janis
function printFromHere(){

if ( window.XMLHttpRequest ) {// code for IE7+, Firefox, Chrome,
Opera, Safari
xhr=new XMLHttpRequest();

}else{ // code for IE6, IE5
xhr=new ActiveXObject("Microsoft.XMLHTTP");

}
xhr.onreadystatechange = function()
{
if(xhr.readyState == 4)
{
if(xhr.status == 200)

document.getElementById( 'printer_response').innerHTML="Received:" +
xhr.responseText;
else
document.getElementById('printer_response').innerHTML
="Error code: " + xhr.status;

}
}
xhr.open(GET, 'print_test.php', true);
xhr.send(null);
var answer= confirm("Confirm all labels were successfully printed?");
if (!answer){return;}
}
thanks,
 
J

jr

Is "onsubmit" a valid event for an object of type submit? I thought it
was a form event.

I'm not sure whether you want to submit data from this form to the php
page that triggers the print operation or not. Clarification of that
might help. If you do, then to use anajaxrequest, you will have to use
javascript to assemble a full post or get request from the form data
when thebuttonis clicked. It may be better to use a traditional form
submit and generate a new page with the printer response message on the
server.

If the data is already on the server, you don't need to use a form for
this, although how does the server know which data to use? Session
cookies? Job id from the form (which you might need to append to the get
request using ?field=value)? In either case, you probably shouldn't be
triggeringajaxrequests from a form's submitbuttonusing an onsubmit
handler.

You could use the form's onsubmit handler to specify javascript to
execute when the submitbuttonis pressed. Perhaps that's what you want
to do? If you do that, and don't want to execute the form's action, make
sure your javascript returns false.

Your submitbuttonis probably trying to call it's parent form's action,
unless your "onsubmit='blah'" is confusing it completely.

Perhaps you should use a normalbutton(rather than a submit) and give
it a value of, for example, "Send to Printer" and, perhaps, use an
appropriate event handler to call the javascript code.

If I had a better understanding of what you were attempting, I might be
able to give better and / or less confusing pointers to possible solutions.

The following javascript and html, if you understand what it shows,
might be useful to you, or it might not. If you don't understand it, but
instead just blindly copy parts, or all, of it, and things still don't
work, well you obviously didn't take the time to try and understand it
first!

<script type="text/javascript">functionclanger() {
  // visit the soupdragon
  var soupdragon;
  if (window.XMLHttpRequest) { // ie7+ etc
    soupdragon=new XMLHttpRequest();
  }else{ // IE6, IE5
    soupdragon=new ActiveXObject("Microsoft.XMLHTTP");
  }
  soupdragon.onreadystatechange =function()
  {
    if(soupdragon.readyState == 4)
    {
      if(soupdragon.status == 200)
        document.getElementById('printer_response').innerHTML=
          "Received: " + soupdragon.responseText;
      else
        document.getElementById('printer_response').innerHTML=
          "Error code: " + soupdragon.status;
    }
  }
  soupdragon.open(GET, "print_test.php", true);
  soupdragon.send();
  return false;}

</script>

<form action="blah" method="whatever" onsubmit="clanger()">
..... fields .....
<inputtype="submit">
</form>

Rgds

Denis McMahon
Is "onsubmit" a valid event for an object of type submit? I thought it
was a form event.

I'm not sure whether you want to submit data from this form to the php
page that triggers the print operation or not. Clarification of that
might help. If you do, then to use anajaxrequest, you will have to use
javascript to assemble a full post or get request from the form data
when thebuttonis clicked. It may be better to use a traditional form
submit and generate a new page with the printer response message on the
server.

If the data is already on the server, you don't need to use a form for
this, although how does the server know which data to use? Session
cookies? Job id from the form (which you might need to append to the get
request using ?field=value)? In either case, you probably shouldn't be
triggeringajaxrequests from a form's submitbuttonusing an onsubmit
handler.

You could use the form's onsubmit handler to specify javascript to
execute when the submitbuttonis pressed. Perhaps that's what you want
to do? If you do that, and don't want to execute the form's action, make
sure your javascript returns false.

Your submitbuttonis probably trying to call it's parent form's action,
unless your "onsubmit='blah'" is confusing it completely.

Perhaps you should use a normalbutton(rather than a submit) and give
it a value of, for example, "Send to Printer" and, perhaps, use an
appropriate event handler to call the javascript code.

If I had a better understanding of what you were attempting, I might be
able to give better and / or less confusing pointers to possible solutions.

The following javascript and html, if you understand what it shows,
might be useful to you, or it might not. If you don't understand it, but
instead just blindly copy parts, or all, of it, and things still don't
work, well you obviously didn't take the time to try and understand it
first!

<script type="text/javascript">functionclanger() {
  // visit the soupdragon
  var soupdragon;
  if (window.XMLHttpRequest) { // ie7+ etc
    soupdragon=new XMLHttpRequest();
  }else{ // IE6, IE5
    soupdragon=new ActiveXObject("Microsoft.XMLHTTP");
  }
  soupdragon.onreadystatechange =function()
  {
    if(soupdragon.readyState == 4)
    {
      if(soupdragon.status == 200)
        document.getElementById('printer_response').innerHTML=
          "Received: " + soupdragon.responseText;
      else
        document.getElementById('printer_response').innerHTML=
          "Error code: " + soupdragon.status;
    }
  }
  soupdragon.open(GET, "print_test.php", true);
  soupdragon.send();
  return false;}

</script>

<form action="blah" method="whatever" onsubmit="clanger()">
..... fields .....
<inputtype="submit">
</form>

Rgds

Denis McMahon

It was a syntax error in the xhr.open("GET", "rpint_test.php",true);
line. It works now.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,077
Messages
2,570,566
Members
47,202
Latest member
misc.

Latest Threads

Top