I am trying to run a PHP file when a user submits the form. However, the submit() function never runs. I have read many posts making suggestions about this and none of them have resolved the problem. So I have a form that looks like this:
<body>
<form id="signupform">
<div id="required">
<table>
Form fields
</table>
</div>
<div id="notrequired">
<table>
More form fields
</table>
</div>
<button type="Submit" name="action">Submit</button>
</form>
<script>
$(document).ready(function() {
$("#signupform").submit(function(e) {
alert("Please work!");
$.ajax({
type : 'POST',
url : 'process.php',
data : JSON.stringify($'signupform').serializeArray(),
dataType: 'json'
})
//More code
});
}););
</script>
</body>
Nothing, except type, has the value 'Submit' in the file. I have tried it with and without the $(document).ready(). So I'm pretty much out of ideas. Any suggestions.
Oh, it doesn't work on any browsers.
There's a syntax error on this line:
data : JSON.stringify($'signupform').serializeArray(),
You missed the parentheses in the call to $()
.
Also you missed the #
to select by id, and you might want .serialize()
rather than .serializeArray()
, though obviously that isn't actually a syntax error.
It should be:
data : JSON.stringify($('#signupform').serialize()),
// add parens here -------^ ^ and here ------------^
// add # here --------------/
Also there was an extra );
at the end of your function that should be removed.
You don't really need the JSON.stringify()
either:
data : $('#signupform').serialize(),
And somewhere (perhaps you already have this in your "more code" section) you want to stop the default form submit action:
e.preventDefault();