Código PHP:
<?php
if(isset($_POST['formInsert'])) {
require_once("conn.php");
mysql_select_db($database_conn,$conn);
$image =$_FILES['userfile']['name'];
$value=strtoupper($_POST['value']);
$country=strtoupper($_POST['country']);
$pais=strtoupper($_POST['pais']);
mysql_query("INSERT INTO flags (value,imageName,pais, country) values ('$value','$image','$pais','$country')");
echo mysql_error($conn);
if($_FILES['userfile']['name']!=""){
// Subir el archivo donde nosotros queramos
$upfile = 'images/flags/'.$_FILES['userfile']['name'];
if(is_uploaded_file($_FILES['userfile']['tmp_name']))
{
if( !move_uploaded_file($_FILES['userfile']['tmp_name'],$upfile)) {
echo "no se pudo mover el archivo al destio deseado";
exit;
}
}
else {
echo 'es probable que el la conexion se inestable';
}
////
}
// header("Location:flags.php");
$fInsert=1;
}
?>
<head>
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link href="../css_s/formsAdmin.css" rel="stylesheet" type="text/css">
<title>Untitled Document</title>
<style type="text/css">
<!--
body {
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
}
-->
</style>
<script language="javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
$('#myForm').submit(function() {
$.ajax({
type: 'POST',
enctype="multipart/form-data" ,
url: $(this).attr('action'),
data: $(this).serialize(),
success: function(data) {
$('#divContent').html(data);
}
})
return false;
});
</script>
</head>
<body>
<?php if(!$fInsert){?>
<div id="stylized" class="myform">
<div class="whiteBorder">
<form enctype="multipart/form-data" action="flagsAdd.php" method="POST" id="myForm" target="iframeUpload" >
<h1>FLAGS FORM</h1>
<p>Please provide all the info in the form</p>
<label>Value:</label><input type="text" name="value" id="value">
<label>Image:</label>
<input type="file" name="userfile" id="userfile" />
<label>Pais:</label>
<input type="text" name="pais" id="pais" />
<label>Country:</label>
<input type="text" name="country" id="country" />
<input name="formInsert" type="hidden" value="1" />
<button type="submit">Submit</button>
<div class="spacer"></div>
</form>
<iframe name="iframeUpload" style=" display:none"> <!-- Se utiliza para la insercion del file-->
</iframe>
</div>
</div>
<?php } else{?>
<div style="padding:20px; text-align:center;">Your New Menu has been stored in the DB</div>
<?php }?>
</body>
</html>