Get values jQuery form plugin using JSON

117 views Asked by At

I use jQuery plugin called AJAX form for use in my forms and use this with JSON, http://malsup.com/jquery/form/ , actually i want reload the form after get result and send values for reload the form, or get finally these values from the form when submit but the problem i don´t know how get this

The Script in the index.php :

<script>
jQuery(document).ready(function() { 
  jQuery('#forma_config').ajaxForm({ 
    dataType: 'json', 
    success: configto
  });
});

function configto(datad) {  
  if (datad.datoscreate=="ok") {    
    alert("All saved ");    
  }   
}

Form in the index.php :

<form action="send.php" name="formasend" id="form_config">
  <input type="text" name="Phone" value="">
  <input type="hidden" name="idform" value="2">
  <input type="hidden" name="send" value="ok">
  <input type="submit" name="Sending" value="Send Now">
</form>

And in send.php I recieive information for process :

<?php
  print '{"datoscreate":"ok"}'; 
?>

The problem it´s I need get value of idform from input hidden for example for reload the same information, and i don´t know how get it for reload right from JSON or using this plugin, if i don´t know this idform , the form don´t reload because idform it´s missing, and don´t load information right

Thank´s in advanced for the help, regards

1

There are 1 answers

0
Özgür Can Karagöz On

code seems to be lacking in some things.

// wrong selector #forma_config
// correct selector #form_config

Second you need to send the data. In your code it does not look anything like that.

jQuery('#form_config').ajaxForm({
    data : jQuery('#form_config').serialize(),
    // dataType: 'json',
    // If you use this feature, it is necessary to specify header json on php.
    success: function(datad) {
       var data = jQuery.parseJSON(datad);
       // paralyzed the data.
       if(data.datoscreate == 'ok') {
          alert("All saved ");
       }
    }
});

Finally, the use of PHP's wrong.

header('Content-Type: application/json');

$json['datoscreate'] = 'ok';

print json_encode($json, JSON_UNESCAPED_UNICODE);

I did not try the code. I try to help if you provide feedback.