DMXzone Server Connect Support Product Page

Can I insert multiple values from a <select> formfield into my database table?

Asked 21 Oct 2019 14:45:12
1
has this question
21 Oct 2019 14:45:12 Bruce Wilkie posted:
Hi there

I'm wanting to insert a comma-separated list into a varchar field in my mysql database table.
I've got a <select> tag in the form on the page that is set to multiple to allow selection of multiple values and I'm wanting to add all selected values into a single field in the dabase with commas separating them. And at th beginning I'm wanting to insert an additional default value that isn't included in the select form field.

So the html for the form field I have is as follows:

<select name="areas" multiple id="areas" dmx-bindptions="serverconnectlistareas.data.querylistprojectareas" optiontext="project_area" optionvalue="project_area">
</select>

The options are populated from another table, but when looking at the source of the published web page you'll see the following:

prntscr.com/pmad95

And I'm wanting to (for example) insert the following into a varchar field in the database table: "unfiltered,Tyneside,Teesside"

(Note that 'unfiltered' is the default value that I'd like to add at the beginning of the comma separated values text string).

Is this possible and if so, how do I do it?

thanks in advance
Bruce

Replies

Replied 24 Oct 2019 07:53:15
24 Oct 2019 07:53:15 Teodor Kuduschiev replied:
Hello,
You can collect the selected multiple values when submiting the form, so:

1. Change your select name to name="areas[]" -> it needs square brackets there
2. On the server side where you use its value, use the join formatter:

{{$_POST.areas.join(",", "")}}


3. Not sure what's the purpose of the undefined thing, but you can just add it to the expression:
undefined, {{$_POST.areas.join(",", "")}}


Replied 24 Oct 2019 09:24:41
24 Oct 2019 09:24:41 Bruce Wilkie replied:
Hi Teodor

I've tried implementing that and it's causing the form execution to hang.
Then I tried using the 'Open in Browser' button to see if there was a clue there and it says 'Formatter Join does not exist'

I've created a stripped down form for testing pursposes.

prntscr.com/pnggbh

prntscr.com/pnggsc

Can you have a look at the screenshots to see if you can see where I've gone wrong?

thanks
Bruce
Replied 24 Oct 2019 09:34:42
24 Oct 2019 09:34:42 Teodor Kuduschiev replied:
can you send a screenshot of the exact error message when you submit the form as described here: www.dmxzone.com/go/32354/debugging-problems-in-dmxzone-server-connect/ (not when running server action in the browser)
Replied 24 Oct 2019 10:00:55
24 Oct 2019 10:00:55 Bruce Wilkie replied:
Hi Teodor

I'm not seeing any errors in the Console tab on page load or form submit.

I've duplicated my test form on another domain that you can have a look at directly if that will help:

www.adsdata.co.uk/dmx-test/test-select-insert.htm

adsdata.co.uk/dmx-test/dmxConnect/api/testselectinsert.php

Bruce
Replied 24 Oct 2019 10:04:34
24 Oct 2019 10:04:34 Teodor Kuduschiev replied:
Well following the tutorial i sent, there is an error, you can also see it (if you follow the tutorial)
prntscr.com/pnh4mq

Do you have the server data formatter installed in your DW?
Replied 24 Oct 2019 10:27:37
24 Oct 2019 10:27:37 Bruce Wilkie replied:
Ah. I can see that in the Network tag now. I was looking in the Console tag.

No I don't think I have Server Data Formatter installed. Is that another extension?
Replied 24 Oct 2019 10:31:12
24 Oct 2019 10:31:12 Teodor Kuduschiev replied:
Well yes, it is required if you want to format the data like this.
Replied 24 Oct 2019 10:37:34
24 Oct 2019 10:37:34 Bruce Wilkie replied:
Oh, ok.

What if I just want to put the raw data, unformatted from a multiple select field into the database? I've tried just using {{$_POST.numbers}} when the form field's name is set to 'numbers' but that only puts one of the selected values into the database field. And then I tried the same but with the form field's name set to 'numbers[ ]' and that just hung the form again.

It works when sending the section field's value(s) in an email but not when entering into a database.

Iin the email it sends the value {{$_POST.numbers}} as, for example: ["One","Three"] and that would be fine if I can get that into the database as once there I can manipulate it later on.

So can you let me know how to do this just with Server Connect? I just need the values into the database from the select field in pretty much any format so long as the values are there.
Replied 24 Oct 2019 11:29:57
24 Oct 2019 11:29:57 Teodor Kuduschiev replied:
It's not as simple as it might sound to you.
This is an array, and arrays need to be formatted in the way i explained above in order to achieve what you explained in your post i.e. - " to insert a comma-separated list into a varchar field in my mysql database table."

If you don't want to insert them like that (comma-separated list into a varchar field) but as separate records, then you can repeat the $_POST (its name must include [] always as it is an array):
prnt.sc/pnigwk

And then use the insert step inside the repeat, adding the {{$value}} as a value for the inserted record:
prnt.sc/pnigo4

Again, the data formatter is required for what you asked in your first post.

Reply to this topic