-1

I would like to get a form input data and use that to create another event in FullCalendar. In order to achieve this, I used php to get the post data and store it into javascript variable and then used moment.js to convert to the right dateTime format. However, I did this in an HTML file and now I would like to do this in a separate javascript file but it does not work. How can I use external javascript file to achieve the same result?

<html lang="en">
<head>
<meta charset="UTF-8">
    <title>test</title>

    <!-- Stylesheet files-->
    <link rel="stylesheet" href="css/main.css"/>
    <link rel="stylesheet" href="jquery-ui/jquery-ui.css"/>
    <link rel="stylesheet" href="css/fullcalendar/cupertino/jquery-ui.min.css"/>
    <link rel="stylesheet" href="css/fullcalendar/fullcalendar.css"/>
    <link rel="stylesheet" href="css/dateTimePicker/jquery.datetimepicker.css"/>
    <link rel="stylesheet" href="css/fullcalendar/fullcalendar.print.css" media="print"/>

    <!-- JavaScript files-->
    <script src="js/moment.js"></script>
    <script src="js/moment.min.js"></script>
</head>
<body>
    <!--Calendar-->
    <div id='top'>

        Language:
        <select id='lang-selector'></select>

    </div>

    <div id='print'>
        <button class="printIt">Print</button>  
    </div>

    <!--User input in the form-->
    <div id='userInput'>
        <form action="" method="post">
            Title: <input type="text" id="title" name="title"></input>
            Date and Time: <input type="text" id="datetimePick" name="dateTime"> </input>   
            <input type="submit" value="Submit">
        </form>
    </div>

    <?php
        //Will get the data from the form!
        $myDate =  $_POST['dateTime'];
        $myTitle = $_POST['title'];

    ?>




    <!-- xn-calendar-func.js will select this div to create a calendar-->
    <div id='calendar'></div>

    <script>
        var myTitle = <?php echo json_encode($myTitle);?>;
        var myDate = <?php echo json_encode($myDate); ?>;
        var myFixedDate = moment(myDate).format("YYYY-MM-DDTHH:mm:ss");
        if(myDate != null){
            document.write(moment(myDate).format("YYYY-MM-DDTHH:mm:ss")); //Convert to the ISO8701 format that FullCalendar Accepts!
        }

            $('#calendar').fullCalendar('renderEvent',{
                id: 99,
                title: myTitle,
                start: myFixedDate
             }, true);

    </script>

    <!--JavaScript files-->
    <script src="js/jquery-1.11.1.min.js"></script>
    <script src="js/application.js"></script>
    <script src="js/fullcalendar/lib/moment.min.js"></script>
    <script src="js/fullcalendar/lib/jquery.min.js"></script>
    <script src="js/fullcalendar/lib/jquery-ui.custom.min.js"></script>
    <script src="js/fullcalendar/fullcalendar.min.js"></script>
    <script src="js/fullcalendar/lang-all.js"></script>
    <script src="js/xn-calendar-func.js"></script>
    <script src="js/printThis/printThis.js"></script>
    <script src="jquery-ui/jquery-ui.js"></script>
    <script src="js/dateTimePicker/jquery.datetimepicker.js"></script>
</body>

So I want to move script tag inside a body tag to a javascript file (.js)

2
  • sry..i misunderstood. forgot whatever you read in my previous comment. Commented Aug 16, 2014 at 10:03
  • You can´t have PHP code inside a JS file. Unless you either let the server parse JS files as PHP or give the JS file a php extension. Commented Aug 16, 2014 at 10:10

1 Answer 1

1

You would have to continue to use script tags inline to output myTitle and myDate, but other than that you can simply just copy

var myFixedDate = moment(myDate).format("YYYY-MM-DDTHH:mm:ss");

if(myDate != null){
  document.write(moment(myDate).format("YYYY-MM-DDTHH:mm:ss")); //Convert to the ISO8701 format that FullCalendar Accepts!
}

$('#calendar').fullCalendar('renderEvent',{
  id: 99,
  title: myTitle,
  start: myFixedDate
}, true);

into a new text file, give it a .js extension and add it in a similar manner to the rest of your links at the bottom.

<script src="js/main.js"></script>
Sign up to request clarification or add additional context in comments.

1 Comment

so you are basically saying that I have to place var myDate and var myTitle in the HTML code to make it work right?

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.