Reputation: 1083
Currently I have an HTML page that has many textboxes that need to be validated via Javascript. Once they are all validated, I am wanting to create a user account via a controller.php
My question is this: How can I POST to a PHP file from within a Javascript function. I gather that I can use AJax, however I would like to use it without this if possible.
thanks
Upvotes: 1
Views: 7978
Reputation: 12916
Create a form with the post method and have it point to the php script. If you set it up like this below you do not need to post from the javascript function because the default behavior of the html form will do the posting for you. It also degrades gracefully for people that do not have javascript enabled.
<form action="/path.to.your.php.file.php" method="POST" onSubmit="return validate();">
... your fields...
<input type="submit" value="validate and submit"/>
</form>
<script>
function validate(){
// do your validation here
return true; // return true if validation was ok, false otherwise
}
</script>
Upvotes: 2
Reputation: 5093
Specify a name for your form like so...
<form name="myForm" id="form1" method="post" action="phpScriptToPostTo.php">
...then run this javascript:
document.forms["myForm"].submit();
...or this in jQuery:
$('#form1').submit();
...or this in jQuery over ajax:
$.post('server.php', $('#form1').serialize())
Upvotes: 0
Reputation: 3634
If you want to use AJAX, using the JQuery library, you can do the following to post to a page
$.ajax({
type: 'POST',
url: url,
data: data,
success: success,
dataType: dataType
});
OR
$.post('controller.php', function(data) {
//do something with the result
});
Upvotes: 0