ajax 2 forms only one works

mikearmas
mikearmas used Ask the Experts™
on
ok i have two forms on 1 page.

then i have a the ajax getting the var from the form and post it to the php.

the php works it updates the dababase.

BUT it ONLY works for the first form on the page.

the plus IF i i put the second form first then the mines works.

So it only work with the first form and not with both How can i get both working????
and
Why does it do that ???
this is my form

  <form name="plus" method="post" action="">

     <input  style=" display:none"type="text" name="fbid" id="fbid" size="1" value="<?=$fbid?>" class="text-input" />
 
     <input style=" display:none" type="text" name="plus1" id="plus1" size="1" value="1" class="text-input" />
     	<br />
      <input type="submit" name="plus-button" class="button" id="plus_btn" value="+ Good" />
 
  </form>

  <form name="mines" method="post" action="">
   
    
    <input style=" display:none" type="text" name="fbid" id="fbid" size="1" value="<?=$fbid?>" class="text-input" />
   
    <input style=" display:none" type="text" name="mines1" id="mines1" size="1" value="-1" class="text-input" />
    	<br />
      <input type="submit" name="mines-button" class="button" id="mines_btn" value="- Evil" />
   
  </form>


this is my js file 


$(function() {
 
  

  $(".button").click(function() {
		// validate and process form
		// first hide any error messages
  
	  var fbid = $("input#fbid").val();
		
		var mines1 = $("input#mines1").val();
		
		var plus1 = $("input#plus1").val();
		
		
		var dataString = 'fbid='+ fbid + '&mines1=' + mines1 + '&plus1=' + plus1;
		//alert (dataString);return false;
		
		$.ajax({
      type: "POST",
      url: "process.php",
      data: dataString,
      success: function() {
       
      }
     });
    return false;
	});
});
runOnLoad(function(){
  $("input#fbid").select().focus();
});


this is my php 

<?php



  $dbuser="fbfbfbf";

	  $dbpassword="fbfbfbfb";

	  $database="fbfbfbfb";

//$fbid = $user_profile['id'];

mysql_connect(localhost,$dbuser,$dbpassword);
mysql_select_db($database) or die( "Unable to select database");

$up = $_POST['plus1'];
$down = $_POST['mines1'];
$fbid = $_POST['fbid'];

if($up == '1') {
       $query1 = "UPDATE Facebook SET click=click+1 WHERE fbid ='$fbid' ";
       $result = mysql_query($query1) or die(mysql_error());
       }

if($down == '-1') {
       $query1 = "UPDATE Facebook SET click=click-1 WHERE fbid ='$fbid' ";
       $result = mysql_query($query1) or die(mysql_error());
       }


   ?>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
You can have only one ID in a page. So, change one of your #fbid to some ID else.

Author

Commented:
no that didn't work
Commented:
I wrapped parts of your JS to:

$(function() {
	$(".button").click(function() {
		var dataString = "";
		if ($(this).attr('name')=="plus-button") {
			dataString = $('input.plus').serialize();
		} else {
			dataString = $('input.minus').serialize();
		}
        $.ajax({
			type: "POST",
			url: "process.php",
			data: dataString,
			success: function() {
			}
		});
    });
});

Open in new window


and your FORM to:

  <form name="plus" method="post" action="">

     <input  style=" display:none"type="text" name="fbid" id="fbid" size="1" value="<?=$fbid?>" class="text-input plus minus" />
 
     <input style=" display:none" type="text" name="plus1" id="plus1" size="1" value="1" class="text-input plus" />
     <input style=" display:none" type="text" name="mines1" id="mines1" size="1" value="-1" class="text-input minus" />
      <input type="button" name="plus-button" class="button" id="plus_btn" value="+ Good" />
         <br />
      <input type="button" name="mines-button" class="button" id="mines_btn" value="- Evil" />

  </form>

Open in new window

Author

Commented:
Damn i need to read more about js you got it working thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial