With jQuery you can simply use the post
method to request your * .php file to receive the data from it.
You will have to separate your * .php file and answer the client-side with some sort of separate string somehow representing the values of the $nome_cat
and $img_cat
variables. You can answer the client-side with JSON and interpret it with the JSON.parse
method (if you are going to send strings that include quotation marks, declare "\"
before the quotation mark, or simply: $value= str_replace('"', '\"', $value);
).
To answer the client-side you declare echo
or printf
with some argument string in PHP.
Now you can use $.post
to execute the file next to the server and receive a response from it in a callback function, success
in the 1st 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 answer 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.