0

Problem:

To detect with jQuery which submit button was pressed after the form is sent through PHP.

Scenario:

I have three buttons:

  1. submit-update1
  2. submit-update2
  3. submit-update3

If $_POST['submit-update1'] is set in PHP then jQuery should add a class to a div with id "collapseOne". For instance:

if (PHP POST submit button 1 is submitted)
{
    $('#collapseOne').addClass('in');
}

if (PHP POST submit button 2 is submitted)
{
    $('#collapseTwo').addClass('in');
}

if (PHP POST submit button 3 is submitted)
{
    $('#collapseThree').addClass('in');
}

DIV elements:

<div class="accordion" id="accordion2">
    <div class="accordion-group">
      <div class="accordion-heading">
        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseOne">
          Title 1
        </a>
      </div>
      <div id="collapseOne" class="accordion-body collapse">
        <div class="accordion-inner">
            Text here
        </div>
      </div>
    </div>
    <div class="accordion-group">
      <div class="accordion-heading">
        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseTwo">
          Title 2
        </a>
      </div>
      <div id="collapseTwo" class="accordion-body collapse">
        <div class="accordion-inner">
          Text here
        </div>
      </div>
    </div>
    <div class="accordion-group">
      <div class="accordion-heading">
        <a class="accordion-toggle" data-toggle="collapse" data-parent="#accordion2" href="#collapseThree">
          Title 3
        </a>
      </div>
      <div id="collapseThree" class="accordion-body collapse">
        <div class="accordion-inner">
          Text here
        </div>
      </div>
    </div>
</div>

Submit buttons:

<button type="submit" id="submit-update1" name="submit-update1">Update 1</button>
<button type="submit" id="submit-update2" name="submit-update2">Update 2</button>
<button type="submit" id="submit-update3" name="submit-update3">Update 3</button>

Any advice is welcome and thanks in advance!

4

3 回答 3

2
  <form method="post" id="many_buttons">
    <input type="hidden" id="submit_button" name="button_pressed" />

    <button type="submit" id="submit-update1" name="submit-update1">Update 1</button>
    <button type="submit" id="submit-update2" name="submit-update2">Update 2</button>
    <button type="submit" id="submit-update3" name="submit-update3">Update 3</button>
  </form>

  <script>
  $('#many_buttons button[type=submit]').click(function(){
    $('#submit_button').val($(this).attr('name'))
  })
  </script>

Then in server side:

  <?php
  if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $button = $_POST['button_pressed'];

    if ($button == 'submit-update1')
      $div = 'collapseOne';
    elseif ($button == 'submit-update2')
      $div = 'collapseTwo';
    elseif ($button == 'submit-update3')
      $div = 'collapseThree';
  }
  ?>

Finally you print this javascript:

  <?php if (!empty($div)) : ?>
  <script>$('#<?php echo $div ?>').addClass('in')</script>
  <?php endif ?>
于 2012-06-02T19:02:49.527 回答
0

I assume that you already have the post submit button thing figured out but are having trouble manipulating the page based on those variables. You could use a dynamic script to set some variables based on the post variables like so:

echo "<script type='text/javascript'>";
if (isset($_POST['submit-update1']) && $_POST['submit-update1'] == true) {
    echo "var submitUpdate1 = true;";
}
else {
    echo "var submitUpdate1 = false;";
}
///...do similar things for the rest of your post variables
echo "</script>";

Then in your javascript you would simply access those variables defined above after the document is ready.

于 2012-06-02T18:54:52.887 回答
0

Add this to your html:

<input type="hidden" name="submit" value="" />

And then this is the jQuery:

$('button[type="submit"]').on('click', function(e){
    $('form input[name="submit"]').val($(this).attr('name'));
    $('form').serialize();
});
于 2012-06-02T19:22:59.737 回答