Click here to Skip to main content
15,896,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
can someone please help me understand why im not able to sendformpost to myPHP? whenI click on submit I get alert ready but I dont get the "sent"
here is my code

XML
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<style>
.table {display:table;}
.row {display:table-row;}
.cell {display: table-cell; padding: 5px;}
.label {text-align: right;}
</style>
</head>
<body>
<form id="fruitform" method="post" action="upload.php">
<div class="table">
<div class="row">
<div class="cell label">Bananas:</div>
<div class="cell"><input name="bananas" value="2"/></div>
</div>
<div class="row">
<div class="cell label">Apples:</div>
<div class="cell"><input name="apples" value="5"/></div>
</div>
<div class="row">
<div class="cell label">Cherries:</div>
<div class="cell"><input name="cherries" value="20"/></div>
</div>
<div class="row">
<div class="cell label">File:</div>
<div class="cell"><input type="file" name="file"/></div>
</div>
<div class="row">
<div class="cell label">Total:</div>
<div id="results" class="cell">0 items</div>
</div>
</div>
<button id="submit" type="submit">Submit Form</button>
</form>
<script>
document.getElementById("submit").onclick = handleButtonPress;
var httpRequest;
function handleButtonPress(e) {
e.preventDefault();
var form = document.getElementById("fruitform");
var formData = new FormData(form);
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = handleResponse;
httpRequest.open("POST", form.action);alert('ready');
httpRequest.send(formData);alert('sent');
}
function handleResponse() {
if (httpRequest.readyState == 4 && httpRequest.status == 200) {
document.getElementById("results").innerHTML
= httpRequest.responseText;
}
}
</script>
</body>
</html>
Posted
Comments
Kornfeld Eliyahu Peter 6-Jan-14 2:21am    
I tried you code - without new FormData(form), that I have not, and it's works perfectly.
I think you may have a JavaScript error, that breaks the flow - so run it with some kind of client side debugger to see...
vanakhas 6-Jan-14 14:29pm    
so on your end you have ran alert('sent'); script!
Kornfeld Eliyahu Peter 6-Jan-14 14:47pm    
Yes - but remember! I have no FormData so I run with occasional data as formData...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900