1

I'm new to php. I am developing a form with the following code:

<td>Severity:</td>
<td>
    <?php $dbc = mysql_connect('localhost', 'root', ''); // root connection
    mysql_select_db('ticket', $dbc); // db selection
    $query = "SELECT id, severity FROM severity" ; // retrieving value from table severity in db
    $result = mysql_query($query, $dbc); // 
    echo'<select name="sever">';
    echo '<option value="Default"> Please Select Severity </option>';
    while($row = mysql_fetch_assoc( $result ))
    {
        echo '<option value="'.$row['id'].'">' . $row['severity'] . '</option>';
    }
    echo '</select>';
?>
</td>

In the database i have created 3 severity levels with values as follows:

Table Severity (ID, Severity, Hours) values (1, 'Critical', '12'), (2, 'Major', '24'),(3, 'Minor', '36');

And i have a date function in the form

<td> Expected Completed Date: </td>
        <td> <input name="exc_date" style="width:150px" type="text" /> </td>

As soon as i select severity i want the expected completed date to be auto generated based on the selection of severity.

I am bit ambiguous as to how i need to generate the php code in the form for Expected Completed Date.

Kindly do help me and thanks in advance for your suggestions.

4

2 回答 2

1

纯 Javascript 解决方案(无 jQuery)

jsFiddle 演示

使用data-hours属性输出每个选项的小时数:

echo '<option data-hours="'.$row['hours'].'" value="'.$row['id'].'">' . $row['severity'] . '</option>';

还要给出 的<select>和 ID server,然后使用 Javascript 进行更改:

window.onload = function(){
    document.getElementById("server").onchange = function(){
        var timeObj = document.getElementById("exc_date");
        if(this.value != 'Default'){
            var hours = this.options[this.selectedIndex].getAttribute('data-hours');

            var now = new Date();
            now.setHours(now.getHours() + (hours*1));
            var nowStr = now.getDate() + "/" + (now.getMonth()+1) + "/" + now.getFullYear();
            timeObj.value = hours + " hours, " + nowStr; 

        } else {
            timeObj.value = '';   
        }
    };
};

这会将带有 ID 的元素更新exc_date为小时数。

于 2013-06-10T11:07:56.157 回答
0

将 PHP 部分更改为此

<td>Severity:</td>
<td>
    <?php $dbc = mysql_connect('localhost', 'root', ''); // root connection
    mysql_select_db('ticket', $dbc); // db selection
    $query = "SELECT id, severity, hours FROM severity" ; // retrieving value from table severity in db
    $result = mysql_query($query, $dbc); // 
    echo'<select name="sever">';
    echo '<option value="Default"> Please Select Severity </option>';
    while($row = mysql_fetch_assoc( $result ))
    {
        echo '<option value="'.$row['id'].'" data-hours="'.$row['hours'].'">' 
             . $row['severity'] . '</option>';
    }
    echo '</select>';
?>
</td>

并添加 JavaScript – http://jsfiddle.net/8YCwb/

于 2013-06-10T11:09:05.997 回答