Javascript 向函数发送sql查询参数
我有一个select,它在更改时调用showText函数Javascript 向函数发送sql查询参数,javascript,php,mysql,Javascript,Php,Mysql,我有一个select,它在更改时调用showText函数 TABLE wp_thesis TABLE wp_courses Thesis_ID Thesis_Title Course_ID Thesis_ID Course 1 thesis1 1 1 course1 2 thesis2 2 1 course2
TABLE wp_thesis TABLE wp_courses
Thesis_ID Thesis_Title Course_ID Thesis_ID Course
1 thesis1 1 1 course1
2 thesis2 2 1 course2
3 2 course1
4 2 course2
5 2 course3
我要发送的值是$courses和$coursesNo。是否可以在同一个php文件中获取select的值,而不使用按钮或类似的东西 在渲染选项之前获取X和Y坐标,并将其作为数据属性提供
$query = "SELECT Course FROM wp_courses WHERE Thesis_ID={$row[Thesis_ID]} ";
$courses = mysqli_query($conn,$query);
$coursesNo = mysqli_num_rows($courses);
希望这对你有所帮助。我终于找到了我需要的东西。我正在发布代码
function showText(obj){
var x_val = $(obj).attr("data-x");
var y_val = $(obj).attr("data-y");
}
//浏览器支持代码
功能展示课程(str){
var ajaxRequest;//使Ajax成为可能的变量!
试一试{
//Opera 8.0+、Firefox、Safari
ajaxRequest=新的XMLHttpRequest();
}捕获(e){
//Internet Explorer浏览器
试一试{
ajaxRequest=newActiveXObject(“Msxml2.XMLHTTP”);
}捕获(e){
试一试{
ajaxRequest=新的ActiveXObject(“Microsoft.XMLHTTP”);
}捕获(e){
//出了点问题
警告(“您的浏览器有问题!”);
返回false;
}
}
}
//创建一个将接收数据的函数
//从服务器发送并将更新
//同一页中的div部分。
ajaxRequest.onreadystatechange=函数(){
if(ajaxRequest.readyState==4){
var ajaxDisplay=document.getElementById('courses');//它应该显示在哪里
ajaxDisplay.innerHTML=ajaxRequest.responseText;
}
}
//现在获取值并将其传递给服务器脚本。
var queryString=“?论文id=“+str;
ajaxRequest.open(“GET”http://localhost/wordpress/get_thesis/“+查询字符串,正确);
ajaxRequest.send(空);
}
从菜单中选择:
<script language="javascript" type="text/javascript">
//Browser Support Code
function showCourses(str){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Problem with your browser!");
return false;
}
}
}
// Create a function that will receive data
// sent from the server and will update
// div section in the same page.
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('courses'); // where it should be displayed
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Now get the value and pass it to server script.
var queryString = "?thesis_id=" + str ;
ajaxRequest.open("GET", "http://localhost/wordpress/get_thesis/" + queryString, true);
ajaxRequest.send(null);
}
";
我正在发送论文ID,其中有一个php文件,用于执行我需要的查询。将所有选项回显到页面上时,只需将课程和课程编号设置为每个选项的数据属性。当select onchange事件触发时,您只需选择事件目标的数据属性即可获得x和y值。@Shilly我尝试了d但我不知道我的方法是否正确。我以前从未使用过数据属性。你需要ajax!@MozzieMD你能帮我做点什么吗?Foreach从wp_论文表中获取值。我想要的x和y值不在wp_论文表中,它们在wp_课程中。@Shilly我想要选择的课程和课程编号ed论文。请参阅我问题末尾的查询。当有人选择论文标题时,我希望获得与此论文相关的课程以及课程数量。我不知道我是否已经说清楚了。如果在创建选项时已包含与每个论文相关的课程,则可以节省往返se的时间服务器,因此,不必使用任何东西从数据库中获取这些数字,您只需检查所选选项的属性。因为在本例中,这两个选项似乎都是静态数据,所以没有理由不包含该数据。@Shilly数据不是静态的,这就是我需要从数据库中获取数据的原因。我认为唯一的方法是使用ajax。
function showText(obj){
var x_val = $(obj).attr("data-x");
var y_val = $(obj).attr("data-y");
}
<script language="javascript" type="text/javascript">
//Browser Support Code
function showCourses(str){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
}catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){
// Something went wrong
alert("Problem with your browser!");
return false;
}
}
}
// Create a function that will receive data
// sent from the server and will update
// div section in the same page.
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('courses'); // where it should be displayed
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
// Now get the value and pass it to server script.
var queryString = "?thesis_id=" + str ;
ajaxRequest.open("GET", "http://localhost/wordpress/get_thesis/" + queryString, true);
ajaxRequest.send(null);
}
<select name="ThesisTitle" id="link_block" onchange="showCourses(this.value)" required="">
<option disabled='disabled' selected='selected' value=''></option>";<?php
foreach ($result as $row)
{
echo "<option value= {$row[Thesis_ID]}>{$row[Thesis_Title]}</option>";
}
echo"</select><br />";?>