tags:

views:

410

answers:

1

What I want to do is: When each button is pressed, show it's respective value. Right now the value returned is always "1". The json.php file will eventually be used to update records in a database.

Any help would be greatly appreciated. Thanks!

Here's the json.php file:

<?php

  if(isset($_POST['hidden'])) {

   $data['value'] = $_POST['hidden'];

  }

  echo json_encode($data);

?>

And here's the HTML:

<html>
<head>
<title>jQuery/Ajax - Display respective values on submit for inputs with same class  name</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt;
<script type="text/javascript">
$(document).ready(function() {

  $("form[name=form1]").submit(function() {
    $.post("json.php", { hidden : $(".check").siblings(".hidden").val() },
      function(data){
        alert(data.value);
      }, "json");
      return false;
  });

});
</script>
</head>
<body>

<form action="<?php $_SERVER['PHP_SELF']; ?>" name="form1" method="post">
  <input type="submit" class="check" value="check1" /><br />
  <input type="submit" class="check" value="check2" /><br />
  <input type="submit" class="check" value="check3" />
  <input class="hidden" name="hidden" type="hidden" value="1" />
  <input class="hidden" name="hidden" type="hidden" value="2" />
  <input class="hidden" name="hidden" type="hidden" value="3" />
</form>


</body>
</html>
A: 

If you just want to display which button was pressed, by its value (which I think is what you're asking, the question isn't very clear, and not sure what you're trying to achieve with the JSON) you could simply do,

var currentFocus = '';    

$("input.check").click(function() {
    currentFocus = $(this).val();
});

// then the standard code you have, displaying what was last pressed.
$("form[name=form1]").submit(function() {
$.post("json.php", { hidden : $(".check").siblings(".hidden").val() },
    function(data){
        alert(currentFocus);
    }, "json");
    return false;
});

The current way you have your code means that whenever the form is submitted, $.post() triggers, which means you can't actually tell which button was pressed, further more the hidden fields you're passing across all have the same name, which means the value just gets overwritten by the other hidden fields with the same name.

jakeisonline
The json.php will eventually be used to update records in a db.
Scott
This doesn't work.
Scott
Yes it does.. http://www.jakeisonline.com/stackoverflow/1483346-jquery-ajax-json-display-respective-values-on-submit-for-inputs-with-same-class/You should also perhaps consider using jQuery.getJSON - http://docs.jquery.com/Ajax/jQuery.getJSON
jakeisonline
Are you still having trouble with this? Is this not the solution you wanted? If you clarify, I'd be more than happy to amend the code.
jakeisonline
Yea...still having some trouble. I see what you did with the example...and that's almost what I was trying to accomplish. The json.php will be used to update items pulled from a db with PHP...so all the hidden inputs will have a unique ID. Problem is, when I use the json file to get the respective ID's it's only returning the first value. I'll try to give you a better example. I appreciate everything you've done so far.
Scott
Hey thanks for the help...I figured it out. I wasn't passing my ID's correctly when submitting. It's working fine now. I'm dumb...and I'm going to bed :)
Scott
Great that you got it all sorted :)
jakeisonline