Set which value will be stored in a PHP variable to be saved to the Database

Asked

Viewed 47 times

0

I have an HTML page where the user selects the information he needs and stores it in the database when he completes the operation. It is a small form in which there are practically only check boxes, except a single text box. In one of the check-boxes (problem type), when the user clicks on the "Machines" option, another select appears, in the case of machines. And if the user chooses the "Other" option, then it will open the only text box on the page. Follow the current code.

HTML:

<select name="tipobroblema" id="tipoproblema">
    <option value="">- Tipo Problema -</option>
    <option value="maquina">Maquina</option>
    <option value="vazamento">Banheiro</option>
    <option value="arcondicionado">Ar Condicionado</option>
    <option value="outro">Outro</option>
</select>

<div id="divOutro" style="display:none;"><input type="text" name="outro" /></div>

<select id="divMaquina" name="maquina" style="display:none;">
    <option value="">- Maquinas -</option>
    <option value="maquina1">Maquinas1</option>
    <option value="maquina2">Maquinas2</option>
    <option value="maquina3">Maquinas3</option>
    <option value="maquina4">Maquinas4</option>
</select>

Script:

<script>                        
    $('#tipoproblema').change(function(){
    if(this.value == 'maquina') {
        $('#divMaquina').css('display', '');
        $('#divOutro').css('display', 'none');}
    else if(this.value == 'outro') {
        $('#divMaquina').css('display', 'none');
        $('#divOutro').css('display', '');}
    else {
        $('#divMaquina').css('display', 'none');
        $('#divOutro').css('display', 'none');}
   });
</script>

When the user clicks on "Generate" to record in the database, the data will go to a PHP page, thus using the POST to get the data that was informed by the user. Before adding the check-box and the "interactive" text (which appear depending on the selected option). I used only:

$problema = $_POST['tipoproblema'];

Thus, taking the value that the user selected and passing to a variable.

Since it was only one, there was no difficulty. He looked for the name of the field, in the case "typoproblema" took the selected value and stored it in the Database. Now there are three possibilities. That is, the way it was, it would continue to store the types of problems in the bank (such as the value "other" and "machines" instead of saving the selected machine or the text typed by the user). I don’t know much about PHP so far, I would really appreciate your collaboration.

1 answer

0


When you send the form to PHP it will create the keys of the array $_POST according to the names of <input>s:

$tipoproblema=$_POST['tipoproblema'];
$outro=$_POST['outro'];
$maquina=$_POST['maquina'];

So you can capture the values of <input>s.

  • Yes, I had already done so. But I do not know if this way is feasible, because then it will complicate to make reports.. I was looking for a solution where I only had one storage. Of which, a "$typoproblema=$_POST['typoproblema'];" already solved. All with just an insert. I hope you understand what I mean

  • It will complicate reporting why?

  • You want to save all information in one field?

  • I say in terms of aesthetics. Often will appear two empty fields

  • only check if the field ta empty, it only puts in the report what you have filled

  • That’s about the size of it. For example, if the value "other" is selected, then it will take the value of the text box and pass to variable to be saved in the Database

  • But if you haven’t filled it will save empty the same way

  • I needed to move to a single variable the real user problem. If the select is as "machine" then it will enable the machine field, the user will select the machine and only the selected machine will be stored in the Database. I need to do so because I have another page to display the problems, which makes a select in the bank, pulling the field "PROBLEM" maybe would have to concatenate the three fields and form a value only, however, would be a mess, as "Machines Machine XFV23", "Another Had a leak in the bathroom X sink Y"

Show 3 more comments

Browser other questions tagged

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