Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Send More Than One Data in PHP Ajax Form

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 585
    Answer it
    <input type="file" name="file" id="file" /> 
    
    <div style="background:url() no-repeat"> 
    <span id="uploaded_image" ><img src=" '.$row["carimg"].' " height="150" width="225" class="img-thumbnail" /></span></div>

     

    Here is the ajax code

     

    <script> 
    $(document).ready(function(){ 
    $(document).on('change', '#file', function(){ 
    var name = document.getElementById("file").files[0]... 
    var form_data = new FormData(); 
    var ext = name.split('.').pop().toLowerCase(); 
    if(jQuery.inArray(ext, ['gif','png','jpg','jpeg']) == -1) 
    { 
    alert("Invalid Image File"); 
    } 
    var oFReader = new FileReader(); 
    oFReader.readAsDataURL(document.getEleme... 
    var f = document.getElementById("file").files[0]... 
    var fsize = f.size||f.fileSize; 
    if(fsize > 2000000) 
    { 
    alert("Image File Size is very big"); 
    } 
    else 
    { 
    form_data.append("file", document.getElementById('file').files[0]... 
    $.ajax({ 
    url:"up1.php", 
    method:"POST", 
    data: form_data, 
    contentType: false, 
    cache: false, 
    processData: false, 
    beforeSend:function(){ 
    $('#uploaded_image').html("<label class='text-success'>Image Uploading...</label>"); 
    }, 
    success:function(data) 
    { 
    $('#uploaded_image').html(data); 
    } 
    }); 
    } 
    }); 
    }); 
    </script> 

     

    here is the php

     

    <?php
    
    //upload.php
    $id = $_FILES["vid"];
    
    if($_FILES["file"]["name"] != '')
    {
     $test = explode('.', $_FILES["file"]["name"]);
     $ext = end($test);
     $name = gen_random_string(6)."n" . '.' . $ext;
     $location = 'assets/img/cars/' . $name;  
     move_uploaded_file($_FILES["file"]["tmp_name"], $location);
     echo $id;
     echo '<img src="'.$location.'" height="150" width="225" class="img-thumbnail" />';
    
     $sql = "UPDATE vehicles SET carimg='".$location."' WHERE vid='".$id."'";
    }
     mysqli_query($connect, $sql);  
    ?>

     

    How to send more than one data in this php ajax form this one capable of sending one data only please help

 1 Answer(s)

  • you can append/add more data same as file in formData data like:- 
    form_data.append(document.getelementById(someid).vlaue()) or
    form_data.append($('#someid or any other selector).value);

    or you can use : $('form').serialize(); if data is in same form
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: