how to retrieve values from database and show it in a page using javascript

I have a drop-down field in that I have 2 options(rate high and rate low) when I select rate high it it should display results in descending order or if I select rate low it should display the results in ascending order.I have tried a code that is not working.

my html code is:

Read More
<select id="ddlViewBy"  onchange="order()">
  <option value="high">Rate(high)</option>
  <option value="low">Rate(low)</option>
</select>

my JavaScript code is:

function order() { 
  var e = document.getElementById("ddlViewBy");
  var strUser = e.options[e.selectedIndex].value;

  if(strUser == 'high')
  {
  <?php
  $post->id="10";
  $mylink = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_biddings WHERE job_id = %d ORDER BY client_bill DESC", $post->id), ARRAY_A);
  if($_GET['bid_id'])
  {
    foreach ( $mylink as $row ) 
    {   
    $id= $row['user_id'];
    $nam=$row["name"];
    $stat=$row['state'];
    $cnty=$row['country'];
    $desc=$row["description"];
    $bid=$row['id'];
    $dat=$row['submission_date'];
    echo '<a href="../../wp-content/themes/businessfinder/profile_register.php?id=$id&view" target="_blank">';
    echo "<div class='mr-btm' style='width:98%'>";
    echo "<h3 style='text-transform:uppercase; line-height:0'><b>$name</b></h3>";
    echo "<p style='color:#808080; text-transform:uppercase;'>$stat,$cnty</p>";
    echo "<p style='color:#808080;'>$desc</p>";
    echo "<date style='color:#35cbc9;'>categories : </date>";
    echo "<date style='color:#808080;'>";
    if($row["plumbing"] != '')
    echo $row["plumbing"]; 
    if($row["electrical"] != '') 
    echo $row["electrical"];
    if($row["heating"] != '')
    echo ",".$row["heating"]; 
    if($row["ventilation"] != '')
    echo ",".$row["ventilation"]; 
    if($row["flooring"] != '')
    echo ",".$row["flooring"]; 
    echo ".</date>";

    echo "<table class='btm-cm'><tr><td>";
    echo "<date style='color:#35cbc9;'> Id : </date><date style='color:#808080;'>$bid</date> </td> <td style='color:#35cbcp;'>  | </td><td>"; 
    echo "<date style='color:#35cbc9;'> submitted on:  </date><date style='color:#808080;'>$dat</date></td></tr></table></div></a>";
    }
  }
  else
  {
  echo "nothing found";
  }
}
?>
}
else if(strUser == 'low')
{
<?php
$post->id="10";
$mylink = $wpdb->get_results($wpdb->prepare("SELECT * FROM wp_biddings WHERE job_id = %d ORDER BY client_bill ASC", $post->id), ARRAY_A);
  if($_GET['bid_id'])
  {
    foreach ( $mylink as $row ) 
    {   
    $id= $row['user_id'];
    $nam=$row["name"];
    $stat=$row['state'];
    $cnty=$row['country'];
    $desc=$row["description"];
    $bid=$row['id'];
    $dat=$row['submission_date'];
    echo '<a href="../../wp-content/themes/businessfinder/profile_register.php?id=$id&view" target="_blank">';
    echo "<div class='mr-btm' style='width:98%'>";
    echo "<h3 style='text-transform:uppercase; line-height:0'><b>$name</b></h3>";
    echo "<p style='color:#808080; text-transform:uppercase;'>$stat,$cnty</p>";
    echo "<p style='color:#808080;'>$desc</p>";
    echo "<date style='color:#35cbc9;'>categories : </date>";
    echo "<date style='color:#808080;'>";
    if($row["plumbing"] != '')
    echo $row["plumbing"]; 
    if($row["electrical"] != '') 
    echo $row["electrical"];
    if($row["heating"] != '')
    echo ",".$row["heating"]; 
    if($row["ventilation"] != '')
    echo ",".$row["ventilation"]; 
    if($row["flooring"] != '')
    echo ",".$row["flooring"]; 
    echo ".</date>";

    echo "<table class='btm-cm'><tr><td>";
    echo "<date style='color:#35cbc9;'> Id : </date><date style='color:#808080;'>$bid</date> </td> <td style='color:#35cbcp;'>  | </td><td>"; 
    echo "<date style='color:#35cbc9;'> submitted on:  </date><date style='color:#808080;'>$dat</date></td></tr></table></div></a>";
    }
  }
  else
  {
  echo "nothing found";
  }
}
?>
}
}
</script>

Related posts

Leave a Reply

1 comment

  1. Think of the php as being the author of a letter which is then placed in an in an envelope and snail mailed to the browser. The browser opens it, reads it, and runs the instructions. The author of the letter can’t add more content once its been posted.

    In other words PHP is executed serverside, javascript is executed clientside

    You’ll want to look into ajax.

    Here is the manual to get you started