0

We are using a form to submit the data and getting the response from our API the code below works fine -

if (0 == strlen($_POST['from_location'])) {
    $_response[] = 'Enter from location';
}
if (0 == strlen($_POST['to_location'])) {
    $_response[] = 'Enter to location';
}

try {
        $_fromDate=DateTime::createFromFormat('m/d/Y',$_POST['from_date']);
        $_toDate =  DateTime::createFromFormat('m/d/Y', $_POST['to_date']); 
        $_interval = $_fromDate->diff($_toDate); //Line 21 where error comes
        $_noOfDays = $_interval->format('%d');
}catch(Exception $e){
    $_response[] = $e->getMessage();
}

Corresponding JS code used in our HTML submit page is -

function testAjaxCall(formobj) {
                $.post(
                    formobj.action,
                    $(formobj).serialize(),
                    function (response, status, jqXhr) {
                        alert(response);
                    },
                    'html'
                );
            }

The above code works fine and gives us the response from API.

However, when we try to change the JS code to -

function testAjaxCall(formobj) {
                $.post(
                    formobj.action,
                    $(formobj).serialize(),
                    function (response, status, jqXhr) {
                        //alert(response);
                        location.href = 'hotel_search.php';
                    },
                    'html'
                );
            }

We get the following Notices and a fatal error the notices are removed by using isset for our input fields. The dates need to be send back in Y/m/d format as thats how we receive the response. I have already gone through the Stackoverflow and googled it as well however, none of the solutions are working for us.

Notice: Undefined index: from_location in /home/developeriq/public_html/doylesweb/hotel_search.php on line 11

Notice: Undefined index: to_location in /home/developeriq/public_html/doylesweb/hotel_search.php on line 14

Notice: Undefined index: from_date in /home/developeriq/public_html/doylesweb/hotel_search.php on line 19

Notice: Undefined index: to_date in /home/developeriq/public_html/doylesweb/hotel_search.php on line 20

Fatal error: Call to a member function diff() on a non-object in /home/developeriq/public_html/doylesweb/hotel_search.php on line 21

Can anyone please help and see what we are doing wrong.

Thanks

Rajnish G
  • 237
  • 3
  • 8
  • You're building the $_response array if $_POST values aren't set, but you're still trying to use values from $_POST in calls to DateTime::createFromFormat() regardless – Mark Baker Mar 14 '16 at 19:11
  • what is `formobj` here? – ameenulla0007 Mar 14 '16 at 19:12
  • The problem is not in your date difference implementation - it's that you can't send POST variables with window.location.href. Try submitting a form, as these answers suggest: http://stackoverflow.com/questions/2367979/pass-post-data-with-window-location-href. – Sensei James Mar 14 '16 at 19:13
  • Thanks I will go through the same – Rajnish G Mar 14 '16 at 19:26
  • Its solved in my code if added the following condition to deal with the format - if ($_toDate instanceof DateTime && $_fromDate instanceof DateTime) { $_interval = $_fromDate->diff($_toDate); $_noOfDays = $_interval->format('%d'); } – Rajnish G Mar 15 '16 at 09:02

0 Answers0