开发者

Help 3 buttons in AJAX

I want to add 3 buttons with different actions. I have made the PHP code that handles those actions but in order to get to those actions I need with ajax and method GET to add 2 parameters: id and act. I made this, in a very primitive way! It does not work because it only gets the first id="act" (which in this case is delete) no matter what I click!:

<form>
     <input type="hidden" id="act" value="delete">
     <input type="hidden"  id="id" value="'.$row['order_id'].'">    
     <input type="button" id="delete" value="delete" onclick="ajaxFunction()">
</form>

<form>
    <input type="hidden" id="act" value="edit">
    <input type="hidden" id="id" value="'.$row['order_id'].'">
    <input type="button" id="edit" value="edit" onclick="ajaxFunction()">
</form>

And here is the AJAX function:

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
    var ajaxRequest;  // The variable that makes Ajax possible!

    try{
        // Opera 8.0+, Firefox, Safari
        ajaxRequest = new XMLHttpRequest();
    } catch (e){
        // Internet Explorer Browsers
        try{
            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try{
                ajaxRequest = new ActiveXObject("Micro开发者_如何学Csoft.XMLHTTP");
            } catch (e){
                // Something went wrong
                alert("Your browser broke!");
                return false;
            }
        }
    }
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
        if(ajaxRequest.readyState == 4){
            document.myForm.time.value = ajaxRequest.responseText;
        }
    }
    var id = document.getElementById(\'id\').value;
    var act = document.getElementById(\'act\').value;
    var queryString = "?id=" + id + "&act=" + act;
    ajaxRequest.open("GET", "classes/ajaxed/reservation_functions.php" + queryString, true);
    ajaxRequest.send(null); 
}

//-->
</script>

So my question is how to make those 3 buttons with delete, edit, print and each one to send with it GET method the proper ID and ACTION (id & act)?


Simplified Markup:

<button onclick="ajaxFunction('delete', '<?php echo $row['order_id']; ?>')">Delete</button>
<button onclick="ajaxFunction('edit', '<?php echo $row['order_id']; ?>')">Edit</button>
<button onclick="ajaxFunction('print', '<?php echo $row['order_id']; ?>')">Print</button>

Change you need to make to your JS function:

function ajaxFunction(action,id){
    ...
    var queryString = "?id=" + id + "&act=" + action;
    ...
}


The main reason is there are two forms both of them have two buttons having same id i.e. id and act.

So change the id's and your problem will be solved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