1

I have been Googling this for a while now, a brief back ground I am a novice at PhP and a newbie on JQuery UI. I appreciate the PhP I am using is now depricated however I am "fixing" up a MySQL database UI for work to make my life easier (in the long run) hence the reason I taught myself PhP in the first place (work does not have any cash to get it fixed and there response is Oh well rewrite the whole thin using a excel spreadsheet. The closest fix I could find was a post here How can I pass PHP variable to Datepicker in jQuery?

However no joy. I have no issues using it to Create / Read and Delete my problem comes with update as the existing data is not assigned to the JQuery id to input. The variable I am using works to populate to text box just not the value that is posted.....grrr! My whole script is 1900 lines long so hopefully posting the relevant bits will be enough. My header Jquery code and my update code from my form:

enter code here

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness    /jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<!-- 3 datepicker javascripts -->
<script> language ="javascript">
$(function() {
$( "#datepicker" ).datepicker();
$('#userDatedr').datepicker({dateFormat:"dd MM yy",
            altField: '#drDate', altFormat: 'yy-mm-dd'});
                //Set javascript date to php variable 
            var phpDate = "<?php echo $grab_staff[driver_exp]?>";
});


                $("#drDate").datepicker("setDate",phpDate);
</script>
<?php
<body>
<html>

print("<tr><td id='employee_left'>Drivers Licence:</td><td>
            <b>Number:</b>&nbsp;<input type='text' name='driver_num'            value='$grab_staff[driver_num]' maxlength='15'     /><br />");
            print("<tr><td id='employee_left'><b>Expiry:</b>&nbsp;</td><td>    <input type= 'text' value='$grab_staff[driver_exp]'     id=\"userDatedr\" maxlength='10'><input type=\"text\" id=\"drDate\"     name=\"drDate\">");

?>
</body>
</html>

To clarify, datepicker works when editing if I select a new date, i want to be able to re-post the existing date from the data base and update without the user having to select the date again from the datepicker calendar keeping in mind this form has 4 different date fields, date of birth, drivers license expiry date, original induction date and last induction date. The current fields above the code refers to is the drivers expiry date. The value that is posted will be changed from text to hidden once I have finished fault finding so at present it gives two fields, the first field displays the original date from the date base, which if edited with datepicker changes to the new date the second text box which will be hidden is blank if the date is not changed using the datepicker. Any help / suggestions will be appreciated Cheers

Jase

2 Answers 2

0

<script> language = "javascript"> should be something like <script type="text/javascript"> code here </script>.

$(function() { your code here } not sure what youre trying to do here but i would just put it in a document ready like this:

also don't declare your variable inside the object you send to the datepicker better do it above

$(document).ready(function() {
        //Set javascript date to php variable 
        var phpDate = "<?php echo $grab_staff[driver_exp]?>";
        $( "#datepicker" ).datepicker();
        $('#userDatedr').datepicker({dateFormat:"dd MM yy",
            altField: '#drDate', altFormat: 'yy-mm-dd'});
        });
        $("#drDate").datepicker("setDate",phpDate);
});

But maybe first start by checking the console (press f12 click on console) see what errors appear.

I would suggest not trying to get a whole thing working at once try to do it part by part for example first see if you can even alert the php var in javascript like this:

<script type="text/javascript"> 
     var phpDate = "<?php echo $grab_staff[driver_exp]?>";
     alert(phpDate);
</script>

If you see the correct date okay good go to the next step check if the datepicker is working in general by first testing the first one like this:

<script type="text/javascript>
    $(document).ready(function() {
        $( "#datepicker" ).datepicker();
    });
</script>

If this works go to the next step and work like this so you dont have to fix 300 errors at once but you can just make it part by part works alot easier

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

9 Comments

herriekrekel, thanks a lot for taking the time to respond. By following your fault finding info I have found that the variable is empty. should I be placing this script elsewhere as the variable value is not declared until halfway through the PhP script of this page? PS datepicker itself works a treat including posting to the data base
@Jase okay so now you know that the php array element $grab_staff[driver_exp] is propably not working. Is driver_exp a constant you have defined somewhere in your php? or are you trying to get an element of an associate array in which case it should be $grav_staff['driver_exp'] or if driver_exp is a variable it should be $grab_staff[$driver_exp]; if you show the php part where you create this array it would help
@Jase can you add the part where you fill this array, put a var_dump($grab_staff); die(); see what it returns can't really help you where this goes wrong without the part where you actually fill it :P
'driver_exp' is an element of an associate array, I have changed the code to add the single quotes and not having much luck. I have reloaded the page using the alert(phpdate); which returns an empty OK popup (which I assume should be echoing the the variable.
@Jase correct it should which is why you need to find out why the element in this array is empty this is inside your php code not the javascript look for the part where the array should put the value in and put a var_dump($grab_staff); die(); there see what it returns than go from there
|
0

I have edited my original line of code:

print("<tr><td id='employee_left'><b>Expiry:</b>&nbsp;</td><td>    <input type= 'text' value='$grab_staff[driver_exp]'     id=\"userDatedr\" maxlength='10'><input type=\"text\" id=\"drDate\"     name=\"drDate\">");

to

print("<tr><td id='employee_left'><b>Expiry:</b>&nbsp;</td><td><input type= 'text' value='$grab_staff[driver_exp]' id=\"userDatedr\" maxlength='10'><input type=\"text\" value='$grab_staff[driver_exp]' id=\"drDate\" name=\"drDate\">");

and it now works, thanks to @herriekrekel for all your suggestions I learnt a few things along the way, note I am using php 5.3.3 so not sur how relevant this answer could be to other users.

Further to this I have also found that there is no need to declare the php variable in the header as part of the script, I commented out all the additional code suggested and it functioned as intended. So long as the php variable is declared in the input code it works as required; note the second text input needs to change to hidden in the production environment. Cheers

Jase

Comments

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.