Running Ajax From HTML list Issues - php

I am trying to run Ajax on a web application which is suppose to list associated data with HTML List from MySQL database using jQuery and PHP.I have two tables in MySQL database called Item and Items as below:
and I have to PHP file called index.php and result.php as:
================================================================== index.php
<html>
<head></head>
<body>
<?php
$mysqli = new mysqli('localhost', 'root', '', 'testdb');
if ($mysqli->connect_errno)
{
die('Unable to connect!');
}
else{
$query = 'SELECT * FROM items';
if ($result = $mysqli->query($query))
{
if ($result->num_rows > 0)
{
?>
<p>
Select a language
<ul id="item">
<?php
while($row = $result->fetch_assoc()){
?>
<li><div id="selectLanguage"><?php echo $row['item']; ?></div></li>
<?php
}
?>
</ul>
</p>
<p id="result"></p>
<?php
}
else
{
echo 'No records found!';
}
$result->close();
}
else
{
echo 'Error in query: $query. '.$mysqli->error;
}
}
$mysqli->close();
?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('#selectLanguage').click(function()
{
alert("hello");
if($(this).val() == '') return;
$.get(
'results.php',
{ id : $(this).val() },
function(data)
{
$('#result').html(data);
}
);
});
});
</script>
</body>
</html>
and the result.php is
====================================================================== result.php
<?php
$mysqli = new mysqli('localhost', 'root', '', 'testdb');
$resultStr = '';
$query = 'SELECT type FROM item where name='.$_GET['item'];
if ($result = $mysqli->query($query))
{
if ($result->num_rows > 0)
{
$resultStr.='<ul>';
while($row = $result->fetch_assoc())
{
$resultStr.= '<li><strong>'.$row['id'].'</strong> - '.$row['type'];
'</li>';
}
$resultStr.= '</ul>';
}
else
{
$resultStr = 'Nothing found';
}
}
echo $resultStr;
?>
now the first part(index.php) is rendering the list base on the items table but I can't click at the second or third item besides none of them not returning any values to the page.
Can you please let me know what I am doing wrong here?

You're creating the selectLanguage div id multiple times. You should use a class instead of an id. Also. the val() is null. You should use html() instead.
looks like in result.php you're populating the list by parameter 'item'
when in your jquery ajax function you're passing the parameter as 'id'
change one of those and you should be good.

Change Your index.php file as :
<html>
<head></head>
<body>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
<?php
$mysqli = new mysqli('localhost', 'root', '', 'testdb');
if ($mysqli->connect_errno)
{
die('Unable to connect!');
}
else{
$query = 'SELECT * FROM items';
if ($result = $mysqli->query($query))
{
if ($result->num_rows > 0)
{
?>
<p>
Select a language
<ul id="item">
<?php
while($row = $result->fetch_assoc()){
?>
<li><div id="selectLanguage<?php echo $row['id']; ?>"><?php echo $row['item']; ?></div></li>
<script type="text/javascript">
$(document).ready(function()
{
$('#selectLanguage<?php echo $row['id']; ?>').click(function()
{
//alert("hello");
$.ajax({
type : 'POST',
url : 'result.php',
data: {
id : <?php echo $row['id']; ?>
},
success : function(data){
$('#result').html(data);
}
});
});
});
</script>
<?php
}
?>
</ul>
</p>
<p id="result"></p>
<?php
}
else
{
echo 'No records found!';
}
$result->close();
}
else
{
echo 'Error in query: $query. '.$mysqli->error;
}
}
$mysqli->close();
?>
</body>
</html>

Related

How can I not open a link in jQuery UI

