helpmescript
helpmescript

Reputation: 29

Can't get values from form?

I can't get the value from the form. Due to Firebug the form is submitted tho and when I delete the js it works so it has something to do with it.

$ok= $_POST['ok']; //this doesnt work

if($ok== "ok" ){ 
  echo "works";
}


<script type="text/javascript">
    $(document).ready(function() {
        var frm = $('#form');
        frm.submit(function (ev) {
            $.ajax({
                type: frm.attr('method'),
                url: frm.attr('action'),
                data: frm.serialize(),

            });
            ev.preventDefault();
        });
    });

 function submitForm2() {
        $('#form').submit();
    }
</script>

<div onclick='submitForm2();'></div>

<form action='' method='post' id='form'>
        <input type="hidden" name='ok' value="ok">

    </form>

Upvotes: 0

Views: 52

Answers (1)

Quentin
Quentin

Reputation: 943193

When the form is submitted, your first set of JavaScript kicks in. It:

  • Stops the normal submission process running
  • Takes the data from the form and submits it via Ajax

Since the normal form submission doesn't run, the page doesn't reload, so you don't load a new document, so you don't see the document with the ok in it loaded in the main browser window.

Since you don't have a success handler for the Ajax request, you completely ignore the response the server sends back to the JavaScript … so you don't see that document (with the ok in it) either.

If you want to see the results of an Ajax request, then you have to write JS to show you the results (or examine the Net tab of your developer tools).

Upvotes: 1

Related Questions