1

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

asked May 21, 2012 at 23:54
2
  • You don't have to you AJAX. You can also use simple javascript and send a request to your page. Commented May 21, 2012 at 23:57
  • 1
    Why would you use javascript for this? Commented May 21, 2012 at 23:57

3 Answers 3

2

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>
answered May 22, 2012 at 0:00
Sign up to request clarification or add additional context in comments.

Comments

0

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
});
answered May 21, 2012 at 23:59

1 Comment

Question asks for a non-ajax answer
0

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())
answered May 21, 2012 at 23:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.