I use jquery-ui (v1.12.1).
https://jqueryui.com/tabs/#ajax (TABS LOAD AJAX)
$( function() {
$("#tabs").tabs({
beforeLoad: function (event, ui) {
$('#preloader').show();
if (ui.tab.data("loaded")) {
event.preventDefault();
$('#preloader').hide();
return;
}
ui.ajaxSettings.cache = false,
ui.panel.html('<img src="assets/ajax-loader.gif"> Loading...'),
ui.jqXHR.success(function() {ui.tab.data( "loaded", true ); $('#preloader').hide();
}),
ui.jqXHR.error(function () {ui.panel.html("Couldn't load Data. Plz Reload Page or Try Again Later."); });
}
});
});
I want to not open the tabs "herf" in the form of a link.
<div class="part" id="tabs">
<ul>
<li>Apple</li>
<li>Samsung</li>
<li>Sony</li>
<li>HTC</li>
</ul>
</div>
What's wrong with the code?
I modified this code.
I have extracted the following code from this site:
http://demo.webslesson.info/jquery-ui-tooltip-with-ajax-php/
But I want to write as the tab of Ajax.
index.php
<?php
include('database_connection.php');
$query = "SELECT * FROM tbl_student ORDER BY student_name ASC";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
?>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div class="part" id="tabs">
<ul>
<?php
foreach($result as $row)
{
echo '<li><a id="'.$row["student_id"].'" href="#">'.$row["student_name"].'</a></li>';
}
?>
</ul>
</div>
<script>
$(document).ready(function(){
$("#tabs").tabs({
beforeLoad: function (event, ui) {
$('#preloader').show();
$.post('fetch.php', { id:$(this).attr('id') }, function(data){ result(data); });
if (ui.tab.data("loaded")) {
event.preventDefault();
$('#preloader').hide();
return;
}
ui.ajaxSettings.cache = false,
ui.panel.html('<img src="assets/ajax-loader.gif"> Loading...'),
ui.jqXHR.success(function() {ui.tab.data( "loaded", true ); $('#preloader').hide();}),
ui.jqXHR.error(function () {ui.panel.html("Couldn't load Data. Plz Reload Page or Try Again Later."); });
}
});
});
</script>
fetch.php
<?php
if(isset($_POST["id"]))
{
$query = "SELECT * FROM tbl_student WHERE student_id = '".$_POST["id"]."'";
$statement = $connect->prepare($query);
$statement->execute();
$result = $statement->fetchAll();
$output = '';
foreach($result as $row)
{
$output .= '
<img src="images/'.$row["image"].'" class="img-thumbnail" />
<h4>Name - '.$row["student_name"].'</h4>
<h4>Phone No. - '.$row["student_phone"].'</h4>
';
}
echo $output;
}
?>
When I click that tab, That text doesn't show.
Is jQueryUI code loaded incorrectly?

How to use jquery ajax to get one variable from a php file to another php file

