how to pass a php string through a java script parameter

Asked

Viewed 614 times

1

I’m creating a click counter on the link this way:

<a href="#" onclick="return chamarPhpAjax();"></a>

That by clicking on the link calls the function:

function chamarPhpAjax() {
    var so = "";
    var name = "";
   $.ajax({
      url:'/assets/classes/meuajax.php',
      type: 'post',
      data: { 'so': so, 'name': name }
  });  

  return false;
}

By Ajax, the data is sent via POST to the page "meuajax.php":

<?php
function testeOnclick() {
    $so = $_POST['so'];
    $name = $_POST['name'] ;

     include_once "../config/Config.inc.php";

      $pdo->query("UPDATE '$so' SET downloads = downloads +1 WHERE name = '$name' ");

}
testeOnclick();
?>

The problem is that I cannot receive by parameter the data to put in var so = ""; var name = ""; Ajax. How do I pass the data to onclick, more or less like this:

<a href="#" onclick="return chamarPhpAjax(<?php $dados->so;?> , <?php $dados->name;?> );"></a>

And receive these parameters in Ajax?

  • You can rephrase your question by deleting snippets that don’t work in context?

  • OK I’ll try a better explanation

2 answers

0

solved it that way

function chamarPhpAjax() {
   
    var so  = $('.icon-dwl').attr('data-so');
    var name = $('.icon-dwl').attr('data-name');
   $.ajax({
      url:'/assets/classes/meuajax.php',
      type: 'post',
      data: { 'so': so, 'name': name }
  });  

  return false;
}

0

You are passing the parameters in

<a href="#" onclick="return chamarPhpAjax(<?php $dados->so;?> , <?php $dados->name;?> );"></a>

but its function

function chamarPhpAjax() {

 var so = "";
 var name = "";
 $.ajax({

      url:'/assets/classes/meuajax.php',
      type: 'post',
      data: { 'so': so, 'name': name }
 });

return false; }

Not taking any parameter. Try putting

 function chamarPhpAjax(so,name) {

 var so = so;
 var name = name;
 $.ajax({

      url:'/assets/classes/meuajax.php',
      type: 'post',
      data: { 'so': so, 'name': name }
 });

return false; }

Browser other questions tagged

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