Hello everyone!
I am trying to upload an image from a form using PHP, I am using JQuery first to validate the other fields of the form and if they are valid, then a callback function calls the php page via .post to submit the info to the database.
I decided now to add an image upload for the form but I cannot get it right! I am new with web developing so I'm quite lost, any help would be greatly appreciated!
When I click the button to send the form, nothing happens, and the console tells me there's an error on the PHP file, which I cannot figure out...
So this is some of the code, hopefully understandable:
This is how the form calls the function:
- <td align="right">
- Photo:
- </td>
- <td>
- <input type="file" id="deal_photo" name="deal_photo"/>
- </td>
- <td>
- <div id="reg_deal_photo_feedback"></div>
- </td>
- </tr>
- <tr>
- <td colspan="3" align="center">Elements marked with * are obligatory</td>
- </tr>
- <tr>
- <td colspan="3" align="center"><div class="buybtn"><a href="#">Add Deal!</a></div>
- </form>
This is part of the the jquery function that checks the values:
- ...
- if(document.newdeal.deal_expiry_date.value.length == 0){
- ok=false;
- $('#reg_deal_expiry_date_feedback').html("Enter the day of expiration!").show();
- }
-
- if(ok==true){
- $.post('dealcheck.php', {
- title: document.newdeal.deal_title.value,
- detail: document.newdeal.deal_details.value,
- company: document.newdeal.deal_company.value,
- price: document.newdeal.deal_price.value,
- reg_price: document.newdeal.deal_reg_price.value,
- limit: document.newdeal.deal_limit.value,
- expiry_date: document.newdeal.deal_expiry_date.value,
- photo: document.newdeal.deal_photo.value,
- }, function(check){
- check = "logged_admin.php?id=?"+check;
- window.location.replace('logged_admin.php?msg=4');
- });
- }
- });
- });
and finally this is the php code that processes:
- if (isset($_FILES('image'))){
- $errors = array();
- $allowed_ext = array('jpg','jpeg','png','gif');
-
- $photo_name = $_FILES['image']['name'];
- $photo_ext = strtolower(end(explode('.', $photo_name)));
- $photo_size = $_FILES['image']['size'];
- $photo_tmp = $_FILES['image']['tmp_name'];
-
- if(in_array($photo_ext,$allowed_ext) === false){
- $errors = 'Extension not allowed';
- }
-
- if($photo_size > 2097152){
- $errors = 'Photo size must be less than 2MB';
- }
- if(empty($errors)){
- if(move_uploaded_file($photo_tmp,'images/'.photo_name){
- echo 'Photo uploaded';
- }
- }else{
- foreach ($errors as $error){
- echo $error;
- }
- }
- }
Thank you very much in advance!