php - 获取由选择选项填充的文本值

标签 php mysql ajax

我在这里急需。我已经去找了我认识的每一个懂 php/mysql/ajax 的人,但没有人能提供帮助。

我正在尝试获取一个输入字段,其中填充了我的数据库中的数据,该数据是从两个不同的选择中选择的。情况如下:

在我的网站上建立一个高尔夫计分页面。用户将选择一个球场(选择#1),然后选择他们打的球座(选择#2),然后禁用的文本输入将填充评级和坡度。评级和斜率非常重要,因为它们有助于确定用户的障碍。我能够很好地填充所有内容,但我无法在 get_ rating.php 页面上的查询中找出正确的 WHERE 子句。有人可以帮我解决这个问题吗?

这是我的代码:

数据库设置:

这是从 2 个表中提取的,一个是类(class)表(course_id、c_id、name),另一个是 course_tees 表(tee_id、course_name、c_id、t_id、color、 rating、slope)。两个表上的 c_id 是相同的。

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" class='new_score'>
  <div class='form-group'>
    <select class="form-control" name="course_name" onchange="get_tees(this.value)">
      <option value="">select a tee</option>
        <?php get_courses() ?>
    </select>
  </div>
  <div class='form-group'>
    <select class='form-control' name='tee_played' onchange="get_rating(this.value)" id='txtHint'>
      <option value="">select a tee</option>
    </select>
  </div>
  <div class="form-group" id="getRating">
  </div>

第一个选择使用 get_tees.php 代码(下面列出),第二个选择使用 get_ rating.php 代码(下面列出 2),这是我遇到问题的代码。

get_tees.php

$con = mysqli_connect("***","***","***","***") or die("connection was not established"); 

$q = intval($_GET['q']);

mysqli_select_db($con,"course_tess");
$sql="SELECT * FROM course_tees WHERE c_id = '".$q."'";

$result = mysqli_query($con,$sql);

    while($row=mysqli_fetch_array($result)) {

        $tee_id = $row['tee_id'];
        $c_id = $row['c_id'];
        $t_id = $row['t_id'];
        $tee_color = $row['color'];
        $cor_rating = $row['rating'];
        $cor_slope = $row['slope']; ?>

        <option value='<?php echo $tee_id ?>'><?php echo $tee_color ?></option>

获取评级.php

$con = mysqli_connect("***","***","***","***") or die("connection was not established"); 

$q = intval($_GET['q']);

mysqli_select_db($con,"course_tees");
$sql="SELECT * FROM course_tees";

$result = mysqli_query($con,$sql);

    while($row=mysqli_fetch_array($result)) {

$c_id = $row['c_id'];
$t_id = $row['t_id'];
$cor_rating = $row['rating'];
$cor_slope = $row['slope']; ?>

<input type='text' name='cor_rating' class='form-control' value='<?php echo $row['rating']; ?>' disabled>
<input type='text' name='cor_slope' class='form-control' value='<?php echo $row['slope']; ?>' disabled>     

这是我的两个选择的ajax:

function get_tees(str) {
if (str == "") {
    document.getElementById("txtHint").innerHTML = "";
    return;
} else { 
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","get_tees.php?q="+str,true);
    xmlhttp.send();
}
}

function get_rating(str) {
if (str == "") {
    document.getElementById("getRating").innerHTML = "";
    return;
} else { 
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("getRating").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET","get_rating.php?q="+str,true);
    xmlhttp.send();
}
}    

我在ajax中没有正确传递一些东西吗?我究竟做错了什么?!?!请帮忙!!

最佳答案

@chris85 这本质上是我现在解决这个问题的创可贴...我没有尝试自动填充文本字段,而是将它们列在选择中并手动输入评级/斜率.. .这是我的代码:

PHP:

mysqli_select_db($con,"course_tess");
$sql="SELECT * FROM course_tees WHERE c_id = '".$q."'";

$result = mysqli_query($con,$sql);

echo "<option value=''>select a tee</option>";

    while($row=mysqli_fetch_array($result)) {

        $tee_id = $row['tee_id'];
        $c_id = $row['c_id'];
        $t_id = $row['t_id'];
        $tee_color = $row['color'];
        $cor_rating = $row['rating'];
        $cor_slope = $row['slope']; ?>

        <option value='<?php echo $tee_color ?>'><?php echo $tee_color ?> - <?php echo $cor_rating ?> : <?php echo $cor_slope ?></option>

和形式:

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" class='new_score'>
                <div class='form-group'>
                    <select class="form-control" name="course_name" onchange="get_tees(this.value)">
                        <option value="">select a tee</option>
                        <?php get_courses() ?>
                    </select>
                </div>
                <div class='form-group'>
                    <select class='form-control' name='tee_played' onchange="get_rating(this.value)" id='txtHint'>
                    </select>
                </div>
                <div class="form-group">
                    <input type="text" class="form-control" name="cor_rating" placeholder="enter rating from above">
                    <input type="text" class="form-control" name="cor_slope" placeholder="enter slope from above">

                </div>  

所以,正如你所看到的,它只是一个创可贴。如果您想查看它的工作原理,请访问 http://www.havikmarketing.com并使用这些凭据登录:

EM:test@test.com 密码:测试123

然后进入设置(齿轮)并选择“新一轮”。浏览并输入乐谱...您将看到它如何完成所有操作。现在请注意,我现在只是将它设置为一个骨架......所以不对设计进行评判:)

再次感谢

关于php - 获取由选择选项填充的文本值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33269958/

相关文章:

php - 在 cakePHP 2.x 中使用虚拟字段

mysql - 如何强制Hibernate添加ID列来插入查询?

mysql - 使用 Sakila 的子查询问题

javascript - 使用另一个网页中的按钮更改一个网页中的 div

php - 使用 composer 安装 propel

php - 我应该在我的 php 项目中使用依赖注入(inject)吗?

php - Bootstrap 4 navwalker - 多级菜单

仅当 count() as col1 <> col2 时,mySQL 选择行

javascript - iOS 上的 JS formdata Chrome

performance - Stack Exchange如何如此快速地生成加载新页面?