AJAX with multiple forms

Discussion in 'JavaScript' started by mattgoody, Feb 21, 2008.

  1. #1
    Hi, im new to the forum and a fairly inexperienced javascript programmer. I'm decent with php. Anyway, i have a php page displaying results from a mysql db, and next to each row, i have a delete button meant to delete that one row from the table. Anyway, since the js code im using uses getElementById, whenever i press the "delete" button, the first row in the table is removed because the javascript only gets the first form inputs with that specific id. I dont know if im explaining this well, so here's a snippet of my code that outputs the table:
    
    <tr><td>Richard Jefferson</td><td>NJN</td><td><form name='del><input type='hidden' id='dlast' value='Jefferson' /><input type='hidden' id='dfirst' value='Richard' /><input type='button' onclick='ajaxFunction2()' value='Delete' /></form></td></tr>
    <tr><td>Vince Carter</td><td>NJN</td><td><form name='del><input type='hidden' id='dlast' value='Carter' /><input type='hidden' id='dfirst' value='Vince' /><input type='button' onclick='ajaxFunction2()' value='Delete' /></form></td></tr>
    
    HTML:
    I just want to know how i can make the javascript distinguish between the 2 records when it gets the id. When i clickt he delete button on the row next to Vince Carter, i want it to realize that i want the calue for the "dfirst" id that is contained in THAT form, not the other form above it. If anything needs to be clarified, let me know. Thanks so much!

    -Matt

    Oh, heres my javascript code:

    function ajaxFunction2(){
    	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("Microsoft.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){
    			var ajaxDisplay = document.getElementById('ajaxDiv');
    			ajaxDisplay.innerHTML = ajaxRequest.responseText;
    		}
    	}
    
    	var last = document.getElementById('dlast').value;
    	var first = document.getElementById('dfirst').value;
    	var queryString = "?last=" + last + "&first=" + first + "&dlt=true";
    	ajaxRequest.open("GET", "outlive2.php" + queryString, true);
    	ajaxRequest.send(null); 
    }
    Code (markup):
     
    mattgoody, Feb 21, 2008 IP
  2. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You shouldn't have the id the same for two elements - ID's are meant to be unique. Probably the best way is to firstly rename the two elements (for example dlast_1 and dlast_2), then pass them through the function itself.

    <input type="button" onclick="ajaxFunction2('dlast_1', 'dfirst_1')" value="Delete" />
    
    // javascript
    
    function ajaxFunction2(dlast, dfirst)
    
    ...
    
    var last = document.getElementById(dlast).value;
    var first = document.getElementById(dfirst).value;
    Code (markup):
     
    decepti0n, Feb 21, 2008 IP
  3. maiq

    maiq Peon

    Messages:
    389
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You don`t need a form to do this, just a button. But you have to pass the variables to the script when calling the function.
    Like decepti0n said, but no form at all:

    and
     
    maiq, Feb 21, 2008 IP
  4. mattgoody

    mattgoody Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks a ton, both of you. I was just trying to implement passing the variables but it was getting caught because when passing the variables, the getElementById didnt work anymore because it was unnecessary. Anyway thanks maiq for making it that much easier for me, rather than taking way too long tinkering with things.

    -Matt

    oh, btw is firebug the best tool for debugging js?
     
    mattgoody, Feb 22, 2008 IP
  5. decepti0n

    decepti0n Peon

    Messages:
    519
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Not sure, could be! I don't use it, the only thing I use to debug js is the firefox console (and the Console2 addon, but that's if I was building a firefox addon)
     
    decepti0n, Feb 23, 2008 IP