Home How to give $_POST and $_FILES together in the AJAX data field so that PHP can grab the value?
Reply: 0

How to give $_POST and $_FILES together in the AJAX data field so that PHP can grab the value?

user4327
1#
user4327 Published in April 26, 2018, 1:46 pm

I have the following form which has

  • a text field
  • date field
  • a file browser.

I am using AJAX to send the $_POST data values to another PHP file to insert into a MySQL database. But I want to move the $_FILES too.

In the $.ajax field, there is data: whereby I can assign those data to be transferred to another PHP file.

I am able to do it with the text field and date fields. How to do it for the $_FILES? My codes are as below

AJAX

<script>
    $("#submit").click(function() {
        var prjId = $('#prjId').val();
        var updatedDate = $('#updatedDate').val();
        $.ajax({
            type: 'POST',
            url: "process.php",
            data: {prjId: prjId,updatedDate: updatedDate},
            success: function(response) {('#resulting').html(response);}
        });
    });
</script>

HTML

<!DOCTYPE html>
<html lang="en">
    <head>
        <link rel="icon" type="image/png" href="images/version-control.png">
        <meta charset="utf-8">
        <link href='https://fonts.googleapis.com/css?family=Raleway:400,300,700,900' rel='stylesheet' type='text/css'>
        <link href='https://fonts.googleapis.com/css?family=Montserrat:400,700' rel='stylesheet' type='text/css'>
        <!-- Latest compiled and minified CSS -->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <!-- jQuery library -->
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <!-- Latest compiled JavaScript -->
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <body>
    <body>
        <div class="container" id="contactform">
            <form method="post" enctype="multipart/form-data">
                <div class="form-group row">
                    <label class="col-sm-3 col-form-label">Project ID</label>
                    <div class="col-sm-7"><?php if(isset($_POST['prjId'])){echo '
                        <input type="text" class="form-control" placeholder="Project ID" name="prjId" id="prjId" value="'.$_POST['prjId'].'">';}else{echo'
                        <input type="text" class="form-control" placeholder="Project ID" name="prjId" id="prjId">';}?>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-sm-3 col-form-label">Updated Date</label>
                    <div class="col-sm-7"><?php if(isset($_POST['udatedDate'])){echo '
                        <input type="date" class="form-control" name = "updatedDate" id="updatedDate" value="'.$_POST['udatedDate'].'">';}else{echo '
                        <input type="date" class="form-control" name = "updatedDate" id="updatedDate">';}?>
                    </div>
                </div>
                <fieldset class="form-group ">
                    <label class="btn btn-default tempPerm" id="techLabelText">
                        <input class="tempPerm" style="" type="file" name="file" id="techInputBoxValue" />
                    </label>
                </fieldset>
            </form>
            <div class="cover">
                <div id="result"></div>
                <input name="submit" id="submit" tabindex="5" value="Send Mail" type="submit" style="width:200px;">
            </div>
        </div>
    </body>
</html>

PHP

<?php include ("../db.php");?>
<?php
    $prjId = $_POST['prjId'];
    $updatedDate = $_POST['updatedDate'];
    if(isset($prjId)){
        $sql="INSERT INTO tbl_uploads(prjId, date) VALUES('$prjId','$updatedDate')";
        mysqli_query($conn, $sql);
    }
?>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.30221 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO