Executing mysql query once a link/button is clicked

Discussion in 'PHP' started by amelen, Sep 19, 2011.

  1. #1
    We currently have 2 php pages.

    Page 1 - Form (submits to page 2)
    page 2 - Mysql query, submits to DB

    We want to add something in between where when the user gets to page #2, it doesn't automatically execute the query until the user clicks "continue".

    So the question is, how can I make the mysql query on page #2 not execute on page-load, but instead only when the user clicks a link on that page. I'm guessing I need ajax for this?
     
    amelen, Sep 19, 2011 IP
  2. Oli3L

    Oli3L Active Member

    Messages:
    207
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    70
    #2
    well.. at page 1, you can have a form (= form1) that redirects to page 2.

    at page 2, you have another form (= form2), which redirects to the same page (page 2).
    if the form1 redirected you, you'd show the second button.
    when you press the second button, it'll execute the mysql query.

    understand?


    EDIT:
    Just now seen you want to do it with AJAX.
    if so, make a third page with the query it self, and using ajax, call this page usin onclick function.
    pm me if you need help wit that :)
     
    Oli3L, Sep 19, 2011 IP
  3. Alex Roxon

    Alex Roxon Active Member

    Messages:
    424
    Likes Received:
    11
    Best Answers:
    7
    Trophy Points:
    80
    #3
    As for ajax, it depends if you want the page to reload, or the query to happen behind the scenes when the user clicks the button.

    If you're fine with the page reloading, you could just use a form to send them to any other script (or the same one, and just insert). i.e. pseudocode:
    if ( clickedSubmit() ) {
      // Insert query
    } 
    else {
      // Output form w/ button
    }
    Code (markup):
    Otherwise your option would be to use Ajax. I'd personally recommend using a library like jQuery. Then you could do something like:
    <input type="submit" value="Submit" id="submitBtn" />
    Code (markup):
    (function($) {
      $(document).ready(function() {
        $("#submitBtn").click(function() {
          $.ajax({
            url: 'query.php',
            success: function() {
              $("#success").fadeIn();
            }
        });
      });
    }(jQuery));
    Code (markup):
    Untested, but you should get the general idea.
     
    Alex Roxon, Sep 19, 2011 IP
  4. akumar2

    akumar2 Peon

    Messages:
    30
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    not sure.......
     
    akumar2, Sep 20, 2011 IP
  5. amelen

    amelen Active Member

    Messages:
    686
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    85
    #5
    Thanks guys! I got it to work by submitting the page to itself as was suggested.
     
    amelen, Sep 20, 2011 IP