skip to Main Content

this is my json encoded data from php that i received and i wish to pass this into the model box form. i don’t know what is wrong with my code because i am not get any error message of any kind please i need help

[{"id":"2","name":"MR Udo ante","phone":"08127249596","email":"[email protected]","address":"18 Udo abasi street by pepperoni off Abak Road Uyo"}]

and i am using jquery ajax to receive the data from php json encoded

$(document).ready(function(){      
    $(document).on('click', '.edit_data', function(){  
       var customer_id = $(this).attr("id");  
       $.ajax({

            type:'POST',
            url:"./includes/fetch.php",
            dataType: "json",    
            data:{customer_id:customer_id},   
            success:function(data){  
                $('#name').val(data.name);
                $('#email').val(data.email); 
                $('#phone').val(data.phone);  
                $('#address').val(data.address);  
                $('#customer_id').val(data.id);  
                $('#insert').val("Update");  
                $('#edit_data_Modal').modal('show');  
            }  
       });  
    }); 
});

but these data is not showing in my model input box or form, it is still empty

<div class="modal fade" id="edit_data_Modal" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title" id="exampleModalCenterTitle">Edit this customer</h3>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">
                <p class="editStatusMsg"></p>
                <form method="POST" id="insert_form">
                    <div class="form-row">
                        <div class="form-group col-md-12">
                            <label for="name">Edit Customer or Company Name</label>
                            <input type="text" name="name" id="name" class="form-control" />
                        </div>
                        <div class="form-group col-md-6">
                            <label for="email">Edit Email Address</label>
                            <input type="email" name="email" id="email" class="form-control" />
                        </div>
                        <div class="form-group col-md-6 mb-3 mb-lg-0">
                            <label for="phone">Edit Phone Number</label>
                            <input type="tel" name="phone" id="phone" class="form-control" />
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputAddress">Edit address</label>
                        <input type="text" name="address" id="address" class="form-control" />
                    </div>
                    <div class="form-group">
                        <input type="hidden" name="customer_id" id="customer_id" />
                        <input type="submit" name="insert" id="insert" value="Insert" class="btn btn 


primary" />
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary subAdd" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>

model box

3

Answers


  1. Your json fields seem to be in a list (your hash delimited by { } is in a [ ] )
    So, if is that, data.name is undefined and you need instead to use data[0].name

    Login or Signup to reply.
  2. Please try to format your response data :

    {
      "id": "2",
      "name": "MR Udo ante",
      "phone": "08127249596",
      "email": "[email protected]",
      "address": "18 Udo abasi street by pepperoni off Abak Road Uyo"
    }
    
    Login or Signup to reply.
  3. The data return an array so you need access your data by: data[0].attribute
    replace data[0].name for data.name.

    The Ajax code should:

    
    <script>
    $(document).ready(function(){      
        $(document).on('click', '.edit_data', function(){  
           var customer_id = $(this).attr("id");  
           $.ajax({
    
                type:'POST',
                url:"./includes/fetch.php",
                dataType: "json",    
                data:{customer_id:customer_id},   
                success:function(data){  
                    $('#name').val(data[0].name);
                    $('#email').val(data[0].email); 
                    $('#phone').val(data[0].phone);  
                    $('#address').val(data[0].address);  
                    $('#customer_id').val(data[0].id);  
                    $('#insert').val("Update");  
                    $('#edit_data_Modal').modal('show');  
                }  
           });  
        }); 
    });</script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search