0

I can't find documentation with this, but is there a way I can format a button name to have multiple words with spaces to match up to a database value?

For instance, if a certain button is clicked I want to pass it's name west wing to a function that will look for the value west wing in the database.

<button class="uk-button" name="west_wing">

Is there a way I can format this to make the button name legitimately west wing ?

Rakesha Shastri
  • 11,053
  • 3
  • 37
  • 50
Whisou138
  • 451
  • 5
  • 21
  • Just enter it as `name="west wing"`? Spaces are allowed in the name attribute ( https://stackoverflow.com/questions/3424860/what-characters-are-allowed-in-the-html-name-attribute-inside-input-tag ) if you're having a problem submitting the data, that's a different question and we'll need to see more of your solution. – DBS Sep 04 '18 at 14:13
  • Inside your function you can replace the underline `var str = "west_wing"; var res = str.replace("_", " ");` – Alvaro Alves Sep 04 '18 at 14:13
  • Might be better to use a `data` attribute: ` – J. Titus Sep 04 '18 at 14:14

5 Answers5

4

There is no requirement that the name not have spaces:

<button class="uk-button" name="west wing">Click</button>

… but this would probably be easier if you used the value instead:

<button class="uk-button" name="foo" value="west wing">Click</button>

Then you don't need to search through the list of possible names in your server-side code. You can just read it (this example uses PHP since you didn't specify what you were using and PHP is very common):

$clicked_button = $_POST['foo'];
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

You could make your life easier and stick with javascript.


var btnname = "west_wing";
var legit_name = btnname.replace("_", " ");

If your name might have more _ or spaces, you can use the global replace


var btnname = "west_wing_plus";
var legit_name = btnname.replace(/_/g, " ");
Sparky
  • 287
  • 1
  • 11
1

Is there a reason you don't want to just write name="west wing"?

I would go with this approach, as it's the simplest and cleanest unless you've got a reason not to do it this way.

acd37
  • 532
  • 1
  • 7
  • 14
  • Here's a simple codepen showing this should work just fine: https://codepen.io/anon/pen/oPwzJZ?editors=1111 – mikeg542 Sep 04 '18 at 14:17
  • Thank you, I wasn't sure if there would be an issue with doing that but this confirmed that it would have been fine to begin with so I'll continue this way. Just wanted to keep my bases covered – Whisou138 Sep 04 '18 at 14:37
  • Yw. Let me know if you have anything else. Happy to help. – acd37 Sep 04 '18 at 19:26
1

If it is a JavaScript code, write this, to have transformed string in west_wing:

var str = "west_wing";
var str = str.replace(/_/g, " ");

also, you can do it in less efficient, but more funky, way, without using regex:

var str = "west_wing";
var str = str.split("_").join(" ");
Alvaro Alves
  • 308
  • 1
  • 3
  • 13
0

I can't find documentation with this

Button element definition can be found here:

https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-34812697

name of type DOMString Form control or object name when submitted with a form. See the name attribute definition in HTML 4.01.

Name attribute definition can be found here

https://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#adef-name-BUTTON

name = cdata [CI] This attribute assigns the control name.

Documentation about type CDATA on HTML elements and attributes can be found here: https://www.w3.org/TR/1999/REC-html401-19991224/types.html#type-cdata

The CDATA CDATA is a sequence of characters from the document character set and may include character entities. User agents should interpret attribute values as follows: Replace character entities with characters, Ignore line feeds, Replace each carriage return or tab with a single space. User agents may ignore leading and trailing white space in CDATA attribute values (e.g., " myval " may be interpreted as "myval"). Authors should not declare attribute values with leading or trailing white space.

Short Answer

I think you can use the name "west wing" like this

<button class="uk-button" name="west wing">

But maybe you want to use a button like this:

<button class="uk-button" name="choice" value="west wing">West wing</button>
<button class="uk-button" name="choice" value="Another option">Another option</button>

Then you can get the value after submit, like this:

Depends on form method:

$_POST['choice'];

or

$_GET['choice']
khoekman
  • 1,153
  • 7
  • 16