I'm having trouble passing a variable in my a php file to another php file using jquery ajax. I don't know why it doesn't read it though because everytime I echo the variable it's undefined or just plain blank.
teams.php
<?php
// Display members from database
$connect = mysqli_connect('localhost', 'root', '', 'chansandbox');
if(isset($_POST['display'])){
$team = "SELECT * FROM team";
$resultTeam = mysqli_query($connect, $team);
?>
<ul class="teams">
<li><a id="teamheader">LIST OF TEAMS</a></li>
</ul>
<?php
while($arrayTeam = mysqli_fetch_array($resultTeam)){
$team_id = $arrayTeam['team_id'];
?>
<ul class="teams" name="teamId" value="<?php echo $team_id ?>">
<li><?php echo $arrayTeam['team_name']; ?>
<ul class="menu">
<li class="memberdivholder">
<div class="gradeview">
<h1 id='options'><i class="fa fa-cog"></i>
Options:
<button class="topbutton">Grade Team</button>
<button class="topbutton">View Team</button>
<button class="teamdelete">Delete Team</button>
</h1>
</div>
<?php
$member = "SELECT * FROM employee WHERE team_team_id = '$team_id' ORDER BY position_pos_id=17 desc";
$resultMember = mysqli_query($connect, $member);
while($arrayMember = mysqli_fetch_array($resultMember)){
?>
<div class="memberholder">
<h1 class="clearfix">
<?php
if($arrayMember['position_pos_id']==17){
echo "<i class='fa fa-star-o' aria-hidden='true'></i>";
}
?>
<?php echo $arrayMember['emp_fname'] . " " . $arrayMember['emp_lname']; ?>
<button class="viewprofile">View Profile</button>
</h1>
</div>
<?php
}
?>
</li>
</ul>
</li>
</ul>
<?php
}
exit();
mysqli_close($connect);
}
?>
script.js
$('.teams').on('click', '.teamdelete', function(){
var teamId = $(.teams).attr("value");
if(confirm("Are you sure?")){
$.ajax({
url: "http://localhost:81/chansandbox/wp-content/themes/Skeleton/teamdelete.php",
method: "POST",
type: "POST",
data: {
team_id: teamId
},
success: function(data)
{
alert(data);
eval(data);
}
});
}
});
teamdelete.php
<?php
$connect = mysqli_connect('localhost', 'root', '', 'chansandbox');
$team_id = $_POST['teamId'];
$update = "UPDATE employee SET team_team_id = NULL WHERE team_team_id = '$team_id'";
if(mysqli_query($connect, $update)){
echo "Employees Updated";
$deleteteam = "DELETE FROM team WHERE team_id = '$team_id'";
if(mysqli_query($connect, $deleteteam)){
echo "Team deleted '$team_id'";
}
else{
echo "Team not deleted";
}
}
else{
echo "Employees not updated";
}
mysqli_close($connect);
?>
As you can see I'm trying to get the value of the ul (class=teams) in the teams.php and passing it on to the js script file using ajax and var teamId = $(this).attr("value"); and passing it onto teamdelete.php but it doesn't seem to work. Any help would really be appreciated thank you.
because $('.teamdelete') has no attr value ..
in your html you should put
<button class="teamdelete" value="<?php echo $arrayTeam['team_id']; ?>">Delete Team</button>
and in your javascript do this
var teamId = $(this).attr("value");
and in your php .. since the name of the post data = team_id you should do
$team_id = $_POST['team_id'];
Your data variable is incorrect...
The ajax data is sent as data: { NAME : VALUE }. The PHP is essentially calling $_POST['VALUE'] So the php doesn't know what that value is.
The PHP calls:
$_POST['teamId'];
But the ajax sends
team_id:
Try changing the PHP on teamdelete.php to:
$team_id = $_POST['team_id'];
There are additional issues as well...
html:
<button class="teamdelete" value="<?php echo $arrayTeam['team_id']; ?>" >Delete Team</button>
js:
$('.teams').on('click', '.teamdelete', function(){
var teamId = $(this).attr("value"); // ------------ CHANGED
console.log(teamId); // ----------- CHANGED should show the right ID in console
if(confirm("Are you sure?")){
$.ajax({
url: ".....teamdelete.php",
method: "POST",
type: "POST",
data: {
team_id: teamId
},
success: function(data)
{
alert(data);
eval(data);
}
});
}
});
PHP:
<?php
$connect = mysqli_connect('localhost', 'root', '', 'chansandbox');
$team_id = $_POST['team_id']; // ----------- CHANGED
$update = "UPDATE employee SET team_team_id = NULL WHERE team_team_id = '$team_id'";
if(mysqli_query($connect, $update)){
echo "Employees Updated";
$deleteteam = "DELETE FROM team WHERE team_id = '$team_id'";
if(mysqli_query($connect, $deleteteam)){
echo "Team deleted '$team_id'";
}
else{
echo "Team not deleted";
}
}
else{
echo "Employees not updated";
}
mysqli_close($connect);
?>
You can use the developers panel and inspector to confirm that the delete button has the correct value for the team.

Ajax request to the same PHP page gives no errors but doesn't work

