2

I have a page with list of buttons, when each button is clicked, it's value is captured and ajax call in made. PHP does DB updates on ajax call. I want to return data to ajax call. The data is obtained from DB. But I'm unable to point out what's the error in below code.

Here is PHP code:

if (isset($_GET['val'])) 
{

    $chapter_id=$_GET['val'];
    $sql= "SELECT file_name,edit_link,name,email FROM `chapter_list` where chapter_id='$chapter_id'";
    $result = mysql_query($sql,$rst);
    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        $vol_name = $row["name"];
        $vol_email= $row["email"];
        $vol_link=  $row["edit_link"];

    } 

    $update=mysql_query("UPDATE `chapter_list` SET `status` = '$update_status' WHERE `chapter_list`.`chapter_id` = '$chapter_id';");

      header('Content-Type: application/json');
    echo json_encode(array("name"=>$vol_name,"email"=>$vol_email,"link"=>$vol_link));

}

Here is the AJAX request

$(document).ready(function(){
  $('.btn').click(function(){
    var clickBtnValue = $(this).val();

    $.ajax ({
      url: '',
      data: { val : clickBtnValue},
      dataType:'JSON',
      success: function(res) {
        alert(res.name);
      }
    });
  });
});

I'm not getting the alert!

Weak to Enuma Elish
  • 4,622
  • 3
  • 24
  • 36
Dee
  • 49
  • 5
  • i dont know if it was on purpose or not, but your url is empty in the ajax. Im thinking you took out for privacy sake? – Pedro Estrada Mar 10 '16 at 01:22
  • Have you tried putting `console.log(res)` in your success callback? and see if its returning something? – Pedro Estrada Mar 10 '16 at 01:29
  • I have to ask. Where do `$update_status` and `$chapter_id` come from? _Blown in on the solar wind?_ – RiggsFolly Mar 10 '16 at 01:29
  • Why are you fetching a single row from a resultset using a while loop. – RiggsFolly Mar 10 '16 at 01:31
  • And then I have to mention: Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Mar 10 '16 at 01:31
  • There is too much going on in the code you show us that depends upon code you **have not** shown us. I am all for a game of 20 questions, but not on SO. – RiggsFolly Mar 10 '16 at 01:34
  • Perhaps .btn has no value. We can't see that it has. – Popnoodles Mar 10 '16 at 03:54
  • Perhaps your DB query returns nothing. – Popnoodles Mar 10 '16 at 03:55

3 Answers3

1

Try like this. Maybe response data is null.check your php code(query lines).

Here My php code is :

if (isset($_GET['val'])) {
            $vol_name = 'dummy_name';
            $vol_email = 'dummy_email';
            $vol_link = 'dummy link';
          header('Content-Type: application/json');
          echo json_encode(array("name"=>$vol_name,"email"=>$vol_email,"link"=>$vol_link));
          exit;
      }

My javascriptcode is :

<input type="text" class="btn" value="test" />
     <script type="text/javascript">
     if('addEventListener' in document){
         document.addEventListener("DOMContentLoaded", function(e){
             //dom loaded
             $(document).on("click",".btn",function(e){
                 e.preventDefault()
                 var e_val = $(this).val();
                 console.log('my value is :' + e_val);
                 if(e_val){
                     $.ajax({
                       type: "get",
                       dataType: 'json',
                       url: 'here your url or slash',
                       data: { // request e_val
                           val : e_val,
                       }
                     }).done(function(xhr) {
                        //  console.log(xhr);
                         if(xhr.name){
                             alert('response data is '+ xhr.name);
                        }
                     })
                 }
             })
         },false)
     }
     </script>

enter image description here

Hax0r
  • 1,722
  • 4
  • 25
  • 43
0

try this..

while($row = mysql_fetch_assoc($result))
    {
        $vol_name = $row["name"];
        $vol_email= $row["email"];
        $vol_link=  $row["edit_link"];

        $ret[$vol_name]= array(
                               'email'=>$vol_email,
                               'link'=>$vol_link
                        );
    } 

then use in the return statement..

echo json_encode($ret);
kev
  • 33
  • 1
  • 6
-1

You can send parameters in HTML

<button class="btn" atribute_id="21543">Button</button>


$(document).ready(function() {
$('.btn').click(function() {
    var Value_of_Btn= $(this).attr("atribute_id"); <-------
    $.ajax({
        url: '',
        data: {
            val: clickBtnValue
        },
        dataType: 'JSON',
        success: function(res) {
            alert(res.name);

        }
    });
});

});