Home show values entered in form as a table before getting submitted
Reply: 0

show values entered in form as a table before getting submitted

user2520
1#
user2520 Published in May 23, 2018, 7:20 am

First, I do not have much talent in ajax so please consider that while answering. Here I want to get all the values which I inserted into the form in a table before getting submitted but unfortunately am not getting the results I want. So far I have done this. Please have a look.

 <script>
 var form_data={ agent_name: $('#agent_name').val(),
                    type: $('input[name="type"]').val(),
                    number: $('#number').val(),
                    quantity: $('#quantity').val()
                  }

    $.ajax({
    type: 'POST',
    url: '<?php echo base_url();?>admin_control/ajax_data',
    data: form_data,
    dataType:"json", //to parse string into JSON object,
    success: function(data){ 
        if(data){
            var len = data.length;
            alert(len);
            var txt = "";
            if(len > 0){
                for(var i=0;i<len;i++){
                    if(data[i].agent_name && data[i].type){
                        txt += $('#table').append('<tr class="item-row"><td><input type="hidden" id="add_type" name="add_type[]" value="super">Super</td><td><input type="hidden" id="add_amount" name="add_amount[]" value="10">745</td><td><input type="hidden" class="add_quantity" id="add_quantity" name="add_quantity[]" value="10">10</td><td name="add_amount" id="add_amount">100</td><td><input type="checkbox" class="add_checkbox" name="layout" id="add_checkbox" value="1" checked></td></tr>');

                    }
                }
                if(txt != ""){

                    $("#table").append(txt).removeClass("hidden");
                }
            }
        }
    },
    error: function(jqXHR, textStatus, errorThrown){
        alert('error: ' + textStatus + ': ' + errorThrown);
    }
});
return false;
</script>  

Here I want to pass the values of form_data in to the table I had written and how can we pass that and did it compulsory to use the url in ajax while using this am getting an error like error: parsererror: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data.

Here is my controller

public function ajax_data()
{
  $array = array("agent_name" => "admin","number"=>"785","type"=>"super","quantity"=>"10");
  $data['json'] = $array;
  echo json_encode($data);
}
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO