Jump to content

Multiple Submit Buttons on one form


skyer2000

Recommended Posts

I have the following form to give out either the value 1 or 0 when clicked.

 

<div id='ajaxDiv'>
<form name='vote'>
<input type="hidden" id="pictureid" value="<? echo $pictureid; ?>">
<input type="radio" id="vote" value="1" onClick="ajaxFunction()">Yes | <input type="radio" id="vote" value="0" onClick="ajaxFunction()">No
</form>
</div>

 

When no is pressed, it still gives "vote" the value of 0. When I take out the yes and only "no" in the form, it gives the vote value of 0.

 

The following is the AJAX ->

 

<script language="javascript" type="text/javascript">
function ajaxFunction(){
var ajaxRequest;  

try{
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			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 pictureid = document.getElementById('pictureid').value;
var vote = document.getElementById('vote').value;
var queryString = "?pictureid=" + pictureid + "&vote=" + vote;
ajaxRequest.open("GET", "ajaxmysql.php" + queryString, true);
ajaxRequest.send(null); 
}
</script>

 

Any ideas to have the "yes" and "no" work together?

Link to comment
Share on other sites

You have two id's with the same value id="vote".  You can't do that.  Give each of your radio buttons a 'name' value and use the same name for related radio buttons.  Then get the value this way in your ajax code:

var vote = document.formname.radiobuttonname.value;

 

Link to comment
Share on other sites

You have two id's with the same value id="vote".  You can't do that.  Give each of your radio buttons a 'name' value and use the same name for related radio buttons.  Then get the value this way in your ajax code:

var vote = document.formname.radiobuttonname.value;

 

 

I switched the radio buttons to have name values and changed the name of the form to "voteform". Then I used this code:

var vote = document.voteform.vote.value;

 

This didn't work however. Is that the correct way to pull out the value of the associated "name" value?

Link to comment
Share on other sites

Still not working. It kept giving a "undefined" value for vote. Here is the code back to what gave the "1" value for both yes and no clicked. Can someone modify this to make it work? I really appreciate it.

 

<html>
<head>
<script language="javascript" type="text/javascript">
function ajaxFunction(){
var ajaxRequest;  

try{
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			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 pictureid = document.getElementById('pictureid').value;
var vote = document.getElementById('vote').value;
var queryString = "?pictureid=" + pictureid + "&vote=" + vote;
ajaxRequest.open("GET", "regvote.php" + queryString, true);
ajaxRequest.send(null); 
}
</script>
</head>

<div id='ajaxDiv'>
<form name="voteform">
			<input type="hidden" id="pictureid" value="<? echo $pictureid; ?>">
            <input type="radio" id="vote" value="1" onClick="ajaxFunction()">Yes | <input type="radio" id="vote" value="0" onClick="ajaxFunction()">No
</form>
</div>

Link to comment
Share on other sites

you could try passing the value as a parameter:

<input type="radio" id="vote" value="1" onClick="ajaxFunction(this.value)">Yes 

then change your function like this:

function ajaxFunction(vote){

 

 

That still makes it that clicking "no" will pass the "1" value. Any other ideas?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.