I am making a product displaying page of an ecommerce website. The products are to be filtered by brands on the basis of brands that are checked by the customer. For this I have used an ajax request everytime a brand is checked. The problem is that the page cannot receive the get variables that i am sending to the same page. The ajax request is not giving any errors and also the chrome debugger side is also not showing any error at all. This is the page:
snapshot of the products page
And this is the code of the page:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<?php
session_start();
include('includes/pdo_connect.php');
include('includes/pdo_functions.php');
$logged_in;
$user_first_name;
if(isset($_SESSION['user'])){ //Determining if the user is logged in or not.
if($_SESSION['user']=='user'){
$user_id = $_SESSION['user_id'];
global $logged_in;
$logged_in = true;
global $user_first_name;
$user_first_name = $_SESSION['user_first_name'];
}
} else {
$_SESSION['user'] = 'guest';
$user_id = $_SERVER['REMOTE_ADDR'];
}
$cat;
if(isset($_GET['cat'])){
global $cat;
$cat = $_GET['cat'];
}
include('includes/logged_in.php');
if(isset($_GET['brand_list'])){
$brand_list = $_GET['brand_list'];
} else {
echo "<script>alert('value not received');</script>";
}
?>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="styles/list_style.css?<?php echo time(); ?>">
<link rel="stylesheet" type="text/css" href="styles/thickbox.css" media="screen">
<script type="text/javascript" src="js/jquery-3.1.1.js"></script>
<script type="text/javascript" src="js/thickbox.js"></script>
<?php
$where = array();
if(!empty($brand_list)){
echo "ajax working";
if(strpos($brand_list, ',')!==false){
$brand_choices = explode(',', $brand_list);
$barray = array();
foreach($brand_choices as $value) {
$barray[] = "brand_id = $value";
}
$where[] = '('.implode(' OR ', $barray).')';
} else {
$where[] = '(brand_id= '.$brand_list.')';
}
} else {
//echo "ajax not working ";
}
$w = implode(' AND ', $where);
$w = "where product_cat=$cat ".$w;
$filter_query = "select * from products $w ";
echo "filter query: ".$filter_query;
$first_load = 'filter';
function show_filtered(){
//echo "<script>alert('filter query working');</script>";
global $filter_query;
global $con;
global $brand_name;
try{
$stmt = $con->prepare($filter_query);
$stmt->execute();
$result = $stmt->fetchAll();
foreach ($result as $data) {
$product_id = $data['product_id'];
$product_cat = $data['product_cat'];
$product_brand = $data['product_brand'];
$product_title = $data['product_title'];
$product_price = $data['product_price'];
$product_desc = $data['product_desc'];
$product_image = $data['product_image'];
echo "
<div class='product_container $brand_name'>
<a href='details.php?pid=".$product_id."' alt='".$product_title."'>
<div class='img_div'>
<img src='admin/product_images/".$product_image."?".time()."' alt='".$product_title."'/>
</div>
<div class='index_product_desc'>".$product_title."</div>
<div class='index_product_price'>₹".$product_price."</div>
</a>
</div>
";
}
} catch(PDOException $e){
echo "Error in show_list(): ".$e->getMessage();
}
}
function show_brands(){
global $con;
global $cat;
global $brand_name;
try{
$query = "select * from cat_brand where cat_id = $cat";
$stmt = $con->prepare($query);
$stmt->execute();
$result = $stmt->fetchAll();
//$brand = array();
foreach ($result as $data) {
$brand_id = $data['brand_id'];
//echo "<script>alert('$brand_id');</script>";
$query1 = "select * from brands where brand_id = $brand_id";
$stmt1 = $con->prepare($query1);
$stmt1->execute();
$result1 = $stmt1->fetchAll();
echo "<ul>";
foreach ($result1 as $data1) {
$brand_name = $data1['brand_title'];
echo "<li><input type='checkbox' value='$brand_id' id='$brand_name' class='brand_check' name='brandchoice'> $brand_name</li>";
}
echo "</ul>";
}
} catch(PDOException $e){
echo "Error in show_brands: ".$e->getMessage();
}
}
function show_price(){
}
?>
</head>
<body>
<div class="wrapper">
<header>
<div class="home_logo">
<a href="index.php">
<img src="images/skyshop_sumopaint.png" alt="Site Home">
</a>
</div>
<div class="login">
<?php user();?> |
<?php login_status(); ?>
</div>
<div class="form">
<form method="get" target="" name="searchbar_form">
<input type="text" name="searchbar" id="searchbar">
<input type="submit" id="search_button" value="Search">
</form>
</div>
</header>
<div class="menubar">
<div class="dropdown">
<button onclick="dropdownToggle()" class="dropdown-button">Shop By Category</button>
<ul class="dropdown-content" id="dropdownContent">
Categories
<?php getcats(); ?>
</ul>
</div>
<div class="menubar-div">
<ul class="menu-items">
<?php getcats(); ?>
</ul>
</div>
<div class="cart">
Cart (0)
</div>
</div>
<div class="content">
<div class="nav">
</div>
<div class="list_wrapper">
<!--/////////////////////////////////////////////// Filter div /////////////////////////////////////////////////////-->
<div class="filter">
<span class="filter_heading">Select Brands</span>
<a href="" class="clear" id="clear_brands">Clear<a>
<div class="brand_div">
<?php
show_brands();
?>
</div>
<div class="price_div">
</div>
</div>
<!--/////////////////////////////////////////////// List div ///////////////////////////////////////////////////////-->
<div class="list">
<div class="loading">
<img src="images/loadingAnimation.gif">
</div>
<?php
show_filtered();
?>
</div>
</div>
<div class="footer">
FOOTER
</div>
</div>
</div>
<?php
?>
<script type="text/javascript">
$(window).on('load', function(){
function filter(){
//alert("filter called");
$('.filter .list').css('opacity', 0.5);
$('.loading').css('visibility', 'visible');
var brandchoice = new Array();
$('input[name="brandchoice"]:checked').each(function(){
brandchoice.push($(this).val());
$('#clear_brands').css('visibility', 'visible');
});
if(brandchoice==""){
$('#clear_brands').css('visibility', 'hidden');
}
var brand_list = '&brand_list='+brandchoice;
var data = brand_list.substring(1, brand_list.length);
//alert(data);
$.ajax({
url: "list.php",
type: "GET",
data: data,
cache: false,
success: function(result){
$(".filter .list").css("opacity", 1);
$(".loading").css("visibility", "hidden");
},
error: function(jqxhr, exception){
console.log(jqxhr);
},
beforeSend: function(){
console.log("before send: "+data); //This is showing "brand_list=1" which is correct.
}
});
}
$('input[type="checkbox"]').on('change', filter);
$('#clear_brands').on('click', function(){
$('.brand_check').removeAttr('checked');
filter();
$('#clear_brands').css('visibility', 'hidden');
});
}); //end of jquery
</script>
</body>
</html>
I am using alert() in the beforeSend in the ajax request and it returns the correct data as expected. But the PHP section of the page does not received the values. There are no errors on the PHP side or the browser debug window.
I checked your code line by line. and figure out that you have code for ajax inside the function named "filter" function filter().
Now you are calling this filter function on by onclick event of the element with id clear_brands
$('#clear_brands').on('click', function(){
$('.brand_check').removeAttr('checked');
filter();
$('#clear_brands').css('visibility', 'hidden');
});
and by this code i came to know that at the end your ajax call is not being made because you click event was not triggered,
So either you should trigger this event on your document get ready or you have to do it by clicking on that element.
Just think about the flow once.
i was testing your script in my local and
with some modification i have made it working.
did some changes like
at the end of the script i just putted this code below.
$(document).ready(function(){
$('#clear_brands').trigger("click");
});
And ajax call was executed..
check out my entire HTML
<!DOCTYPE html>
<html>
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<a href="" class="clear" id="clear_brands">Clear<a>
<script type="text/javascript">
$(window).on('load', function(){
function filter(){
//alert("filter called");
$('.filter .list').css('opacity', 0.5);
$('.loading').css('visibility', 'visible');
var brandchoice = new Array();
$('input[name="brandchoice"]:checked').each(function(){
brandchoice.push($(this).val());
$('#clear_brands').css('visibility', 'visible');
});
if(brandchoice==""){
$('#clear_brands').css('visibility', 'hidden');
}
var brand_list = '&brand_list='+brandchoice;
var data = brand_list.substring(1, brand_list.length);
//alert(data);
$.ajax({
url: "list.php",
type: "GET",
data: data,
cache: false,
success: function(result){
$(".filter .list").css("opacity", 1);
$(".loading").css("visibility", "hidden");
},
error: function(jqxhr, exception){
console.log(jqxhr);
},
beforeSend: function(){
console.log("before send: "+data); //This is showing "brand_list=1" which is correct.
}
});
}
$('input[type="checkbox"]').on('change', filter);
$('#clear_brands').on('click', function(){
$('.brand_check').removeAttr('checked');
filter();
$('#clear_brands').css('visibility', 'hidden');
});
}); //end of jquery
$(document).ready(function(){
$('#clear_brands').trigger("click");
});
</script>
</body>
</html>
In Chrome do Ctrl+Shift+I to open Developer Tools and check Console for errors and Network tab to see if the data is passed in request.

