I want to post a logical value with ajax
Sep 5, 2020
PHP
FuelPHP
Ajax
post
boolean
When posting data asynchronously using ajax, you want to post the value as a logical value (TRUE / FALSE).
Even if it is written normally, it will be treated as a string
type.
Even if you bother to write boolean (true)
etc. when posting, it will not be posted as a logical value.
So, I have to mold the data once somewhere.
This time, I will describe the case with FuelPHP.
<script>
$ ('# send_button'). on ('click', function () {// When the button is pressed
var formdata = new FormData ($ ('# main_form'). get (0)); // Get data from form tag
function () {
$ .ajax ({{
type:'POST', // method specification
url:'<? php echo $ url;?>', // Post URL
dataType:'json',
data: formdata,
cache: false,
contentType: false,
processData: false
});
}
});
</script>
$ data = \ Input :: post (); // Get the data to post
if ($ data ['true_or_false'] ='true') {
$ data ['true_or_false'] = true; // Overwrite with logical value
} elseif ($ data ['true_or_false'] ='false') {
$ data ['true_or_false'] = false; // Overwrite with logical value
}
// Throw $ data to the process you want to do
By the way, this time I was writing a process to throw to an external API, and I worked hard on the view to arrange the shape of the data to be posted, but in the end I ended up playing with the controller, so what I can do with the controller and model is there I really felt like doing it.