OnChange changes the displayed data from the table

I have a table on my website and a dropdown on my website that, according to the value selected from the dropdown, will change the result of the table. I did a search and I found this StackOverflow question and I tried to do something similar but it doesn't work. Here is my php file called the ads that I want to display in the table.

<?php
include_once 'header.php';
$connection = mysql_connect("localhost", "root", "smogi")?>

<html>
<head>
<script type="text/javascript" src="javascript.js"></script>
</head>
<body>
<?php
$result = queryMysql("SELECT * FROM doctor WHERE username='$username'");
if (mysql_num_rows($result)):
?>
<!-- Koumpi pou se metaferei sti selida gia tin dimiourgia neas anakoinwseis -->
<form action="new_announcement.php">
<input type="submit" value="Create New Announcement">
</form>
<br />

Select Category :<select id="SelectDisease" name="category">
<option value="*">All</option>
<!--emfanizei tis epiloges gia ta specialties me basi auta p exoume sti basi mas -->
<?php
$sql = mysql_query("SELECT name FROM disease");
while ($row = mysql_fetch_array($sql)) {
echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
}
?>
</select><br><br />

<table border="1" style="width:100%">
<tr>
<td><b>Author</b></td>
<td><b>Category</b></td>
<td><b>Subject</b></td>
<td><b>Content</b></td>
</tr>
<?php
if(isset($_GET["selected"])){

$type = $_GET["selected"];
$query = "SELECT author,category,subject,content FROM announcements WHERE category='" . $type . "'";
$announcements = mysql_query($query, $connection);
$counter = 0;
$z = 0;
if ($announcements == FALSE) {
die(mysql_error()); // To get better errors report
}
while ($row = mysql_fetch_assoc($announcements)) {
while ($row = mysql_fetch_assoc($announcements)) {
$counter++;
?>
<tr>
<td><?php echo $row['author'];?></td>
<td><?php echo $row['category']; ?></td>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['content']; ?></td>
</tr>
<?php }
}
}
?>
<?php
else:
?>

Select Category :<select id="SelectDisease" name="category">
<option value="*">All</option>
<!--emfanizei tis epiloges gia ta specialties me basi auta p exoume sti basi mas -->
<?php
$sql = mysql_query("SELECT name FROM disease");
while ($row = mysql_fetch_array($sql)) {
echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
}
?>
</select><br><br />

<table border="1" style="width:100%">
<tr>
<td><b>Author</b></td>
<td><b>Category</b></td>
<td><b>Subject</b></td>
<td><b>Content</b></td>
</tr>
<?php
if(isset($_GET["selected"])){
$type = $_GET["selected"];
$query = "SELECT author,category,subject,content FROM announcements WHERE category='" . $type . "'";
$announcements = mysql_query($query, $connection);
$counter = 0;
$z = 0;
while ($row = mysql_fetch_assoc($announcements)) {
$counter++;
?>
<tr>
<td><?php echo $row['author'];?></td>
<td><?php echo $row['category']; ?></td>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['content']; ?></td>
</tr>
<?php } } endif;?>
</table>
</body>
</html>

      

And this is my javascript.js file

$(document).ready(function() {
  $('#SelectDisease').change(function() {
var selected=$(this).val();
  $.get("announcements.php?selected="+selected, function(data){
      $('.result').html(data);

    });
    });
});

      

Thank you for your time:)

PS: INCLUDE_ONCE CODE creates connection to db

+1


source to share


3 answers


First of all, if your user doesn't make a choice, you don't have $ _GET, so it will be undefined. You should get the value if available. For example:

if(isset($_GET['selected'])){
    $type = $_GET['selected'];
}

      

But this is not the only problem. $.get

is an ajax request and it just sends a request to your php file and gets all the html content of your page.

If you don't want to reload the page every time your user selects an option, instead of asking, it $.get

just redirects the user to that URL. Otherwise, if you want to do it without rebooting, you need to execute the request $.get

correctly.



An example of what you said you were trying to do something like this does not send a request $.get

to the same page as the user. It sends a request to another php page that is only meant to fetch $_GET["selected"]

from its url, send a request to the database, get the result from the database, and then just return the result that would be data

in your request $.get

.

See this example: Get data from mysql database using php and jquery ajax

It tries to do the same as you, except $.POST

that you can do the same or change it to $.get

if you want. See how another php page is returning the result and how the request $.get

gets and looks at the data.

+1


source


mysql_query()

will result in FALSE

which is a boolean value, if there is any error, check before the loop while

to get the best error,



if($announcements == FALSE) { 
    die(mysql_error()); // To get better errors report
}

while($row = mysql_fetch_assoc($announcements)){
  // then you code here
}

      

0


source


mysql_query () returns FALSE on error. You must choose a name for the database. For example:

$connection = mysql_connect("localhost", "root", "smogi", "***DATABASE_NAME***");

      

Remember mysql functions are deprecated! Use mysqli or PDO.

A good day

0


source







All Articles