1
Hello, what I want to do specifically are radio Buttons that when one is activated the background color of the div skirt of the default color (white) and change its background color according to the radio buttom, and save this change in the database referring to such color, without having to submit any form type through a submit button. It got a little confused, so, below is an example:
I have the radio buttom options: opc1, opc2, opc3
When I activate the opc1 the div that contains the Buttons radio set gets its totally green background, and this is saved in the BD so that whenever I return it turns green and with its proper radio option selected (opc1) and do not lose the change when the page is updated. But if in this same div I decide to click on opc2, the div changes the color and turns orange, indicating something else, thus saving it in the BD tbm in the same way, and overwriting in the BD the Row that was stored the green color, and finally, if the user clicks on opc3, the div will turn red, and also the change will be saved in the BD so that overrides the previous.
I’m new and in the process of learning in Javascript, so I still have some difficulty in this although it seems simple, I’ve searched a lot for it and I didn’t find anything very enlightening, I hope someone here can help me, follows below the two scripts I did (There are two because I tried two ways, but both failed, but I will leave you both to know which way is more correct)
First way:
<body>
<div>
<form method="post">
<label><input type="radio" name="colors" name="green" onClick="changeColor('g')"> Verde <br></label>
<label><input type="radio" name="colors" name="orange" onClick="changeColor('o')"> Laranja <br></label>
<label><input type="radio" name="colors" name="red" onClick="changeColor('r')"> Vermelho <br></label>
</form>
</div>
Script way 1:
function changeColor(color){
var color = document.div.style.backgroundColor;
switch(value){
case 'g':
color = "#6F8456";
break;
case 'o':
color = "#FA7921";
break;
case 'r':
color = "#670000";
break;
}
document.div.backgroundColor = color;
}
Second way:
<div>
<form method="post">
<label><input type="radio" name="colors" name="green" onClick="changeColour('g')"> Verde <br></label>
<label><input type="radio" name="colors" name="orange" onClick="changeColour('o')"> Laranja <br></label>
<label><input type="radio" name="colors" name="red" onClick="changeColour('r')"> Vermelho <br></label>
</form>
</div>
Script Second way:
function changeColour(){
if("g")
document.div.style.backgroundColor="#6F8456";
else
document.div.style.backgroundColor="#ffffff";
if("o")
document.div.style.backgroundColor="#FA7921";
else
document.div.style.backgroundColor="#ffffff";
if("r")
document.div.style.backgroundColor="#670000";
else
document.div.style.backgroundColor="#ffffff";
}
If possible send the code commenting to me try to understand which changes have referred to what, and also do the code in Javascript and NOT in Jquery.
It didn’t work properly, when sent to the BD he doesn’t know how to differentiate the 3 colors from each other, regardless of the radio option I check it saves "color" in the BD, and when updating the page, the selected color goes back to the standard (white)
– Luiz Oleia
@Luizoleia You have to put the
#
before the color code in PHP– Sam
I put and it didn’t work, the difference is that now the field in BD is blank, put the code like this: $status = $_GET['#color'];
– Luiz Oleia
It’s not that.. the GET was right... you have to put the
#
in PHP where you want to put the color in the element, something like this:<div style="background: #<?=cor ?>">
– Sam
The color code has a
#
before the numbers, right? As you are saving in the bank only the numbers, you need to concatenate in the code the number saved in the bank with the#
, being like this:#6F8456
– Sam
Oh yes, I had not understood this from php, but even so it still didn’t work and still sends only the word "color" for BD, in the last hour tried several ways and so far none of them worked
– Luiz Oleia
It looks like the code: <div id="radioCor" style="background: #<? php= $status ? >"> The $status variable is how I referenced the color value in my php upload code that I had already finished
– Luiz Oleia
I think the syntax is wrong. It would have to be like this:
<div id="radioCor" style="background: #<?=$status ?>">
... but look at the page source code and see what’s coming back from PHP...– Sam