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
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";
form._submit_function_ = form.submit;
form.setAttribute("method", method);
form.setAttribute("action", path);
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.
<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>
</form>
</html>