PDA

View Full Version : 2 onclick submit buttons



wkdchris
02-19-2007, 10:01 PM
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.



//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.



<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.

jscheuer1
02-20-2007, 04:02 AM
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:


<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:


<input type="button" onclick="ajaxFunction('but1')" id="click" value="vote">

and the other:


<input type="button" onclick="ajaxFunction('but2')" id="click2" value="vote">

Then your function could start like so:


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:


function ajaxFunction(butvar){
alert(butvar);
}

wkdchris
02-20-2007, 06:26 PM
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.

wkdchris
02-21-2007, 10:51 PM
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
evalScripts: true but i'm not sure how to include that in my javascript above.

Many thanks
Chris.

jscheuer1
02-22-2007, 05:20 AM
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/showthread.php?t=17426

and possibly (advanced method):

http://www.dynamicdrive.com/forums/showthread.php?t=13003