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.

Dale
  • 10,384
  • 21
  • 34
MM0959
  • 167
  • 1
  • 1
  • 13
  • 1
    If you are new to PHP don't waste your time learning the `mysql_...()` library. It's deprecated and unmaintained and it'll possibly be removed in a future PHP major version. See [Choosing an API](http://es1.php.net/manual/en/mysqlinfo.api.choosing.php) for more details. – Álvaro González Jun 10 '13 at 11:00
  • 1
    use javascript/jquery for on option select data changes – swapnesh Jun 10 '13 at 11:00
  • 1
    [The mysql_* functions are deprecated as of PHP 5.5 and will be removed](http://php.net/mysql_query). Use [mysqli](http://php.net/mysqli) or [PDO](http://php.net/PDO) instead. – PointedEars Jun 10 '13 at 11:01
  • If you want something to work immediately, without a form doing an action, then you have to use ajax. A pretty good tutorial can be found on w3schools: http://www.w3schools.com/ajax/ – Marko Ćilimković Jun 10 '13 at 11:02
  • 1
    @MarkoĆilimković "pretty good" and "w3schools" in the same sentence is an error. see http://www.w3fools.com for the reasons. – STT LCU Jun 11 '13 at 07:50

2 Answers2

1

Plain Javascript Solution (no jQuery)

jsFiddle demo

Output the number of hours to each option, using a data-hours attribute:

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

Also give the <select> and ID of server, then use Javascript to do the change:

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 = '';   
        }
    };
};

This would update the element with an ID exc_date to the number of hours.

MrCode
  • 63,975
  • 10
  • 90
  • 112
  • thanks MrCode your code worked out. Is there a way i can keep in touch with you? If i get any queries i would like to share with you so that it would be helpful for me to gain knowledge. – MM0959 Jun 11 '13 at 06:33
  • Mrcode i have another query. I want JS to show me the date of the corresponding day say like i if selected the severity say Minor date is showing up as 36 but instead i want it to show 14/06/2013 with the corresponding time. I tried the following code but i could not make it. var currentDate = new Date(); timeObj.value = currentDate + this.options[this.selectedIndex].getAttribute('data-hours'); – MM0959 Jun 11 '13 at 08:07
  • See my updated demo and code, I've added the ability to show the date that it will be completed. – MrCode Jun 11 '13 at 08:15
  • thanks for the code. Is there a way i can get the month name instead of month number? – MM0959 Jun 11 '13 at 08:58
  • Yeah, have a look at this answer: http://stackoverflow.com/questions/1643320/get-month-name-from-date-using-javascript – MrCode Jun 11 '13 at 08:59
  • Mrcode my email id is msrinivasamahesh@gmail.com. I would like to speak clearly regarding this if that is ok with you. – MM0959 Jun 11 '13 at 09:34
0

Change the PHP part to this

<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>

And add JavaScript – http://jsfiddle.net/8YCwb/.

Gedrox
  • 3,592
  • 1
  • 21
  • 29
  • Gedrox the code which you have given is not working. Even on jsfiddle i am not getting the date. – MM0959 Jun 11 '13 at 06:28
  • Changed "Hours" to "hours" in SQL. Did you try changing severity? What JS errors are you getting? – Gedrox Jun 11 '13 at 07:43