setar background color of radio button via javascript

0

I'm learning php and tried something I do in ASP.NET but it's not rolling. I have a page that receives values from a previous page via _GET. In it I have input of the radio type that I want, that when clicking on a submit, the selected radio turns red. the problem is that by clicking submit, the previously received value is lost, so the page does not load. How to do this without losing the amount received?

The code structure is:

html head body form

Code php:

?php

define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__ . '
<script type="text/javascript">
document.getElementById("submit").onclick = function() {
    var radios = document.getElementsByName("0");
    for (var i = 0; i < radios.length; i++) {
        if (radios[i].checked) {
            $('input:radio').css('background','#000');
            console.log("Escolheu: " + radios[i].value);
        }
    }
};
</script>
<input id="submit" type="submit">
- Apresentação\Controller\PerguntaController.php'); require_once(__ROOT__ . '
?php

define('__ROOT__', dirname(dirname(__FILE__)));
require_once(__ROOT__ . '
<script type="text/javascript">
document.getElementById("submit").onclick = function() {
    var radios = document.getElementsByName("0");
    for (var i = 0; i < radios.length; i++) {
        if (radios[i].checked) {
            $('input:radio').css('background','#000');
            console.log("Escolheu: " + radios[i].value);
        }
    }
};
</script>
<input id="submit" type="submit">
- Apresentação\Controller\PerguntaController.php'); require_once(__ROOT__ . '%pre% -Apresentação\Controller\RespostaController.php'); $Disciplina=$_GET["pergunta"]; $Perguntacontroller = new PerguntaController(); $retorno = $Perguntacontroller->Carregar($Disciplina); $Respostacontroller = new RespostaController(); $x = 0; while ($linha1 = mysqli_fetch_array($retorno)) { $y=0; $Pergunta = $linha1["pergunta"]; $Id[$x] = $linha1["Id"]; $certa[$x]=$linha1["certa"]; $retornoR = $Respostacontroller->Carregar($Id[$x]); echo "$Pergunta<br /><br />"; while ($linha2 = mysqli_fetch_array($retornoR)) { $Resposta = $linha2["Resposta"]; echo"<input type=\"radio\" name=$x value=$y >$Resposta <br /><br />"; echo"<input type=\"hidden\" name=$x value=$certa[$x]>"; $y++; } $x++; } ?>
-Apresentação\Controller\RespostaController.php'); $Disciplina=$_GET["pergunta"]; $Perguntacontroller = new PerguntaController(); $retorno = $Perguntacontroller->Carregar($Disciplina); $Respostacontroller = new RespostaController(); $x = 0; while ($linha1 = mysqli_fetch_array($retorno)) { $y=0; $Pergunta = $linha1["pergunta"]; $Id[$x] = $linha1["Id"]; $certa[$x]=$linha1["certa"]; $retornoR = $Respostacontroller->Carregar($Id[$x]); echo "$Pergunta<br /><br />"; while ($linha2 = mysqli_fetch_array($retornoR)) { $Resposta = $linha2["Resposta"]; echo"<input type=\"radio\" name=$x value=$y >$Resposta <br /><br />"; echo"<input type=\"hidden\" name=$x value=$certa[$x]>"; $y++; } $x++; } ?>

Java script code:

%pre%     
asked by anonymous 03.03.2017 / 20:58

1 answer

0

By analyzing the posted code, I make three considerations:

1) If you do not have a form tag, you can click on submit the will that is not going anywhere.

2) There is no element with id submit on your page, so your javascript will never run.

3) When a form is submitted with submit, only form element names and their values are sent, css formatting is not sent.

Responding to the title "setar background color via javascript", follows a code snippet:

<input type="button" value="Me clique" onclick="this.style.backgroundColor='#ff0000'">
    
03.03.2017 / 21:43