Fairly new but learning more every day. I am creating a hybrid App in XDK and have so far managed to get basic data uploaded to a php server using the following;
<script>
function sendData()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("All Gone");
}
}
var postdata = '{"SBP_UserName": "Phil", "SBP_Title": "Title1"}';
xmlhttp.open("POST","http://stxxx.co.uk/SBPostPost.php", false);
xmlhttp.setRequestHeader("Content-type","application/json");
xmlhttp.send(postdata);
}
</script>
Problem: I have searched for 2 days and am getting lost and confused. First - and probably very basic - How do I pass the data from my HTML input form into this function to be sent (what is going at the moment is hardcoded as you can see);
But Most important - how can I get this to send a Form that contains Input fields AND a File (graphic) in the same upload.
Any help will be very much appreciated.
I'm still really struggling...any further help would be very much appreciated.
This is my FORM
<form id="PostData" name="PostData" method = "post" enctype="multipart/form-data">
<p>Page Title<p>
<label class="narrow-control label-inline" for="VehicleReg">Vehicle Reg:</label>
<input class="wide-control" type="text" name="VehicleReg" id="VehicleReg">
<label class="narrow-control label-inline" for="Category">Offense:</label>
<select class="wide-control" name="Category" id="Category">
<option>Select</option>
<option>Opt1</option>
<option>Opt2</option>
</select>
<label class="narrow-control label-inline" for="Details">Details</label>
<textarea class="wide-control" rows="4" wrap="soft" name="Details" id="Details"></textarea>
<input id="mypic" type="file" accept="image/*;capture=camera" width="50%">
<button onclick="sendData(this.form)">Post</button>
</form>
and here the JS
<script>
function sendData(form)
{
var postform = new FormData(form);
var xmlhttp = new XHRObject();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
//xmlhttp.responsetext will have what is sent back - use Print_R in php
}
}
xmlhttp.open("POST","http://sxxxx.co.uk/SBPostPost.php", false);
xmlhttp.setRequestHeader("Content-type","multipart/form-data");
// only sends now previous stuff just establishing a connection
xmlhttp.send(postform);
}
</script>
When I run in an emulator and look at the console - it IS communicating with my back end; and the headers hold the data fields, but it's failing (I think) on accessing the Picture that has been selected. Error is http://127.0.0.1:58889/http-services/emulator-webserver/ripple/userapp/x/C/…ratchdir/e14eb410-7cf2-4bed-ba25-515e6df98e8c/platforms/ios/www/index.html 404 (Not Found)
Any idea?