3

I need to fetch my id and Edit the tables value..but i am not able to fetch id from database and actually i am trying to send it by this way....

$(document).on('submit', '#form_company.edit', function(e){
e.preventDefault();
// Validate form
if (form_company.valid() == true){
  // Send company information to database
  hide_ipad_keyboard();
  hide_lightbox();
  show_loading_message();
  var id        = $('#form_company').attr('data-id');
  var form_data = $('#form_company').serialize();
  var request   = 

 $.ajax({
    url:          'data.php?job=edit_company&id=' + id,
    cache:        false,
    data:         form_data,
    dataType:     'json',
    contentType:  'application/json; charset=utf-8',
    type:         'get'
  });

`

but it is getting passed by URL ..i want to send it by "data:"..how to do it..

2
  • try this, data : {id : id}, and use post method. Commented Jul 22, 2016 at 7:42
  • Please keep in mind that ajax calls are visible to the outside world. Is it ok that the id value is public knowledge? Using 'data.php' people can clean out your database? Commented Jul 22, 2016 at 7:44

4 Answers 4

1

You can post both data in POST parameter like below:-

$.ajax({
  url:          'data.php',
  type:          'POST',
  cache:        false,
  data:         {'form_data':form_data,'job':'get_company','id': id} ,
  dataType:     'json',
  contentType:  'application/json; charset=utf-8',

});

And now in php (data.php):-

<?php
  echo"<pre/>";print_r($_POST);
?>

Note:- based on this printed data you can do your stuff accordingly. Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

yes after using data:{'job':'get_company','id': id} ..it works but it's fetching only ID..but doesn't get updated..(i am using it to edit table)
1

You are using type as get that's why it is passing data by url. You should use POST if not want to pass data in URL

$.ajax({
  url:          'data.php?job=get_company',
  type:          'POST',
  cache:        false,
  data:         {id: id} ,
  dataType:     'json',
  contentType:  'application/json; charset=utf-8',

});

1 Comment

what if i send it like... {'job':'get_company','id': id} ,..will it work..
1

Check my changes and hope this would be helpful to you.

$(document).on('submit', '#form_company.edit', function(e){
e.preventDefault();
// Validate form
if (form_company.valid() == true){
  // Send company information to database
  hide_ipad_keyboard();
  hide_lightbox();
  show_loading_message();

  var form_data = $('#form_company').serialize();
  form_data.append('id',$('#form_company').attr('data-id'));
  form_data.append('job',"edit_company");

  var request= $.ajax({
    url:          'data.php',
    cache:        false,
    data:         form_data,
    dataType:     'json',
    contentType:  'application/json; charset=utf-8',
    type:         'post'
  });

1 Comment

i want to send it by data..not through URL..i will call only 'data.php'..and all other in data:...field..
1

Try a post request with the following:

$.ajax({
    url:          'data.php',
    cache:        false,
    data:         {form_data:form_data,job:'edit_company',id:id},
    dataType:     'json',
    contentType:  'application/json; charset=utf-8',
    type:         'POST',
    success:       function(data){
    }
});

2 Comments

i am getting error..data: {form_data:form_data,job:'edit_company',id:id},..is not working..
it works ..only i change it with this..small change('').. data: {'form_data':form_data,job:'edit_company',id:id},..thanx for your help..

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.