odk with php mysql code example
Example: odk with php mysql
<?php
//With default Collect Submission path setting, this script would be located at https://your.server/submission/index.php
//this is where all the uploaded files will eventually be stored.
$datapath='/path/to/your/datafiles/';
//Set a variable to tell us whether an XML file has successfully been uploaded later:
$xmlfile=false;
//we need to process POST and HEAD requests separately, and cater for GET just in case
$method=strtolower($_SERVER['REQUEST_METHOD']);
//Issue these response headers to both HEAD and POST requests:
header("Content-Type: text/xml; charset=utf-8");
header("Content-Language: en");
header("X-OpenRosa-Version: 1.0");
header("X-OpenRosa-Accept-Content-Length: 10000000");//limit upload to 10Mb - probably too high for most realistic scenarios!
//Now process POST and HEAD requests differently
if ($method=='post') {
//we should have at least one POSTED file
if (isset($_FILES)) {
//we need to move all the POSTed files from the tmp upload folder, as with any file POST, then respond with a Success code and message.
foreach($_FILES as $file) {
$filename=$file['name'];
$ok=move_uploaded_file($file['tmp_name'],$datapath.$filename);
//There should only be one XML file, which is the completed form. Any other files are assets, whose file names will be included in the form data.
if ($file['type']=="text/xml") $xmlfile=$file['name'];
}
header("HTTP/1.1 202 Accepted");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
echo '<OpenRosaResponse xmlns="http://openrosa.org/http/response" items="1">';
echo '<message nature="submit_success">Form submitted successfully</message>';
echo '</OpenRosaResponse>';
}
else {
/* If $_FILES is not set in the POST, that means we have no XML file of the completed form, so we need to return an error. This should really be a 400, but
in my original implementation circa 2012, Collect didn't interpret a 400 response correctly, and only a 403 response would produce a human-readable
error message in Collect. */
header("HTTP/1.1 403 Forbidden");
echo '<?xml version="1.0" encoding="UTF-8" ?>';
echo '<OpenRosaResponse xmlns="http://openrosa.org/http/response" items="1">';
echo '<message nature="submit_success">Submission unsuccessful - no data files were received.</message>';
echo '</OpenRosaResponse>';
}
}
if ($method=='head' || $method=='get') {
//This is the initial HEAD request from Collect, so we need to return a Location header telling Collect where to POST the completed form.
//We also provide this response to GET requests to give us browser testing options.
header("Location: $_SERVER[SCRIPT_URI]");//this script -
header("HTTP/1.1 204 No Content");//that's all, no actual content in the response.
}
if ($xmlfile!==false){
//now process the contents of $path.$xmlfile however you want to!
require 'my_form_processor_script.php';
}
?>