I´m developing a wordpress pluging. I have a php file that controll all the plugin and some php class and css files.
In the main php file I create a form. When the form is submited, a Jquery.Ajax function catch the form values and send them to a php function defined in the main php file. After this jquery have to give you a alert verification.
I know how to send the file imagen. I know hot to send the text values.
But I have not idea how to send the file imagen and the text values to my php function in my php main file. Here is my code:
PHP main file:
Create the form
function formulario_nueva_ft(){
$cadena="<form id='form_new_ft' method='post' enctype='multipart/form-data'>
$cadena.="<input id='tit_new_ft' maxlength='40' type='text'>";
$cadena.="<input id='horas_new_ft' type='text'>";
$cadena.="<textarea id='requisitos_new_ft' type='text' maxlength='245'> </textarea>";
$lista=new areatematica;
$res=$lista->listarareastematicas();
$cadena.="<select id='cod_at_new_ft'>";
foreach($res as $valor){
$cadena.="<option value='.$valor->id_at.'>".$valor->titulo_at."</option>";
}
$cadena.="</select>";
$cadena.="<select id='modalidad_new_ft'>
<option value='online'>Online</option>
<option value='mixta'>Mixta</option>
<option value='presencial'>Presencial</option>";
$cadena.="</select>";
$cadena.="<input type='file' name='new_pdf_ft' id='new_pdf_ft'/>";
$cadena.="<h4 id='loading' >loading..</h4><div id='message'></div>";
$cadena.="<input id='btn_new_ft' type='submit' value='Añadir'>";
$cadena.="</form>";
echo $cadena;
die();
}
Here is where I catch the ajax response ,insert data in mysql and upload the image file:
function crear_ft(){
$tit=$_POST['tit'];
$hor=$_POST['hor'];
$req=$_POST['req'];
$cod_at=$_POST['cod'];
$modalidad=$_POST['mod'];
$pdf=$_POST['pdf'];
$tabla='ft';
$ft=new curso();
$sourcePath = $_FILES['new_pdf_ft']['tmp_name'];
$targetPath = "archivos/".$_FILES['new_pdf_ft']['name'];
move_uploaded_file($sourcePath,$targetPath);
$ft->set_titulo($tit);
$ft->set_horas($hor);
$ft->set_requisitos($req);
$ft->set_cod_at($cod_at);
$ft->set_modalidad($modalidad);
$ft->set_pdf($pdf);
$ft->set_tabla($tabla);
$nada=$ft->insertarcurso();
echo $nada;
die();
}
And this is my js code:
This have to be 'live' method because is about an element that appears
after other js event.
jQuery('#form_new_ft').live("submit", function(e){
var file = new FormData(jQuery('#form_new_ft'));
e.preventDefault();
jQuery("#message").empty();
jQuery('#loading').show();
jQuery.ajax({
url: "/wordpress/wp-admin/admin-ajax.php",
type: "POST",
contentType: false,
processData: false,
cache: false,
//how to send img file and string values?
data: file + { tit:jQuery('#tit_new_ft').val(),
hor:jQuery('#horas_new_ft').val(),
req:jQuery('#requisitos_new_ft').val(),
mod:jQuery('#modalidad_new_ft').val(),
cod:jQuery('#cod_at_new_ft').val(),
pdf:jQuery('#modalidad_new_ft').val(),
action:'crear_ft'//this is the php function that will
//handle this data.
},
success: function(data){
jQuery("#message").html('');
jQuery("#message").html(data);
},
error: function(){
alert("Error funcion");
}
});
});
Help me please!!