获取由select选项填充的文本值

时间:2015-10-21 21:55:17

标签: php mysql ajax

我在这里非常需要。我已经去了我认识的每个知道php / mysql / ajax的人,但没有人可以提供帮助。

我正在尝试从我的dbase中获取一个输入字段,该数据来自两个不同的选择。情况如下:

在我的网站上建立一个高尔夫评分页面。用户将选择一个课程(选择#1),然后选择他们演奏的T恤(选择#2),然后禁用的文本输入将填充评级和斜率。评级和坡度是非常重要的b / c他们帮助找出用户的障碍。我能够很好地填充所有内容,但我无法在get_rating.php页面上的查询中找出正确的WHERE子句。有人可以帮我查询吗?

这是我的代码:

dbase设置:

这是从2个表中提取的,一个是课程表(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>

get_rating.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中正确传递一些东西吗?我究竟做错了什么?!?!请帮助!!

1 个答案:

答案 0 :(得分:0)

@ 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 PW:testing123

然后进入设置(齿轮)并选择新一轮&#39;。通过并输入一个分数...你会看到它如何拉动所有东西。现在请注意,我现在只是将它设置为骷髅......所以没有判断设计:)

再次感谢

相关问题