Jump to content

Filling in input value based on selection


winter-ale
Go to solution Solved by winter-ale,

Recommended Posts

Hello all, looking for some help here as I seem to be fairly stuck... this yields no results at all and I give up, I need help! :)

The user should be able to select an employee from a select field, and based on their selected we should be able to grab the start time and end time of that empoyee, which resides on the database.

 

My form:

<form action=".php" name="absence" id="absence" method="post">

Employee:
<select name="empid" class="clockinputs" required> 
<option value=""></option>
<option value='20'>Bob Jones</option>
<option value='13'>Bob Phones</option>
<option value='93'>Bob Lomes</option>
<option value='30'>Bob Somes</option>
<option value='107'>Bob Pomes</option>
<option value='74'>Bob Womes</option>
</select>

Shift Start: <input type="text" name="startshift" id="startshift" class="clockinputs" data-inputmask="'mask': '99:99:99'" required>
Shift End: <input type="text" name="endshift" id="endshift" class="clockinputs" data-inputmask="'mask': '99:99:99'" required>

<input type="hidden" name="eid" value="<?php echo $row{'EID'}; ?>">
<input type="submit" value="Save">
</form>

Javascript:

<script type="text/javascript">
    jQuery(document).ready(function(){
        jQuery('#empid').live('change', function(event) {
            $.ajax({
                url     : 'absence-get.php',
                type    : 'POST',
                dataType: 'json',
                data    : $('#absence').serialize(),
                success: function( data ) {
                       for(var id in data) {        
                              $(id).val( data[id] );
                       }
                }
            });
        });
    });
    </script>

And absence-get.php:

<?php
$e = $_POST['empid'];

$result = mysql_query("SELECT * FROM employees WHERE EID='$e' ");
$row = mysql_fetch_array($result, MYSQL_ASSOC)

$startshift = $row{'startshift'};
$endshift = $row{'endshift'};

$arr = array( 'input#startshift' => $startshift , 'input#endshift' => $endshift );
echo json_encode( $arr );
?>

Anyone spot anything? Thank you!

Link to comment
Share on other sites

You didn't give the <select> the "empid" ID your code expects.

 

Thanks, I've added this, but still no resuts.

 

 

In jQuery, the live() method is deprecated. The on() method is the current preferred method.
The mysql code you're using is deprecated. The preferred way is Mysqli or PDO.

 

 

Regarding the on() method, I am not familiar... if you have any details please let me know.

Regarding mysqli, I am aware, but not about to start mid way through a rather large application.

Link to comment
Share on other sites

Hi, I could be wrong, but is the issue to do with you using curly braces instead of square brackets in the following line in absence-get.php:

$startshift = 
$row{'startshift'}; $endshift = $row{'endshift'}; 

I would have thought it should be:

$startshift = $row['startshift']; $endshift = $row['endshift']; 

AND the same with this line in your form:

<input type="hidden" name="eid" value="<?php echo $row{'EID'}; ?>"> 

Good luck.

Edited by wezhind
  • Like 1
Link to comment
Share on other sites

your absence-get.php page doesn't show any code making a database connection and the items wezhind found would have been throwing php errors.

for the time being, forget about using ajax, just have your html form submit to the absence-get.php page and get both parts working without any errors, retrieving and echoing the expected data.

edit: the posted code, even after correcting the syntax errors that wezhind found, throws this fatal parse error - Parse error: syntax error, unexpected '$startshift' (T_VARIABLE) in your_path\absence-get.php on line 7 and doesn't even run. you are missing a ; on the preceding line.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.