Write a Jquery inside the <?php?> tag

I want to write Jquery code inside PHP tag. I wanted to do this so that I can perform AJAX after that.
I have tried to echo it, but it doesn't work.
<?php
require("conn.php");
$rs = mysql_query("select * from food order by LENGTH(price), price");
if($rs!=false && mysql_num_rows($rs)>0){
$counter ++;
while($row = mysql_fetch_array($rs)){
echo '
<script src="jquery.js">
<script>
$(document).ready(function(){
$("#'.$row["code"].'").click(function(){
echo "clicked";
});
});
</script>
';
}
mysql_free_result($rs);
}else{
echo mysql_error();
}
mysql_close();
?>
Here's a small rearrangement of your code.
<?php
require("conn.php");
$rs = mysql_query("select * from food order by LENGTH(price), price");
if($rs!=false && mysql_num_rows($rs)>0){
$counter ++;
while($row = mysql_fetch_array($rs)){
echo '<a class="row_food">'. $row["code"] .'</a>';
}
mysql_free_result($rs);
}else{
echo mysql_error();
}
mysql_close();
?>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$('.row_food').click(function(){
alert("clicked");
return false;
});
});
</script>
You don't have to echo the javascript code every time in the loop! You only need to echo the content from your db inside the loop.
In the javascript/jquery, you could bind the click event to the element and do whatever you needed.
Hope it helps.
Just put </script> after jquery.js.

