Two excluding fields

0

I have two small test files: 1 testEs.html and 2 testprocessEs.php. In file 1 I have two exclusive fields: if LanguageB is chosen, Art or Music can not be chosen. How can any condition be put in file 2 so that the server does not return an error message in the variable "bildemu" (art or music), since the db does not receive one of the two variables? It seems that it can be done with isset or empty, but I need a practical example. I am very newbie yet. Can somebody help me? Thanks for any idea.

<!DOCTYPE html>
    <head>
    <title>Prueba</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
        <script languaje="javascript">
            function habilita(form)
            {    
    form.bildemu[0].disabled = true;
    form.bildemu[1].disabled = true;
    }
function deshabilita(form)
    { 
        form.bildemu[0].disabled = false;
        form.bildemu[1].disabled = false;
    }
    </script>
</head>
    <body>
        <form action="procesaPrueba.php" method="post"> 
            <p>
            <label for="name">Apellido:</label>
            <input type="text" name="name"required id="name"=>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp
            <!--para introducir comentarios-->
            <label for="vorname">Nombre:</label>
            <input type="text" name="vorname"required id="vorname"></br>
            <i>Elige una lengua-B:</i>
            <input type="radio" name="b_sprache" value="Spanisch B"onClick="habilita(this.form)"> Español B
            <input type="radio" name="b_sprache" value="Italienisch B"onClick="habilita(this.form)"> Italiano B
            <input type="radio" name="b_sprache" value="Französisch B"onClick="habilita(this.form)"> Francés B </br> 
            <i>Para las clases 12/13 me inscribo en las siguientes asignaturas:</i></br>
            <input type="radio" name="bildemu"   value="Arte">
            <label for="Arte">Arte</label> 
            <input type="radio" name="bildemu"   value="Música">
            <label for="Musica">Música,</label>&nbsp; </br>
            <i>y</i>
            <input type="radio" name="ct" required id="ct" value="Computertechnik">
            <label for="ct">Técnica de ordenadores.</label></br>
            </br></br>
            <input type="submit" value="Enviar">
            <input type="reset">

            </br></br>
    </form>
</body>

<?php
     $bildemu=$_POST['bildemu'];
     $b_sprache=$_POST['b_sprache'];
     $name=$_POST['name'];
     $vorname=$_POST['vorname'];
     $ct=$_POST['ct'];
     $servername = "localhost";
     $username = "root";
     $password = "";
     $dbname = "schule3";

     $conn = new mysqli($servername, $username, $password, $dbname);
     if ($conn->connect_error) {
     die("Connection fallida: " . $conn->connect_error);
     } 

     $sql = "INSERT INTO schule3(name, vorname, b_sprache, bildemu, ct)
     VALUES ('$name', '$vorname', '$b_sprache','$bildemu','$ct',)";

     if ($conn->query($sql) === TRUE) {
     echo "das Formular wurde geschickt!";
     } else {
     echo "Error: " . $sql . "<br>" . $conn->error;
     }

     $conn->close();

? >

    
asked by Juan Carlos 27.03.2017 в 22:52
source

1 answer

0

if what you want is to validate the variable bildmenu so that if it goes empty do not send you error or that is what I understand that you want to do, it would be something like that.

$b_sprache=$_POST['b_sprache'];
$name=$_POST['name'];
$vorname=$_POST['vorname'];
$ct=$_POST['ct'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "schule3";

if(empty($_POST['bildemu'])){
$bildemu = '0';

$sql = "INSERT INTO schule3(name, vorname, b_sprache, bildemu, ct)
VALUES ('$name', '$vorname', '$b_sprache','$bildemu','$ct',)";

} else { 
$sql = "INSERT INTO schule3(name, vorname, b_sprache, bildemu, ct)
VALUES ('$name', '$vorname', '$b_sprache','$bildemu','$ct',)";
}

This would be a different way it could be like that.

if(empty($_POST['bildemu'])){

$bildemu = '0';

} else {

$bildemu = $_POST['bildemu'];

}

$b_sprache=$_POST['b_sprache'];
$name=$_POST['name'];
$vorname=$_POST['vorname'];
$ct=$_POST['ct'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "schule3";

$sql = "INSERT INTO schule3(name, vorname, b_sprache, bildemu, ct)
VALUES ('$name', '$vorname', '$b_sprache','$bildemu','$ct',)";

in the first you receive your variables and at the moment of doing the insert it is valid if it is empty that it makes a insert and if not that it does the other, in the second it is the same condition but this time it is valid only If the field is empty, you assign a value to save something in db , I hope it helps you.

    
answered by 28.03.2017 / 00:04
source