1

HTML --

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
Field: <span id="test"></span>

<form id="form" method="post">
  <input type="submit" name="submit" value="Submit" />
  <input type="submit" name="submit" value="Don't Click" />
</form>

<script>
/* attach a submit handler to the form */
$("#form").submit(function(event) {

  /* stop form from submitting normally */
  event.preventDefault();

  /* set all the vars you want to post on here */
  var parameters = { 
    'submit': $('input[name="submit"]').val()
  };

    $.ajax({
         url: 'php/test.php',
         method:'POST',
         data: parameters,
         success: function(msg) {
            $('#test').append(msg);
         }
    })

});

PHP --

<?php
    $submit = $_POST['submit'];

    if($submit === "Submit"){
        echo 'Success!';
    } else if($submit === "Don't Click") {
        echo 'You Effed Up!';
    }
?>

When I run the Ajax call, it always displays Success in the Field: area. Why is that? Should it not be getting the value of whichever submit button was clicked?

Mr.Meekoh
  • 33
  • 1
  • 6
  • You need to pass the form data in your ajax call - at least a POST var named `submit` with a value called `Input Information` – raidenace May 03 '13 at 18:18
  • 5
    You're vulnerable to [SQL injection attacks](http://bobby-tables.com) – Marc B May 03 '13 at 18:19
  • [How to prevent SQL injection in PHP?](http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php) – Andreas May 03 '13 at 18:22
  • There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future. – Schleis May 03 '13 at 20:39
  • Got it done :) Thank you for the hints; they are greatly appreciated! – Mr.Meekoh May 06 '13 at 14:43

1 Answers1

2

You would need to send the parameters with your ajax call:

var parameters = { 
          'submit': 'foo',
          'x': '123a',
          'y': '123b',
          'w': '123c'
          };

j(document).ready(

    j.ajax({
         url: 'php/buttonActions.php',
         method:'POST',
         data: parameters,
         success: function() {
             alert('win');
         }
    })
);

On the PHP end, use POST instead of REQUEST for all.

UPDATE:

If you want to post through ajax after the user clicks the submit button, then you need to set the values according to whatever the user typed. Example:

HTML test.html

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

Field: <span id="test"></span>

<form id="form" method="post">
  <input type="text" name="w" value="" />
  <input type="text" name="submit" value="" />
  <input type="submit" value="submit" />
</form>

<script>
/* attach a submit handler to the form */
$("#form").submit(function(event) {

  /* stop form from submitting normally */
  event.preventDefault();

  /* set all the vars you want to post on here */
  var parameters = { 
    'w': $('input[name="w"]').val(),
    'submit': $('input[name="submit"]').val()
  };

    $.ajax({
         url: 'test.php',
         method:'POST',
         data: parameters,
         success: function(msg) {
            $('#test').append(msg);
         }
    })

});
</script>

PHP test.php

<?php print_r($_POST); ?>
Fabi
  • 973
  • 7
  • 18
  • I edited the answer with the submit example. The value of course could be anything... and for every param you want to send, you do that – Fabi May 03 '13 at 18:39
  • submit is the name of your variable. I took examples from your code: $_REQUEST['x'], $_REQUEST['y'], $_REQUEST['w'] and so on – Fabi May 03 '13 at 18:46
  • Hey, just did a quick edit above; hows that looking? Wish I could up your answer to be more useful; unfortunately I'm a new member -_-. Thanks a ton for your help Fabi! – Mr.Meekoh May 03 '13 at 18:54
  • it's a post, so it will be as if you had posted by simply submitting. On the php side, $_POST['submit'], etc – Fabi May 06 '13 at 15:17
  • well you need to test that before u do anything on the DB. Try: success: function(data) { alert(data); } and on the php side, just a simple if ($_POST['submit'] == 'bla') echo 'Ok'; else echo 'Not Ok'; that way you can find out if its working or not since it will return a response to the first page/js. – Fabi May 06 '13 at 15:43
  • There must be something wrong with the code then. I'll paste my test on here and you can try that. – Fabi May 06 '13 at 16:03
  • That wont work, success returns whatever your php returns. I'd encourage some reading on how it works http://api.jquery.com/jQuery.ajax/ – Fabi May 06 '13 at 17:04
  • whatever you echo on the php page will return to your main page. The PHP page does its thing and tells you what happened. – Fabi May 06 '13 at 17:25
  • The fact that you clicked the button doesnt set its value alone. Use the value of an input text field instead and one submit button only. A lot of what you're doing is trial/error, so it would help you to try different ways of getting what you want, like instead of using a submit button value, use an actual value. – Fabi May 06 '13 at 18:34
  • But why do you need to get the value of the submits? Depending on the reason you can work around that. – Fabi May 06 '13 at 18:55
  • ah ok, its the way they determine what kind of form is being submmitted I guess.. but that would be still just one button from what I see. – Fabi May 06 '13 at 19:58
  • but cant you have just one type=SUBMIT? and type=reset or type=button as a second button? – Fabi May 06 '13 at 20:19
  • You know what? I have no idea; lemme try that out and let you know how it goes. I can't even thank you enough for all your help Fabi; truly showing me how much knowledge this site has to offer. – Mr.Meekoh May 06 '13 at 20:29
  • np sorry cant help much more i have work to get done too lol good luck! – Fabi May 06 '13 at 20:31
  • So, it's looking like I'm going to go the route of possibly creating 3 separate forms with 3 separate submit buttons calling their appropriate SQL statements. I wanted to go the route of just creating one form with one submit, however I'm a bit unsure how to do that in this things current state, lol. – Mr.Meekoh May 07 '13 at 13:32