How to get values from multiple names and insert in database?
How to get values from multiple names and insert in database?
robertutzu
Posts: 11Questions: 1Answers: 0
I want to insert in database values from other fields/names
For example i want to insert the value from field "parcela", "zona", "rand" and "loc" to "idunic" for example
Parcela value is 5
Zona value is B
Rand value is 22
Loc value is 563
I want to insert in database field "idunic" with the value "5-B-22-563"
So i have this:
<?php
/*
* Example PHP implementation used for the index.html example
*/
// DataTables PHP library
include( "../php/DataTables.php" );
// Alias Editor classes so they are easy to use
use
DataTables\Editor,
DataTables\Editor\Field,
DataTables\Editor\Format,
DataTables\Editor\Join,
DataTables\Editor\Upload,
DataTables\Editor\Validate;
// Build our Editor instance and process the data coming from _POST
Editor::inst( $db, 'loc' )
->fields(
Field::inst( 'parcela' ) ->options( 'parcele', 'parcela', 'parcela' ) ->validator( 'Validate::notEmpty' ),
Field::inst( 'zona' ) ->options( 'zone', 'zona', 'zona' ) -> validator( 'Validate::notEmpty' ),
Field::inst( 'rand' ) -> validator( 'Validate::notEmpty' ),
Field::inst( 'loc' ) -> validator( 'Validate::unique' ),
Field::inst( 'nord' ) -> validator( 'Validate::notEmpty' ),
Field::inst( 'sud' ) -> validator( 'Validate::notEmpty' ),
Field::inst( 'est' ) -> validator( 'Validate::notEmpty' ),
Field::inst( 'vest' ) -> validator( 'Validate::notEmpty' ),
Field::inst( 'idunic' ) -> validator( 'Validate::notEmpty' ) //here i want to be inserted value from parcela-zona-rand-loc, it would look like a code for example 6-B-22-547
)
->where( $key = 'loc', $value = '', $op = '!=' )
->process( $_POST )
->json();
and the javascript:
maybe i could put it like a default value, but i dont know how to parse the values from parcela-zona-rand-loc
<script>
var editor; // use a global for the submit and return data rendering in the examples
$( document ).ready(function() {
editor = new $.fn.dataTable.Editor( {
ajax: "ajax/loc.php",
table: "#loc",
fields: [
{label: "Parcela:", name: "parcela", type: "select"},
{label: "Zona:", name: "zona", type: "select"},
{label: "Rand:", name: "rand"},
{label: "Loc:", name: "loc"},
{label: "Nord:", name: "nord"},
{label: "Sud:", name: "sud"},
{label: "Est:", name: "est"},
{label: "Vest:", name: "vest"},
{label: "Id Unic:", name: "idunic", def: "parcela-zona-rand-loc" }
]
} );
var table = $('#loc').DataTable( {
ajax: {
url: "ajax/loc.php",
type: "POST"
},
processing: true,
serverSide: true,
columns: [
{ data: "parcela" },
{ data: "zona" },
{ data: "rand" },
{ data: "loc" },
{ data: "nord" },
{ data: "sud" },
{ data: "est" },
{ data: "vest" },
{ data: "idunic"}
],
select: true
} );
// Display the buttons
new $.fn.dataTable.Buttons( table, [
{ extend: "create", editor: editor },
{ extend: "edit", editor: editor },
{ extend: "remove", editor: editor },
{ extend: 'collection', text: 'Export', buttons: ['copy', 'excel', 'csv', 'pdf', 'print']}
] );
table.buttons().container()
.appendTo( $('.col-sm-6:eq(0)', table.table().container() ) );
} );
</script>
I apreciate any help !
This discussion has been closed.
Replies
Hi,
It sounds like you need to use a get formatter which can be used to pre-process the data submitted by the client. Another option might be to use a server-side event but I think a formatter would probably be best here.
Allan
Thx a lot allan, after a little digging i found the answer
Excellent - good to hear your got it sorted!
Allan
Ok, sorted that out but now the unique doesnt work, it doesnt validates that field "idunic" is unique.
Any help is apreciated.
The issue there is that the validation is done on the data before the formatter is executed (the formatter assumes that valid data is received).
You would need to use a server-side event if you want to add such a validator.
Allan
Ok i went on Events but still didnt works, i think i did something wrong but i dont know what...
ok i got it working this way:
create works as expexted but edit makes error:
TypeError: o.data[0] is undefined
The event method should have worked - are you using 1.5.3 of the PHP libraries? If so, I'll try to replicate the issue locally.
Allan
i have 1.5.2, ill put soon the full code
edit:// updated to 1.5.3 still doesnt validate
ps: just to let you know, you are the BEST!!!
php code:
javascript code:
and if add preEdit gives error"
errors:
The arguments are wrong here. It should be:
See the PHP events documentation for the full list of parameters and their description.
Allan
i made the changes and nope validate still doesnt works, please check
php script:
Yes - that isn't going work either since the value won't have been created at the point the validator is run.
Sorry to lead you up the garden path here - it sounds like client-side code is going to be the best option here if you want a unique validator - specifically use
preSubmit
:That will submit the required
idunic
value which can then be validated like any other field.Allan
just beautiful, works amazing
many thanks Allan