1

I have made one form which is two step based. also i have added feature add more. So when you add click show same filed duplicate on step two.

Problem---- When i'm trying to send data by (add more) more then one or two. data not sending to database also not seeing any error.

please help me to solve this my full code below--


add_shortcode('encash_management', 'encashdevice_form');
function encashdevice_form(){
    ob_start();
    error_reporting(E_ALL);
ini_set('display_errors', 1);

?>
<div id="step1">
<form action="" method="post" enctype="multipart/form-data" >
<fieldset class="encashform">
    <p><label>Company Name (Same as Trade Licence)*</label><input name="company_name" type="text" value="" /></p>
    <p><label>Contact Person Name*</label><input name="contact_person_name" type="text" value="" /></p>        
    <p><label>Contact Email*</label><input name="company_email" type="text" value="" /></p>
    <p><label>Contact Number*</label><input name="contact_number" type="text" value=""/></p>
    <p><label>Address</label><input name="company_address" type="text" value=""/></p>
    <p><label>License</label><input name="license" type="file" accept="image/x-png,image/jpg,image/jpeg,application/pdf" multiple="multiple" /></p>
    <p><label>VAT Certificate</label><input name="vat_certificate" type="file" accept="image/x-png,image/jpg,image/jpeg,application/pdf" multiple="multiple" /></p>
    <p><label>ID of the Owner</label><input name="owner_idcard[]" type="file" accept="image/x-png,image/jpg,image/jpeg,application/pdf" multiple="multiple"/></p>
    <input type="button" value="Next" id="next-step"/>
</fieldset>
</div>

<div id="step2" style="display: none;">
                <fieldset class="encashform">
                    <div id="fields">
                        <p><label>Device type:</label><select name="device_type[]">
                            <option>Select Type</option>
                        <option value="Mobile">Mobile</option>
                        <option value="Laptop">Laptop</option>
                        <option value="Desktop">Desktop</option>
                        <option value="Others">Others</option>
                        
                        </select></p>
                        <p><label>Brand:</label><select name="brand[]">
                            <option value="">Select Brand</option>
                            <option value="Apple">Apple</option>
                            <option value="Samsung">Samsung</option>
                            <option value="Huawei">Huawei</option>
                            <option value="Xiaomi">Xiaomi</option>
                            <option value="OnePlus">OnePlus</option>
                            <option value="Oppo">Oppo</option>
                            <option value="Vivo">Vivo</option>
                            <option value="Sony">Sony</option>
                            <option value="LG">LG</option>
                            <option value="Google (Pixel)">Google (Pixel)</option>
                            <option value="Motorola">Motorola</option>
                            <option value="Nokia">Nokia</option>
                            <option value="HTC">HTC</option>
                            <option value="BlackBerry">BlackBerry</option>
                            <option value="ASUS">ASUS</option>
                            <option value="Lenovo">Lenovo</option>
                        
                        </select></p>
                        <p><label>Model:</label><input name="model[]" type="text" value="" /></p>
                        <p><label>Physical condition:</label>
                            <select name="physical_condition[]">
                                <option>Select Condition</option>
                            <option value="Excellent - no marks of usage works perfectly fine">Excellent - no marks of usage works perfectly fine</option>
                            <option value="Good - Marks of usage no damage works perfectly fine">Good - Marks of usage no damage works perfectly fine</option>
                            <option value="Fair - Marks of usage Minor damage Works perfectly fine">Fair - Marks of usage Minor damage Works perfectly fine</option>
                            <option value="Broken - Physical damage">Broken - Physical damage</option>

                            </select>
                        </p>
                        <p><label>Quantity:</label><input name="quantity[]" type="text" /></p>
                        <p><label>Purchased as:</label>
                            <select name="purchase_as[]" id="">
                                <option value="New">New</option>
                                <option value="Used">Used</option>
                            </select>
                        </p>
                        <p><label>Images:</label><input name="images[]" type="file" accept="image/x-png,image/jpg,image/jpeg,application/pdf" multiple="multiple"/></p>
                        <input type="button" value="Add More" onclick="addField()"/>
                    </div>
                    <input type="button" value="Back" id="prev-step"/>
                    <button type="submit">Submit</button>
                </fieldset>
                </form>
</div>
<script type="text/javascript">
    jQuery(function(){

        // Step 1 to Step 2 transition
        jQuery("#next-step").click(function() {
            jQuery("#step1").hide();
            jQuery("#step2").show();
        });

        // Step 2 to Step 1 transition
        jQuery("#prev-step").click(function() {
            jQuery("#step2").hide();
            jQuery("#step1").show();
        });
    });
//    jQuery("#add-more").click(function() {
//         addField();
//     });
//     jQuery("#remove-fields").click(function() {
//         rmField();
//     });
    var num=0;
    function addField(){
        num++;
        if(num>7){num--;}
        makefields();
    }
    function rmField(){
        num--;
        if(num<0){num++;}
        makefields();
    }

    function makefields() {
    var fields = "";
    for (var o = 0; o <= num; o++) {
        fields += "<div class='device-fields'>";
        fields += "<p><label>Device type:</label><select name='device_type[]'><option>Select Type</option><option value='Mobile'>Mobile</option><option value='Laptop'>Laptop</option><option value='Desktop'>Desktop</option><option value='Others'>Others</option></select></p>";
        fields += "<p><label>Brand:</label><select name='brand[]'><option>Select Brand</option><option value='Apple'>Apple</option><option value='Samsung'>Samsung</option><option value='Huawei'>Huawei</option><option value='Xiaomi'>Xiaomi</option><option value='OnePlus'>OnePlus</option><option value='Oppo'>Oppo</option><option value='Vivo'>Vivo</option><option value='Sony'>Sony</option><option value='LG'>LG</option><option value='Google (Pixel)'>Google (Pixel)</option><option value='Motorola'>Motorola</option><option value='Nokia'>Nokia</option><option value='HTC'>HTC</option><option value='BlackBerry'>BlackBerry</option><option value='ASUS'>ASUS</option><option value='Lenovo'>Lenovo</option></select></p>";
        fields += "<p><label>Model:</label><input name='model[]' type='text' class='text-long' /></p>";
        fields += "<p><label>Physical condition:</label><select name='physical_condition[]'><option>Select Condition</option><option value='Excellent - no marks of usage works perfectly fine'>Excellent - no marks of usage works perfectly fine</option><option value='Good - Marks of usage no damage works perfectly fine'>Good - Marks of usage no damage works perfectly fine</option><option value='Fair - Marks of usage Minor damage Works perfectly fine'>Fair - Marks of usage Minor damage Works perfectly fine</option><option value='Broken - Physical damage'>Broken - Physical damage</option></select></p>";
        fields += "<p><label>Quantity:</label><input name='quantity[]' type='text' class='text-long' /></p>";
        fields += "<p><label>Purchased as:</label><select name='purchase_as[]'><option value='New'>New</option><option value='Used'>Used</option></select></p>";
        fields += "<p><label>Images:</label><input name='images[]' type='file' accept='image/x-png,image/jpg,image/jpeg,application/pdf' multiple='multiple'/></p>";
        fields += "</div>";
    }
    fields += "<br/><input type='hidden' name='num' value='"+o+"'/>";
    if (num != 6) { 
        fields += "<button type='button' onclick='addField()'>Add More</button>"; 
    }
    if (num > 0) { 
        fields += "<button type='button' onclick='rmField()'>Remove</button>"; 
    }
    jQuery("#fields").html(fields);
}
   
</script>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['company_name']) && isset($_POST['contact_person_name']) && isset($_POST['company_email'])  && isset($_POST['contact_number']) && isset($_POST['company_address']) && isset($_FILES['license']) && isset($_FILES['vat_certificate']) && isset($_FILES['owner_idcard']) && isset($_POST['device_type']) && isset($_POST['brand']) && isset($_POST['model']) && isset($_POST['physical_condition']) && isset($_POST['quantity']) && isset($_POST['purchase_as']) && isset($_FILES['images'])) {
    var_dump($_POST);
    var_dump($_FILES);
    global $wpdb;
    $table_name = $wpdb->prefix . 'encash';
    $table_name_devices = $wpdb->prefix . 'encash_devices';

    // Sanitize and prepare data
    $company_name = sanitize_text_field($_POST['company_name']);
    $company_email = sanitize_email($_POST['company_email']);
    $person_name = sanitize_text_field($_POST['contact_person_name']);
    $contract_number = sanitize_text_field($_POST['contact_number']);
    $address = sanitize_text_field($_POST['company_address']);
    $Status = 'New';

    // Define the directory where you want to move the files
    $targetDir = plugin_dir_path(__FILE__) . 'encashform/';
    $targetDirImages = plugin_dir_path(__FILE__) . 'encashform/encashimages/';
    // Create the directories if they don't exist
    if (!file_exists($targetDir)) {
        mkdir($targetDir, 0755, true);
    }
    if (!file_exists($targetDirImages)) {
        mkdir($targetDirImages, 0755, true);
    }    
    // Move the License file
    if ($_FILES["license"]["name"]) {
        $licenseFileName = basename($_FILES["license"]["name"]);
        move_uploaded_file($_FILES["license"]["tmp_name"], $targetDir . $licenseFileName);
    }

    // Move the VAT Certificate file
    if ($_FILES["vat_certificate"]["name"]) {
        $vatCertificateFileName = basename($_FILES["vat_certificate"]["name"]);
        move_uploaded_file($_FILES["vat_certificate"]["tmp_name"], $targetDir . $vatCertificateFileName);
    }

    // Move the Owner ID Card files (if multiple files are uploaded)
    $ownerIdCardFiles = array();
    if (!empty($_FILES['owner_idcard']['name'][0])) {
        foreach ($_FILES['owner_idcard']['name'] as $key => $name) {
            $ownerIdCardFileName = basename($name);
            move_uploaded_file($_FILES['owner_idcard']['tmp_name'][$key], $targetDir . $ownerIdCardFileName);
            $ownerIdCardFiles[] = $ownerIdCardFileName;
        }
    }

    // Insert owner information into database
    $owner_data = array(
        'company_name' => $company_name,
        'company_email' => $company_email,
        'contact_person_name' => $person_name,
        'contact_number' => $contract_number,
        'company_address' => $address,
        'license' => $licenseFileName,
        'vat_certificate' => $vatCertificateFileName,
        'owner_idcard' => implode(',', $ownerIdCardFiles),
        'Status'=>  $Status
    );

    $wpdb->insert($table_name, $owner_data);
    $owner_id = $wpdb->insert_id; // Get the inserted owner ID

    // Insert device information into database
if (isset($_POST['device_type']) && isset($_POST['brand']) && isset($_POST['model']) && isset($_POST['physical_condition']) && isset($_POST['quantity']) && isset($_POST['purchase_as']) && isset($_FILES['images'])) {
   
$imagesfiles = array();
if (!empty($_FILES['images']['name'][0])) {
    foreach ($_FILES['images']['name'] as $key => $name) {
        $imagesfilesName = basename($name);
        move_uploaded_file($_FILES['images']['tmp_name'][$key], $targetDirImages . $imagesfilesName);
        $imagesfiles[] = $imagesfilesName;
    }
}
    foreach ($_POST['device_type'] as $key => $value) {
        // Prepare device data for insertion
        $device_data = array(
            'company_info_id' => $owner_id,
            'device_type' => sanitize_text_field($_POST['device_type'][$key]),
            'brand' => sanitize_text_field($_POST['brand'][$key]),
            'model' => sanitize_text_field($_POST['model'][$key]),
            'physical_condition' => sanitize_text_field($_POST['physical_condition'][$key]),
            'quantity' => sanitize_text_field($_POST['quantity'][$key]),
            'purchase_as' => sanitize_text_field($_POST['purchase_as'][$key]),
            'images' => sanitize_text_field($imagesfiles[$key]),
            'Status'=>  $Status
        );
        $inserted = $wpdb->insert($table_name_devices, $device_data);

        if ($inserted === false) {
            // Failed to insert data
            echo "Failed to insert device data into the database: ' . $wpdb->last_error";
        }
    }
}

}



return ob_get_clean();
} ```

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.