query WHERE id='.$_GET['selected"] not working as intended

I need to allow my user to choose an option value in a dropdown list, upon which the page will refresh and columns from the DB will be update on the page. How can I set this behavior?
<script language="JavaScript">
// this js autorehresh page if option was change
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
</script>
<?php
//at first we must connect to DB
//external
require ('connectDB.php');
?>
<form method="post">
<select name="selected" onChange="MM_jumpMenu('parent',this,1)">
<?php
**// echo all id in table to options dropdown menu**
$sql='SELECT id FROM lekcia1';
$result=mysql_query($sql) or die(mysql_error($db));
$a = 0;
while ($recording=mysql_fetch_array($result)){
$a ++;
echo '<option value="'.$recording['selected'].'"> '.$a.' </option>';
};
?>
</select>
</form>
**<?php
// query NOT WORK
$sql1='SELECT * FROM lekcia1 WHERE id='.$_GET['']; // Notice: Undefined index: selected
$result1=mysql_query($sql1) or die(mysql_error($db));
$recording1=mysql_fetch_array($result1);
?>**
<ul>
<li><?php echo $recording1['column1'];?></li>
<li><?php echo $recording1['column2'];?></li>
<li><?php echo $recording1['column3'];?></li>
</ul>
The query is still no-working so here is my correct code.
<html>
<head>
<script language="JavaScript">
// this js autorehresh page if option was change
function MM_jumpMenu(targ,selObj,restore){ //v3.0
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
</script>
</head>
<body>
<?php
$host='localhost'; // this will ususally be 'localhost', but can sometimes differ
$dbname='phpmyadmin'; // the name of the database that you are going to use for this project
$user='root'; // the USERNAME that you created, or were given, to access your database
$password=''; // the PASSWORD that you created, or were given, to access your database
$db = mysql_connect($host, $user, $password) or die("Neda sa pripojit k MySQL serveru: " . mysql_error());
mysql_select_db($dbname, $db) or die(mysql_error($db));
mysql_query('SET NAMES UTF8');
mysql_query('SET COLLATION_CONNECTION=uft8_general_ci');
?>
<form method="post">
<select name="selected" onChange="MM_jumpMenu('parent',this,1)">
<?php
// echo all id in table to options dropdown menu**
$sql='SELECT id FROM lekcia1';
$result=mysql_query($sql) or die(mysql_error($db));
$a = 0;
while ($recording=mysql_fetch_array($result)){
$a ++;
echo '<option value="'.$recording['selected'].'"> '.$a.' </option>';
};
?>
</select>
</form>
<?php
// query NOT WORK
$sql1="SELECT * FROM lekcia1 WHERE id='".$_POST['selected']."'"; // Notice: Undefined index: selected
$result1=mysql_query($sql1) or die(mysql_error($db));
$recording1=mysql_fetch_array($result1);
?>
<ul>
<li><?php echo $recording['nazov_lekcie'];?></li>
<li><?php echo $recording['cislo_ulohy'];?></li>
<li><?php echo $recording['nazov_ulohy'];?></li>
</ul>
<?php
//at first we must connect to DB
//external
//require ('connectDB.php');
?>
</body>
</html>

Categories