Skip to main content
added 100 characters in body
Source Link
pti_jul
  • 512
  • 1
  • 6
  • 19

I'm using another way that works for me :

Serialize form data (as specified above, will exclude unchecked'valuescheckboxes' values that are unchecked) :

const formData = $("form[name='config']").serialize();
const data = {
                config: formData
            }

Then, in PHP, I'm building a JSON object :

parse_str($_REQUEST['config'], $configJSON);
$configJSON = json_encode($configJSON);

And in MSSQL, in my stored procedure when a column is missing from the JSON (because it's been filtered out by the jquery serialization) it will be NULL and so will be replaced by 0 (thanks to ISNULL function) :

ALTER PROCEDURE [dbo].[eth_userprofil_updateconfig]
(
    @userId int, 
    @configJSON nvarchar(max)
)   
AS
BEGIN
    SET NOCOUNT ON;
    
    UPDATE dbo.MyTable
    SET MyField = ISNULL(JSON_VALUE(@configJSON, '$.MyField '), 0),
        ....
    WHERE userId = @userId
END

I'm using another way that works for me :

Serialize form data (as specified above, will exclude unchecked'values) :

const formData = $("form[name='config']").serialize();

Then, in PHP, I'm building a JSON object :

parse_str($_REQUEST['config'], $configJSON);
$configJSON = json_encode($configJSON);

And in MSSQL, in my stored procedure when a column is missing from the JSON (because it's been filtered out by the jquery serialization) it will be NULL and so will be replaced by 0 (thanks to ISNULL function) :

ALTER PROCEDURE [dbo].[eth_userprofil_updateconfig]
(
    @userId int, 
    @configJSON nvarchar(max)
)   
AS
BEGIN
    SET NOCOUNT ON;
    
    UPDATE dbo.MyTable
    SET MyField = ISNULL(JSON_VALUE(@configJSON, '$.MyField '), 0),
        ....
    WHERE userId = @userId
END

I'm using another way that works for me :

Serialize form data (as specified above, will exclude checkboxes' values that are unchecked) :

const formData = $("form[name='config']").serialize();
const data = {
                config: formData
            }

Then, in PHP, I'm building a JSON object :

parse_str($_REQUEST['config'], $configJSON);
$configJSON = json_encode($configJSON);

And in MSSQL, in my stored procedure when a column is missing from the JSON (because it's been filtered out by the jquery serialization) it will be NULL and so will be replaced by 0 (thanks to ISNULL function) :

ALTER PROCEDURE [dbo].[eth_userprofil_updateconfig]
(
    @userId int, 
    @configJSON nvarchar(max)
)   
AS
BEGIN
    SET NOCOUNT ON;
    
    UPDATE dbo.MyTable
    SET MyField = ISNULL(JSON_VALUE(@configJSON, '$.MyField '), 0),
        ....
    WHERE userId = @userId
END
Source Link
pti_jul
  • 512
  • 1
  • 6
  • 19

I'm using another way that works for me :

Serialize form data (as specified above, will exclude unchecked'values) :

const formData = $("form[name='config']").serialize();

Then, in PHP, I'm building a JSON object :

parse_str($_REQUEST['config'], $configJSON);
$configJSON = json_encode($configJSON);

And in MSSQL, in my stored procedure when a column is missing from the JSON (because it's been filtered out by the jquery serialization) it will be NULL and so will be replaced by 0 (thanks to ISNULL function) :

ALTER PROCEDURE [dbo].[eth_userprofil_updateconfig]
(
    @userId int, 
    @configJSON nvarchar(max)
)   
AS
BEGIN
    SET NOCOUNT ON;
    
    UPDATE dbo.MyTable
    SET MyField = ISNULL(JSON_VALUE(@configJSON, '$.MyField '), 0),
        ....
    WHERE userId = @userId
END