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.
How you are getting the database data?
– mcamara
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.
– Alh
You are making the call via AJAX (Asynchronous) or when loading the page (Synchronous)
– mcamara
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/
– Alh
@miltoncamara, amigo, posted a self explanatory image of what I need, the code can take from jsfiddle as an example.
– Alh
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!
– mcamara
Let’s go continue this discussion in chat.
– mcamara
Ahhh another detail, it’s not working on Fiddler because you’re not loading jQuery! ;)
– mcamara
Check out this link I made https://jsfiddle.net/eo8ke6sg/
– mcamara
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.
– Alh
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())
– mcamara
USING THE ONLOAD FUNCTION. THANK YOU
– Alh