0

I'm trying to enable a button based on a query result using php/mysql and jQuery:

$query = mysql_query("SELECT definition, rating FROM tbl_disease WHERE disease = '".$disease."' ;  ")

The button should be enabled based on the value of 'rating':

<input type="button" name="book" id="book" class="login login-submit"  align="center" value="Book Appointment">

I know how to enable/disable buttons using jQuery so was wondering how I would access the 'rating' value from the query using jQuery and enabling/disabling the button based on that?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sabelo Ngcobo
  • 33
  • 1
  • 7
  • Do you display the button after the query is executed? Or can the rating change while the button is already being display? – nils Sep 01 '14 at 17:08
  • You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Quentin Sep 01 '14 at 17:29
  • And you’ve got an SQL injection vulnerability. – icktoofay Sep 01 '14 at 18:35
  • The button is displayed after the query is executed and the rating can not change unless the user goes back to run the query again. – Sabelo Ngcobo Sep 03 '14 at 14:18

3 Answers3

1

Do an Ajax call, In back end execute the query return response. Based on the response you can handle it in jQuery.

$.ajax({
  type: 'POST',
  url: "/updateFunction",
  data: {
    records: "disease"
  },
  success: function(result) {
    alert(result)
    // do your stuff here           
  }
});
Ende Neu
  • 15,581
  • 5
  • 57
  • 68
teshvenk
  • 403
  • 3
  • 8
0

What's the problem?

<?php
  $q = mysqli_query($db_connection, "SELECT definition, rating 
                                   FROM tbl_disease WHERE disease = $disease");
  if (mysqli_num_rows($q)>0) 
    $r = mysqli_fetch_assoc($q);
  else
    $r['rating'] = -1;
?>
<!--  ...  -->
<input<?php if ($r['rating']<1) echo ' disabled' // <- or your own condition ?> 
 type="button" name="book" id="book" class="login login-submit" align="center"
 value="Book Appointment">
marek094
  • 424
  • 2
  • 11
0

It's better to use JSON as data format. So in PHP make a regular query and echo json_encode($results), and in javascript use $.ajax({dataType: 'json'...})

Dusan
  • 276
  • 1
  • 16