0

I'm looking to make an html form post to a php script but I do not want to actually redirect the user to this script. All I need is for the script to take the post data, do something with it and then somehow return this data so it's useable to the calling script. Is this at all possible?

The only way I can think of doing it is to have a php script that posts to itself but it's very messy as I am using jQuery and JavaScript inside the calling script.

Thanks in advance

  • 3
    You might be better off using jQuery/AJAX POST request in that case. – Maximus2012 May 13 '15 at 16:03
  • jQuery has a nice set of AJAX functions and they are [easy to use](http://jayblanchard.net/basics_of_jquery_ajax.html) – Jay Blanchard May 13 '15 at 16:03
  • and maybe this link help you: > [jQuery Ajax POST example with PHP][1] [1]: http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php?rq=1 – Mojtaba Rezaeian May 13 '15 at 16:17
  • Maybe this link help you: > [jQuery Ajax POST example with PHP][1] [1]: http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php?rq=1 – Mojtaba Rezaeian May 13 '15 at 16:20

1 Answers1

1

Use JQuery Ajax.

Assuming that you have a form like

<form id="Form" action="response.php" method="post">...</form>

JQuery:

$('#Form').submit(function (event){
        $.ajax({
            type:"POST", // define method post or get
            url:$('#Form').attr('action'), // gets the post url from your action attribute
            data:$('#Form').serialize(), // binds your form data
            dataType:'json', // server response data type, use 'html' 'json' etc
            encode:true,
            success:function(data){
            },
            error:function( xhr, status, errorThrown ) {
            alert( "Sorry, there was a problem!" );
            console.log( "Error: " + errorThrown );
            console.log( "Status: " + status );
            console.dir( xhr );

            },
        });
         event.preventDefault(); // prevents from going to the posting url
    });
asfandahmed1
  • 462
  • 5
  • 23
  • 1
    Why should the OP try this? Please add an explanation of what you did and why you did it that way, not only for the OP but for future visitors to SO. – Jay Blanchard May 13 '15 at 16:06
  • @david, I have solved same problem with above code in a prestashop ajax login module. It's better not rate down an answer before you try it :) – Mojtaba Rezaeian May 13 '15 at 16:14
  • Hi all. Thanks for the replies. I'm not sure why a reply had been marked down as an answer. This is the first time I've looked at it since originally posting.I'll give the suggestions a go. Thanks again – David O'Reilly May 13 '15 at 17:53