1

i have two dropdown/select input in my services page one for the type of services and the other is the price of every services each is sent from mysql How do I make the value of the price change automatically to whatever data is connected to the the services type what is want For example selection ,writing a book it will show the price of it from db

when i try it with jquery i can't give any option value because it comes from db both

<label style="margin-left:20px; padding: 0 20px;
    margin-bottom: 39px;">what we do for you  </label><select name="topic"   Required="Required">
<?php
// Make the connection:
$connection = mysqli_connect ('localhost', '#', '#', '#');
// If no connection could be made, trigger an error:
if ($connection->connect_error)
{
    die("Database selection failed: " . $connection->connect_error);
}
# Set encoding to match PHP script encoding.
mysqli_set_charset($connection, 'utf8');
$query = "SELECT * FROM services";
$select_service = mysql_query($query);
while ($row = @mysql_fetch_assoc($select_service)) {
echo "<option value=\"".$row['id']."\">".$row['title']."</option>\n  ";
}
?>
</select><br>

<label style="    margin-left: 136px;
    margin-bottom: 30px;     padding: 0 20px;">price</label><select disabled name="topic"   Required="Required">
<?php
// Make the connection:
$connection = mysqli_connect ('localhost', '#', '#', '#');
// If no connection could be made, trigger an error:
if ($connection->connect_error)
{
    die("Database selection failed: " . $connection->connect_error);
}
# Set encoding to match PHP script encoding.
mysqli_set_charset($connection, 'utf8');
$query = "SELECT * FROM services";
$select_service = mysql_query($query);
while ($row = @mysql_fetch_assoc($select_service)) {
    $service_prise = $row['prise'];
    $service_content = $row['description'];
echo "<option >".$row['description']."</option>\n  ";
}
?>
</select>
<br>
Ahmed Syam
  • 63
  • 8
  • 2
    You're mixing MySQL APIs (`mysqli_` and `mysql_`). Don't do that. Stick with MySQLi, as the `mysql_` constructor has been obsolete for 4 years. – Obsidian Age Aug 05 '19 at 03:48

1 Answers1

1

You can use jquery & ajax to automatically get values from database on change of select.i,e :

Your jquery will look like below :

$("select[name='topic']").on("change",function(e) {
     e.preventDefault();
    //getting selected  value
    var topic=$(this).val();
    console.log(topic);
        $.ajax({
            type: 'POST',
            url: 'yourpagename.php',
             data: {'topic': topic },//<-passing value to php
            success: function(php) {
               $("#price").html(php);//<--response will show in div with id=price
            }               
        });

    });

Your php page :

 <?php
    // Make the connection:
    $connection = mysqli_connect ('localhost', '#', '#', '#');
    // If no connection could be made, trigger an error:
    if ($connection->connect_error)
    {
        die("Database selection failed: " . $connection->connect_error);
    }
    $topic=$_POST['topic'];//<-getting value which is passed from ajax
    # Set encoding to match PHP script encoding.
    mysqli_set_charset($connection, 'utf8');
 //here column name will be name of coulmn which you need to compare 
    $query = "SELECT * FROM services where columnname = '".$topic."'";
    $select_service = mysqli_query($connection,$query);
    echo '<label style=" margin-left: 136px;
    margin-bottom: 30px;padding: 0 20px;">price</label><select disabled name="topic1"   Required="Required">';
    while ($row = mysqli_fetch_assoc($select_service)) {
        $service_prise = $row['prise'];
        $service_content = $row['description'];
    echo "<option >".$row['description']."</option>\n  ";
    } 

    echo "</select>";
    ?>

Also in your current php page add a <div></div> .i.e :

<div id="price">//<--Inside this div response from ajax will get display
<label style="    margin-left: 136px;
    margin-bottom: 30px;     padding: 0 20px;">price</label><select disabled name="topic"   Required="Required">
>..
...
</select>
</div>
Swati
  • 28,069
  • 4
  • 21
  • 41
  • i replaced the >..... with php code it gave me errors – Ahmed Syam Aug 05 '19 at 05:19
  • what error ? only you need to add a `div` and inside your div add that code .? – Swati Aug 05 '19 at 05:44
  • mysqli_query() expects at least 2 parameters, 1 given in /home/u942786615/domains/search-academy.com/public_html/contactusprise.php on line 14 here is the website https://search-academy.com/contactus.php – Ahmed Syam Aug 05 '19 at 05:53
  • Sorry my mistake , you need to pass your `$connection` to `mysqli_query` i.e : `$select_service = mysqli_query($connection,$query);` – Swati Aug 05 '19 at 05:56
  • error fixed but no data in select – Ahmed Syam Aug 05 '19 at 06:04
  • did you replaced `columnname` with the column you need to get value i.e from your `select` id is been passed , so did you compare with `id` column ,also try to post your table structure. – Swati Aug 05 '19 at 06:06
  • $query = "SELECT * FROM services where description = '".$topic."'"; yes i did – Ahmed Syam Aug 05 '19 at 06:15
  • on change of your `select` the value which is ` – Swati Aug 05 '19 at 06:18
  • https://ibb.co/SJ9zKQ9 – Ahmed Syam Aug 05 '19 at 06:21
  • Your query need to be like this -> `$query = "SELECT * FROM services where id= '".$topic."'";` , you need to compare with `id` column as `id` is been in ajax call ,try to change query and check if it works – Swati Aug 05 '19 at 06:25
  • still nothing show up – Ahmed Syam Aug 05 '19 at 06:30
  • Try to `echo $query ;` and see what does it print ,also change your query like this `$query = "SELECT * FROM services where id= ".$topic;` – Swati Aug 05 '19 at 06:34
  • it worked yesssss ,@swati thank you very much for answering me every time you are amazing – Ahmed Syam Aug 05 '19 at 06:42
  • glad i helped , try to [accept](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) this answer if it was helpful :) – Swati Aug 05 '19 at 06:45
  • i will when i get my 15 reputation ,thanks again – Ahmed Syam Aug 05 '19 at 06:48