Select saved shows inputs through ID

Asked

Viewed 50 times

1

I have a SELECT that opens certain INPUTS.

FUNCTION TAKING THE SELECT ID AND OPENING INPUTS.

     //Mostra div dos inputs do ipi 
  $('#selectProduto').change(function() {
    if ($('#selectProduto').val() == '1') {
      $('#nome_produto').show();
      $('#preco').show();
      $('#descricao').show();

    }
    if ($('#selectProduto').val() == '2') {
      $('#nome_produto').show();
      $('#preco').show();
      $('#descricao').show();

    }
      });

I NEED TO LOAD THE ID I GET VIA GET BY PHP:

 $id_produto =   $_GET['id']; 

https://jsfiddle.net/stfn5168/

SEE THE IMAGE EXPLAINING:

inserir a descrição da imagem aqui

  • How you are getting the database data?

  • I get the ID via get, (example product.php?ID=1), then fill in the fields via SELECT Where id_product=1, then I show the data of select equals the product ID, missing it start already with the inputs that are div display:None, turn display:block, the parameter being the selected ID in select.

  • You are making the call via AJAX (Asynchronous) or when loading the page (Synchronous)

  • See the code in Jsfiddle, only it doesn’t work in fiddel, here tested and works, but you can see the code.. code is simple.. https://jsfiddle.net/stfn5168/

  • @miltoncamara, amigo, posted a self explanatory image of what I need, the code can take from jsfiddle as an example.

  • Try to do the following, in the onload of the page, you read the selected value in your select and call the function to display the inputs. I’ll make an example in JSFIDDLER!

  • Ahhh another detail, it’s not working on Fiddler because you’re not loading jQuery! ;)

  • Check out this link I made https://jsfiddle.net/eo8ke6sg/

  • yours didn’t work in jsfiddle either, but what I need is for you to open the INPUTS in the ONLOAD that matches that ID, as in the js example.

  • Strange that it didn’t work, here it is working, only if the behavior expected by you is not what I thought! What I did, in the page load event (When page loads) I check the ID of the selected item in SELECT and then make the comparisons showing the fields on the screen with the SHOW method ($(ELEMENT). SHOW())

  • USING THE ONLOAD FUNCTION. THANK YOU

Show 7 more comments

1 answer

0

Do it like this: When loading the page run an ajax by posting the id to a php script, then retrieve the id value in your php by the variable $_GET['id'], inside it make a select in the database filtering by the id sent, finally load the information and print the reply in json.

In your html :

<input type="hidden" id="produto_id"  value="<?php echo $_GET['id']; ?>" />

In your javascript do:

$(document).ready(function (){ 
    $.get("loadValue.php",{id : $('#produto_id').val()},function(data,status){
        if(data == 'success'){
            var arr = $.parseJSON(data);
            $('#nome_produto').val(arr.nome);
            $('#descricao').val(arr.texto);
        }
    });
});

$ (Documento).ready()

A page cannot be handled safely until the document is "ready". Jquery detects this state of readiness for you. The code inside

$( document ).ready()

will only be executed once the Document Object Model (DOM) page is ready for the Javascript code to execute. The code included inside

$( window ).on("load",function() { ... })

will be executed once the full page (images or iframes), not just DOM, is ready.

Now if you just want to show the input text fields when loading the page, the answer is above also serves. Simply load the id coming for get into a field of type Hidden then using Document.ready to display the text fields if the value attribute has value.

  • As it is already on the edit page only need the onload!

  • Well, I had understood that it only has the product id without name and description.

  • True, perhaps you didn’t understand this detail!

  • Wait for him to manifest.

  • hello, this is inside the edit page that comes with the id by get..

  • accurate: ONLOAD checks the ID it receives from $GET_["id"] and CHECKS WHICH SELECT is selected AND SHOWS THE INPUTS SHOW(); .

  • Then, create a field of input type Hidden when opening the editing page, load the id value by get into the input value attribute, then the above routine runs after reading the html page will send a get request to the backend to collect the information and reload it in your input text field.

Show 2 more comments

Browser other questions tagged

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