How to run a php file via a Switch Button with html and css?

Asked

Viewed 415 times

1

Save people!! I’m new in the programming universe. I wonder how to make this button run a file in . php triggered and another file . php when not triggered.

/* Estilo iOS */
.switch__container {
  margin: 30px auto;
  width: 120px;
}

.switch {
  visibility: hidden;
  position: absolute;
  margin-left: -9999px;
}

.switch + label {
  display: block;
  position: relative;
  cursor: pointer;
  outline: none;
  user-select: none;
}

.switch--shadow + label {
  padding: 2px;
  width: 120px;
  height: 60px;
  background-color: #dddddd;
  border-radius: 60px;
}
.switch--shadow + label:before,
.switch--shadow + label:after {
  display: block;
  position: absolute;
  top: 1px;
  left: 1px;
  bottom: 1px;
  content: "";
}
.switch--shadow + label:before {
  right: 1px;
  background-color: #f1f1f1;
  border-radius: 60px;
  transition: background 0.4s;
}
.switch--shadow + label:after {
  width: 62px;
  background-color: #fff;
  border-radius: 100%;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.3);
  transition: all 0.4s;
}
.switch--shadow:checked + label:before {
  background-color: #8ce196;
}
.switch--shadow:checked + label:after {
  transform: translateX(60px);
}
<div class="switch__container">
  <input id="switch-shadow" class="switch switch--shadow" type="checkbox">
  <label for="switch-shadow"></label>
</div>

1 answer

1

You must request the file PHP via Ajax when changing the value of switch.

You can use:

<input id="switch-shadow" name="switch-btn" class="switch switch--shadow" type="checkbox">

And in the JQuery:

// quando estado do switch for alterado
$('input[name = switch-btn').change(function()
{
    // variavel indica script PHP a ser executado
    var pagina = '';

    // se está ligado seta um script, se nao seta outro
    if($(this).is(":checked")) 
        pagina = "ligado .php";
    else
        pagina = "desligado.php"

    // AJAX faz requisição no script, caso nunca usou ajax, saiba
    // que ele aguada um retorno do PHP, seja um echo ou algo assim
    $.ajax
    ({
        url: pagina, // script PHP
        type: 'POST ou GET', // metodo (caso tenha de enviar dados ao script)
        data:  seus-dados, // dados a enviar, caso tenha
        mimeType:"multipart/form-data", // caso for enviar arquivo
        contentType: false,
        cache: false,
        processData:false,
        success: function(data, textStatus, jqXHR)
        {
            // se houve sucesso ao contatar pagina php ou coletar retorno
            // a variavel data é o seu retorno do PHP
        },
        error: function(jqXHR, textStatus, errorThrown) 
        {
            // Em caso de erro
        }          
    });
});
  • Note that it wants to perform 2 types of functions, one when activated and the other when disabled. "one file in . php triggered and another file . php when not triggered".

  • 1

    Yes yes, I just added in reply, thanks for the feedback.

  • Already appreciate the help, I tried to do here but it didn’t work. I think I did something wrong. I put your code in index.html and the files in . php on the local server, but the files are not loaded.

  • How did you put in . php? I didn’t put php code in the answer, the first snippet of code is the button, the second is the javascript which goes to the bottom of the html page, I suggest you do more research on the subject as this is part of the basics.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.