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.

5
  • 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 for more details. Commented Jun 10, 2013 at 11:00
  • 1
    use javascript/jquery for on option select data changes Commented Jun 10, 2013 at 11:00
  • 1
    The mysql_* functions are deprecated as of PHP 5.5 and will be removed. Use mysqli or PDO instead. Commented Jun 10, 2013 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: w3schools.com/ajax Commented Jun 10, 2013 at 11:02
  • 1
    @MarkoĆilimković "pretty good" and "w3schools" in the same sentence is an error. see w3fools.com for the reasons. Commented Jun 11, 2013 at 7:50

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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.
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');
See my updated demo and code, I've added the ability to show the date that it will be completed.
thanks for the code. Is there a way i can get the month name instead of month number?
Yeah, have a look at this answer: stackoverflow.com/questions/1643320/…
|
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/.

2 Comments

Gedrox the code which you have given is not working. Even on jsfiddle i am not getting the date.
Changed "Hours" to "hours" in SQL. Did you try changing severity? What JS errors are you getting?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.