samedi 18 avril 2015

menu 2 result to use for SQL search

Sorry if this is not clear but I am very new to this and am having trouble understanding the use of jQuery and php/mySQL. My problem is that I have successfully populated 2 menus dynamically from database tables using jquery. Now I need to be able to use the selection of menu 2 to query my database. I just cant seem to be able to retrieve the contents of the chosen field in menu 2 in the right format to use Below is my attempt:


select.php





<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://ift.tt/kTyqzh">
<html>

<head>
<script type="text/javascript" src="jquery-1.7.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("select#exercise").attr("disabled", "disabled");
$("select#category").change(function() {
$("select#exercise").attr("disabled", "disabled");
$("select#exercise").html("<option>wait...</option>");
var id = $("select#category option:selected").attr('value');
$.post("select_exercise.php", {
id: id
}, function(data) {
$("select#exercise").removeAttr("disabled");
$("select#exercise").html(data);
});
});
$("form#select_form").submit(function() {
var cat = $("select#category option:selected").attr('value');
var type = $("select#exercise option:selected").attr('value');
if (cat > 0 && type > 0) {
var result = $("select#exercise option:selected").html();
$("#result").html('your choice: ' + result);
} else {
$("#result").html("you must choose two options!");
}
return false;
});
});
</script>
</head>

<body>
<?php include "select.class.php"; ?>
<form action="process.php" id="select_form" method="post">
Choose a category:
<br />
<select id="category">
<?php echo $opt->ShowCategory(); ?>
</select>
<br />
<br />Choose a :
<br />
<select id="exercise">
<option value="0">choose...</option>
</select>
<br />
<br />
<input id="submit" type="submit" value="Submit" />
</form>
<div id="result"></div>

<!-- Trying to get result from menu2 to use in process.php sor sql query -->
<script>
< div id = "resultsContainer" > < /div>

$(document).ready(function(){
/ / alert('jQuery loaded');
$('#select_form').submit(function(event) {
event.preventDefault;
resultvalue = $('#result')[0].selectedIndex;
$.post('process.php', {
result: resultvalue
}).done(function(data) {
alert(data);
})
return false;
})
})
</script>
</body>

</html>



process.php





<?php
$host = "localhost";
$user = "root";
$password = "";
$db = "myDB";

mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
mysql_select_db($db) OR die("can not select the database $db");

$result = $_POST["result"];
$sql = "SELECT aim1,aim2,aim3,aim4 FROM exercise WHERE name=$result";
$result1 = mysql_query($sql);
mysql_close();

while($row = mysql_fetch_array($result1)){
echo '<table class="table">';
echo '<thead>';
echo"<tr><th>Communication</th></tr>";
echo '</thead><tbody>';
echo "<tr><td>";
echo $row['aim1'];
echo "</td></tr><tr><td>";
echo $row['aim2'];
echo "</td></tr><tr><td>";
echo $row['aim3'];
echo "</TD></tr>";
echo "</td></tr><tr><td>";
echo $row['aim4'];
echo "</td></tr></tbody></table>";
}
?>
echo $sql;



Any help would be much appreciated.


Aucun commentaire:

Enregistrer un commentaire