发送sql查询参数来实现功能

时间:2015-10-02 15:19:29

标签: javascript php mysql

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

我有一个选择调用showText函数onchange。

$query = "SELECT * FROM wp_thesis";
$result = mysqli_query($conn,$query);?>

<select name="ThesisTitle" onchange="showText(x,y)" 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 />";?>

首先想到的是发送select的值(onchange =&#34; showText(this.value)&#34;),然后在showText函数中有一个sql查询,以获得我想要的两个值。我读到你不能在函数内部执行sql查询,因为Javascript是客户端的,所以我想在php上执行sql查询,然后将值发送到showText函数。我想要的查询是:

$query = "SELECT Course FROM wp_courses WHERE Thesis_ID={$row[Thesis_ID]} ";
$courses = mysqli_query($conn,$query);
$coursesNo = mysqli_num_rows($courses);

我想发送的价值观是$ course和$ courseNo。是否可以在同一个php文件中获取select的值,而不使用按钮或类似的东西?

2 个答案:

答案 0 :(得分:2)

在渲染选项之前获取X和Y坐标,并将其作为数据属性提供。

    <select name="ThesisTitle" onchange="showText(this)" required="">
      <option disabled='disabled' selected='selected' value=''></option>"; <?php
      foreach ($result as $row)
      {
          echo "<option data-x={$row[Thesis_X]} data-y={$row[Thesis_Y]} value= {$row[Thesis_ID]}>{$row[Thesis_Title]}</option>";
      }
    echo"</select><br />";?>

在此之后,使用this作为参数转到showText(this)函数,并使用

获取属性
  function showText(obj){
     var x_val = $(obj).attr("data-x");
     var y_val = $(obj).attr("data-y");
  }

希望这对你有所帮助。

答案 1 :(得分:0)

我终于找到了我需要的东西。我发布了代码。

<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 />";?>

我将Thesis_ID发送到http://localhost/wordpress/get_thesis/,这是一个执行我需要的查询的php文件。