1
Dear, I have a code that displays a dialog with a table, where after selecting the item, it is sent to an input field, via ajax. I need to use this value in a php function on the same page, so that another dialog displays a table based on this input data. I’ve been analyzing the use of a global jquery variable, however, I’m not being able to make this variable receive the value indicated in the input so that I can use it in the future. Follow the code I use:
Javascript:
var cgc;
$(document).ready(function () {
$(function () {
$(".dialog").dialog({
autoOpen: false,
height: 400,
modal: true,
resizable: false,
width: 700,
open: function (event, ui) {
$('.ui-dialog-titlebar').display("none");
}
});
$(".opener").click(function () {
$('input:checkbox').attr('checked', false);
var nameD = $(this).attr("id");
var toRemove = 'opener';
var idDialog = "#dialog" + nameD.replace(toRemove, '');
$(idDialog).dialog("open");
});
$(".botao").click(function () {
$(".dialog").dialog("close");
});
});
$(".botao").click(function () {
getValueUsingClass(
$(this).attr("id")
);
});
function getValueUsingClass (id) {
var chkArray = [];
$(".chk:checked").each(function () {
chkArray.push(
$(this).val()
);
});
var name = id;
var toRemove = 'bt';
var id = name.replace(toRemove, '');
var val_id = [];
$("input:checked").each(function () {
val_id.push(
$(this).attr("id")
);
});
var selected;
selected = chkArray.join('') + "";
var val_id2;
val_id2 = val_id.join('') + "";
if (selected.length > 1) {
if (chkArray.length > 1) {
alert("Selecione um campo");
$('input[id=' + id + ']').val("");
} else {
$('input[id=' + id + ']').val(selected);
/* Configura a requisição AJAX */
if(id=='Cliente') {
$.ajax({
url: 'dados_pedido.php', /* URL que será chamada */
type : 'POST', /* Tipo da requisição */
data: 'CODIGO=' + $('#Cliente').val(), /* dado que será enviado via POST */
dataType: 'json', /* Tipo de transmissão */
success: function (data) {
if(data.sucesso == 1) {
$('#codigocli').val(data.codigo);
$('#nomecli').val(data.nomecli);
$('#cnpjcli').val(data.cnpjcli);
$('#transpcli').val(data.transpcli);
$('#PagCli').val(data.PagCli);
cgc = data.cnpjcli;
}
}
});
};
return false;
}
} else {
alert("Nenhuma dado selecionado");
}
};
});
HTML and PHP:
<a href="#dialogPed3" id="openerPed3" class="opener">
<img src="../../images/lupa.png" id="lupa3" width="16" height="16" alt = "Add"/>
</a>
<div id="dialogPed3" class="dialog">
<?php
echo "teste"."<script>document.write(cgc);</script>";
?>
</div>
At the beginning of the jquery code there is a declared cgc variable, if I put a value immediately, type var cgc=123, it is normally displayed in php echo, but when its value is changed within "$(Document). ready(Function(){" this nothing happens, the value remains 123.
Well, I don’t know if it helps, but I’ll explain the point. On my page, in a given field, there is a button that displays a dialog with a table, however this table is based on the value that is inserted in the input #cnpjcli, but I did not find a way to pass this value to php at the time I call the function, what I am trying to do is to use the Document.write(cgc) feature; to store in a php variable and then move on to the php function. If you have some other way to take the value of the input and pass to the function, tb will help.
how and where it receives another value? in your code it only appears in the statement and at the time of writing...
– RodrigoBorth
It has a good AJAX stroke in the W3C. The first line and inter:
– Peter
Oops, sorry, I forgot to put the cgc assignment location, I just updated the code.I need it to receive a value from an ajax query.
– Thiago
Every time you stop indenting a line of code, God kills a kitten. Please think of the kittens.
– Oralista de Sistemas
In this case already checked the console to see if it is giving you the return?
– RodrigoBorth
@Renan the Evil God that ein.
– RodrigoBorth
kkk sorry for the mess of the code, I will try and correct the next.
– Thiago
If I use an Alert(cgc); after the assignment, it displays the correct value, but it does not appear when called in echo "<script>Document.write(cgc);</script>";
– Thiago
@Thiago had similar problem and I can’t remember how I solved it, but if I’m not mistaken the global variable will not be affected by you assign a value to it within Success
– RodrigoBorth
I am assigning inside Success because I need the value of the #cnpjcli field, I tried to assign the value outside with the cgc = $('#cnpjcli') line. Val() and also did not work.
– Thiago
@Thiago I found a solution here of how to access it, I will only do the test to be sure and answer your question
– RodrigoBorth
The main problem is to send this variable to php with "<script>Document.write(cgc);</script>"
– Thiago
Concatenate empty string with function return
join
ofarray
is redundant, okay?– Oralista de Sistemas
@Thiago gives a look at the part of global variables, I could not make an example here... https://developer.mozilla.org/pt-PT/docs/Web/JavaScript/Guia/Valores,_Vari%C3%A1veis_e_literais
– RodrigoBorth
Something else:
$(document).ready(function () {/* .. SNIP .. */ })
and$(function () { /* .. SNIP .. */})
are exactly the same thing. You can eliminate one of the two to make your life easier.– Oralista de Sistemas
@Does Thiago know the difference between client-side and server-side? That is, that the Javascript variable runs on the client side and not server side.
– Sergio
Another tip, Thiago, instead of clarifying and putting code here in the comments, prefer [Edit] the question to consolidate the whole problem there. So no one has to read all this to understand what’s going on.
– brasofilo