0

as I stated in the title, I can get the onclick calling object only in Firefox: not in Chrome, not in IE, not in Safari.

I am pretty new to ajax and javascript in general, so I built my code around the answers you guys gave here.

I have a html page with a number of 'products': for each one of them, I have a form with hidden fields which contain the information about the product. Every form has two (submit) buttons: one is to 'add' the product to the shopping cart, the other is to take it 'off' of it. I want to identify the button that gets clicked in order to identify the product it refers to and then add it to or cancel it from the cart list.

Here is the html code for the page:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>
        TEST
    </title>

    <meta charset="utf-8">
<script src="form_submit.js" type="text/javascript">
</script>
</head>

<body>
    <form id="ajax_1" name="ajax_form" method="POST" action="test.php">
        <fieldset>
            <legend>My form</legend>
            <label for="stnz">Stnz</label><br />
            <input type="hidden" name="stnz" id="stnz" value="1" /><br />

            <label for="opz">Opz</label><br />
            <input type="hidden" name="opz" id="opz" value="1" /><br />


            <button id="ajax_1" type="submit" name="on">On</button>
            <button id="ajax_1" type="submit" name="off">Off</button><br />
        </fieldset>
    </form>
    <form id="ajax_2" method="POST" action="test.php">
        <fieldset>
            <legend>My form</legend>
            <label for="stnz">Stnz</label><br />
            <input type="hidden" name="stnz" id="stnz" value="1" /><br />

            <label for="opz">Opz</label><br />
            <input type="hidden" name="opz" id="opz" value="2" /><br />


            <button id="ajax_2" type="submit" name="on">On</button>
            <button id="ajax_2" type="submit" name="off">Off</button><br />
        </fieldset>
    </form>
    <form id="ajax_3" method="POST" action="test.php">
        <fieldset>
            <legend>My form</legend>
            <label for="stnz">Stnz</label><br />
            <input type="hidden" name="stnz" id="stnz" value="1" /><br />

            <label for="opz">Opz</label><br />
            <input type="hidden" name="opz" id="opz" value="3" /><br />


            <button id="ajax_3" type="submit" name="on">On</button>
            <button id="ajax_3" type="submit" name="off">Off</button><br />
        </fieldset>
    </form>
    <div id="responseArea">&nbsp;</div>
</body>
</html>

This is the script code:

window.onload = checkButton;
var xhr = false;
var on;
var stnz;   
var opz;
var url;



function checkButton() {
var el = document.getElementsByTagName('button');
for(var i=0; i<el.length; i++){
   el[i].onclick = function (e) {

    // Capturing the event  


e = e || window.event;
var targ = e.target || e.srcElement;

on = (targ.name == 'on') ? true: false;

var form = document.getElementById(targ.id);        
url = form.action;      
stnz = form.stnz.value;     
opz = form.opz.value;

makeRequest();
return false;

   };
}
 }


function makeRequest(){
 if (window.XMLHttpRequest) {
  xhr = new XMLHttpRequest();
}else {
  if (window.ActiveXObject) {
    try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
        }
    catch (e) { }
}   
 }

if (xhr){       
    var data = "stnz=" + stnz + "&opz=" + opz + "&act=";
    if(on){
             data = data + "1";
            } else {
                data = data + "0";
        }

    xhr.onreadystatechange = showContents;
    xhr.open("POST", url, true);
    xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("Content-length", data.length);
    xhr.setRequestHeader("Connection", "close");
    xhr.send(data);
     }
       }

function showContents(){    
    if(xhr.readyState == 4 && xhr.status == 200) {
        var return_data = xhr.responseText;
        console.log(return_data);
        } else {
            var return_data = "Sorry, but I couldn't create an XMLHttpRequest";
            console.log(return_data);
            }   
    /*document.getElementById("responseArea").innerHTML = return_data;*/
}

The test.php file is just:

<?php
if (isset($_POST['stnz'],$_POST['opz'],$_POST['act'])){
$stnz= $_POST['stnz'];
$opz = $_POST['opz'];
$act = $_POST['act'];
echo "Stnz: " . $stnz . ", Opz: " . $opz . ", Azt: " . $act;
} 

?>

Please, help me fixit this thing for Chrome, IE and Safari.... Also, is there a better way to get the same functionality? (maybe not using forms?) Thanks a lot!

Community
  • 1
  • 1
Tercol
  • 1
  • 3

1 Answers1

0

each browser has its own event handling method , use a library like jquery to be able to handle all the browsers .

$(el[i]).click(function(e){});

using jquery isn't the only solution you can optimize your code for every browser by adding the browser specific codes but that is a recipe for disaster.same goes for your ajax request(cross browser problems).

jquery designed with all the browsers in mind , so you write just one code and jquery handles the browser specific stuff .

example for your ajax with jquery : https://api.jquery.com/jQuery.ajax/

$.ajax({
   url : url,
   type:'POST',
   data:{"stnz" : stnz , "opz" : opz , "act" : (on ? 1 : 0)}
   success : function (data){

   },
   error:function(){}
});
Exlord
  • 5,009
  • 4
  • 31
  • 51
  • Thanks for your answer. Do you think using jQuery would be the only way to fix this? Also, using some 'console.log()' here and there in the script file, I think I found out that the problem is in the makeRequest function: it looks like Chrome, IE and Safari do not allow the XMLHttpRequest to get by, but it seems well formulated to me. Can you spot any mistake in the code? Again, thank a lot for your help. – Tercol Apr 06 '14 at 15:07
  • thank you. It seems I solved this problem: nothing to do with the code, just configuration settings. Chrome, IE and Safari are not my main browsers and for some strange reason their initial configuration did not allow me to do a XMLHttpRequest (don't ask me why!). Reset everything properly and now everything is fine. – Tercol Apr 07 '14 at 07:06