0% found this document useful (0 votes)
3 views2 pages

Send Post Data From From With Empty Action and Send The Info With Javascript To The File That Must Handle The Post Information

send post data from from with empty action and send the info with javascript to the file that must handle the post information

Uploaded by

April Killase
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
3 views2 pages

Send Post Data From From With Empty Action and Send The Info With Javascript To The File That Must Handle The Post Information

send post data from from with empty action and send the info with javascript to the file that must handle the post information

Uploaded by

April Killase
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 2

send post data from from with empty action and send the info with javascript to the

file that
must handle the post information.
<HTML>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<script>
function post_to_url(path, params, method) {
method = method || "post";

var form = document.createElement("form");

form._submit_function_ = form.submit;

form.setAttribute("method", method);
form.setAttribute("action", path);

for(var key in params) {


var hiddenField = document.createElement("input");
hiddenField.setAttribute("type", "hidden");
hiddenField.setAttribute("name", key);
hiddenField.setAttribute("value", params[key]);

form.appendChild(hiddenField);
}

document.body.appendChild(form);
form._submit_function_();
}
post_to_url("./postinfo.php", { submit: "submit" } );
</script>
<form method="post" onsubmit="return post_to_url()">
<input type="text" name="ime">
<input type="submit" id="submit" name="submit" value="Send">
</form>

So how i can send the post data to postinfo.php with javascript with empty action in my html
form?
$('#form_id').on('submit', function(e){
e.preventDefault();
$.ajax({
type: "POST",
url: "/postinfo.php",
data: $(this).serialize(),
success: function() {
alert('success');
}
});
});

I have tried but it's not redirecting me to postinfo.php like with action="postinfo.php" in html form. Also it's
not giving the success alert. I pasted exactly your code in <script></script> tags before my post form and i
added id="form_id" to my form.

Try this code.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></
script>

<script type="text/javascript">
function validate() {
var ra = document.getElementById("uname").value;
var rag = document.getElementById("pwd").value;
$.ajax({
type: "POST",
url: "/login",
contentType: "application/json",
dataType: 'json',
data:JSON.stringify({
username:ra,
password:rag
}),
success: function() {
alert('success');
}
});
console.log(ra, rag)

}
</script>
<html>
<form name="myform">
<p>ENTER USER NAME <input id="uname" type="text" name="username"></p>

<p>ENTER PASSWORD <input type="password" id="pwd" name="pwd"></p>


<input type="button" value="Check In" name="Submit" onclick= "validate()">

</form>
</html>

You might also like