With jQuery you can simply use the method post
to request your *.php file to receive the data from it.
You will have to separate your *.php file and respond to the client-side with some sort of separate string in some way that represents the values of the variables $nome_cat
and $img_cat
. You can respond to the client-side with JSON and interpret it with the method JSON.parse
(if sending strings that include quotes, declare "\\"
before the quotation mark, or simply: $value= str_replace('"', '\\"', $value);
).
To answer the client-side you declare the echo
or printf
with some argument string in PHP.
Now, you can use $.post
to run the file next to the server and get a response from it in a callback function, success
in the first parameter, which occurs when the request is made successively.
$.post({
/* Coloque os dados de postagem e seus valores no objeto `data` */
data: {
codigo: null,
descricao: null,
img_prod: null,
nome_cat: null,
titulo: null
},
success: function(reply) {
var data = JSON.parse(reply),
$link = $('.cat-link');
$link.attr("title", data.nome_cat);
var catImage = $link[0].getElementsByTagName("img")[0];
catImage.setAttribute("title", data.nome_cat);
catImage.src = "img_cate/" + data.img_cat;
},
url: "script.php"
});
In PHP, you could respond to the client-side in this way if you use JSON:
echo '{'.
'"codigo":'.$codigo.
'"descricao":'.$descricao.
'"img_prod":'.$img_prod.
'"nome_cat":'.$nome_cat.
'"titulo":'.$titulo.
'}';
With this, you will have to ignore the use of PHP in some cases.
Hello @Klaider Klai, if I post the code I am using would have as you adapt it... because I am very lay in javascript.
– Murilo Cabral
@Murilocabral If you can, put no images, so it will facilitate the response.
– Klaider
I’ll insert a reply with the full code, thanks?
– Murilo Cabral
@Murilocabral Well, I think it would be kind of broad to put the whole code in the answer, it would be simpler to explain in comments. What can’t you understand? (ps: I think the codes in the question already help)
– Klaider
Now it’s gone... Rsrsrsrsr... But where do I insert these tips @Klaider Klai?
– Murilo Cabral
@Murilocabral That example of
$.post
would go to the event block from where you click to update the category (I think), already that code in PHP where you define the variables$nome_cat
and others, it sits in a separate *.php file on the server (gave the name of"file.php"
in the js script) and he has to return these variables as a response to js with some sort of organization (I used JSON syntax, which is easy).– Klaider
Let’s go continue this discussion in chat.
– Murilo Cabral