Results 1 to 5 of 5

Thread: 2 onclick submit buttons

  1. #1
    Join Date
    Feb 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 2 onclick submit buttons

    I'm using the following javascript code to collect data from a form via javascript and then send the var's to a php script and return the data to the same page via ajax, All is working fine at the moment.

    Code:
    //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("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 pollname = document.getElementById('pollname').value;
    	var pollid = document.getElementById('pollid').value;
    
    
    for(i=0; i<document.getElementById('freepoll').optionselect.length; i++){
        if(document.getElementById('freepoll').optionselect[i].checked){
            var optionselect = document.getElementById('freepoll').optionselect[i].value;
            }
        } 
    
    
    	var wpm = document.getElementById('wpm').value;
    	var sex = document.getElementById('sex').value;
    	var queryString = "?pollname=" + pollname + "&pollid=" + pollid + "&click=" + click + "&optionselect=" + optionselect + "&wpm=" + wpm + "&sex=" + sex;
    	ajaxRequest.open("GET", "ajax-poll.php" + queryString, true);
    	ajaxRequest.send(null); 
    }
    I use the following button code in my html web page.

    HTML Code:
    <input type="button" onclick="ajaxFunction()" id="click" value="vote">
    What i want to do is add a second submit button to the html page and have the javascript send 1 var to the php script if submit1 is clicked and another var if submit2 is clicked. All the other var's sent via the form will remain the same regardless of which button is clicked, i just need to let the php script know which submit button was pressed.

    Thanks for your help
    Chris.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Where's the first submit button and what does it look like? You might not mean a submit button though, you might mean the button from your post which is an ordinary button input with an onclick event. I'm not much up n the PHP side of things but, in javascript, if you were to have two buttons like that:

    HTML Code:
    <input type="button" onclick="ajaxFunction()" id="click" value="vote">
    The second one would be required to have a different id BTW but id's, though they could be used aren't needed here. One button could be:

    Code:
    <input type="button" onclick="ajaxFunction('but1')" id="click" value="vote">
    and the other:

    Code:
    <input type="button" onclick="ajaxFunction('but2')" id="click2" value="vote">
    Then your function could start like so:

    Code:
    function ajaxFunction(butvar){
    That way, all throughout the function, butvar would carry the string ('but1' or 'but2') of the button used. What you do with that is up to you but a simple demo would be:

    Code:
    function ajaxFunction(butvar){
    alert(butvar);
    }
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Feb 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi john it was a button i was talking about and your code did exactly what i was looking to do. So a very big thanks you

    Chris.

  4. #4
    Join Date
    Feb 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi again, just got 1 more quick question. After the button is pressed and the ajax reloads another form appears and i need the ajax to continue to work but it seems the javascript only works the once. How can keep the javascript working for the second form.

    I think i need to use
    Code:
    evalScripts: true
    but i'm not sure how to include that in my javascript above.

    Many thanks
    Chris.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In many cases (looks like it would work in this one), if the script is in the head of the 'top' page, it will be available to any content loaded on to the 'top' page via Ajax. However, if initialization is required, see (simple method):

    http://www.dynamicdrive.com/forums/s...ad.php?t=17426

    and possibly (advanced method):

    http://www.dynamicdrive.com/forums/s...ad.php?t=13003
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •