-5

I have created a javascript function. And the work of that function is to store the value which is selected by the user. Now I want to do that if the user selects the value then that value should be searched inside the database.

<script>
function test(){

   var a=$("#to_artist").val();
    var aakash="<option>Select If you want to change</option><?php
define('DB_HOST','localhost');
define('DB_USERNAME','username');
define('DB_PASSWORD','password');
define('DB_NAME','dbname');

//echo '<script> $("#to_artist").val(); </script>';
$con = mysqli_connect(DB_HOST,DB_USERNAME,DB_PASSWORD,DB_NAME) or die("Unable to connect");
$sql = mysqli_query($con, "SELECT *  FROM album;");
$row2 = mysqli_num_rows($sql2); ?>";
    aakash+="<?php while ($row2 = mysqli_fetch_array($sql)){echo "<option value='". $row2['album_name'] ."'>" .$row2['album_name'] ."</option>" ;} ?>" ;
   $("#to_album").append(aakash);
}
</script>

I want to use Select * from album where artist=a this query inside PHP but i don't know how i can use this can any one help me.

1 Answers1

0

You should understand the difference between the parsing times of js and PHP. PHP is a server side language while js executes on the client's browser. so what you want to do is, you should first create your UI to do the selection. after that pass that value to a php script using a post req ( u can use AJAX but given your knowledge level on this I suggest start from starch.). that means create a simple form using html and pass the selected value using that form's post method

after that take that value(POST value) from the php script and query the database

ALPHA
  • 1,135
  • 1
  • 8
  • 18