i need post both files , input value throught 1 ajax call here code using actually, upload working problem when submit form 2 rows(see pic) image here
because posting 2 ajax calls same php page, if can post 1 ajajx call both values. html
<form method="post" id="form_upload" enctype="multipart/form-data"> <input type="file" name="user_upload" id="mydrop" /> <input type="hidden" id="userid" value="1" /> </form>
ajax
var form_upload = $("#form_upload").serialize(); $.ajax({ data: form_upload, type: 'post', url: 'upload.php', success : function(data){ alert(data); } }); var file_data = $('#mydrop').prop('files')[0]; var form_data = new formdata(); form_data.append('file', file_data); $.ajax({ url: 'upload.php', datatype: 'text', cache: false, contenttype: false, processdata: false, data: form_data, type: 'post', });
php
<?php if(isset($_post)){ $dbhost = "localhost"; $dbuser = "root"; $dbpass = ""; $dbname = "494815"; try { $dbcon = new pdo("mysql:host={$dbhost};dbname={$dbname}",$dbuser,$dbpass); $dbcon->setattribute(pdo::attr_errmode, pdo::errmode_exception); $targetdir = "users_upload/"; $temp = explode(".", $_files["file"]["name"]); $newfilename = round(microtime(true)) . '.' . end($temp); $targetfile = $targetdir.$newfilename; if move_uploaded_file($_files['file']['tmp_name'],$targetfile); $user_id = $_post['userid']; $stmt = $dbcon->prepare("insert users_upload (user_id, file_name, file_date) values('$user_id', '".$newfilename."','".date("y-m-d h:i:s")."')"); $stmt->execute(); } catch(pdoexception $e) { echo $e->getmessage(); } $conn = null; } ?>
Comments
Post a Comment