0

I would like to convert a script I found ... But I can not. It works with Jquery 1.5. But it no longer works with Jquery 1.6.

Yet I tried jquery -migrate ( Migrate older jQuery code to jQuery 1.9+ ) . But without result ...

The script :

$.post('', form.serialize(), function(msg) {
        submitFlag = false;
        overlay.hide();
        $('span.errorIcon').remove();
        if (msg.success) {
            $('#formContainer, #mdiv, #contactboxfloat').fadeOut(function() {
                form.get(0).reset();
                $('#thankYou').fadeIn();
            });
        } else {
            $.each(msg, function(k, v) {
                var errorIcon = $('<span>', {
                    className: 'errorIcon'
                });
                var errorTip = $('<span>', {
                    className: 'errorTip',
                    text: v
                }).hide().appendTo(errorIcon);
                errorIcon.hover(function() {
                    errorTip.stop().fadeIn(function() {
                        errorTip.css('opacity', 1);
                    });
                }, function() {
                    errorTip.stop().fadeOut('slow', function() {
                        errorTip.hide().css('opacity', 1);
                    });
                });
                form.find('[name=' + k + ']').closest('.formRow').append(errorIcon);
                if ($(window).width() - errorIcon.offset().left > 240) {
                    errorTip.css('left', 30);
                } else {
                    errorTip.css('right', 30);
                }
            });
        }
    }, 'json');

php json

    if (!function_exists('json_encode'))
{
    function json_encode($a=false)
    {
        if (is_null($a)) return 'null';
        if ($a === false) return 'false';
        if ($a === true) return 'true';
        if (is_scalar($a))
        {
            if (is_float($a))
            {
                // Always use "." for floats.
                return floatval(str_replace(",", ".", strval($a)));
            }

            if (is_string($a))
            {
                static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
                return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
            }
            else return $a;
        }

        $isList = true;
        for ($i = 0, reset($a); $i < count($a); $i++, next($a))
        {
            if (key($a) !== $i)
            {
                $isList = false;
                break;
            }
        }

        $result = array();
        if ($isList)
        {
            foreach ($a as $v) $result[] = json_encode($v);
            return '[' . join(',', $result) . ']';
        }
        else
        {
            foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
            return '{' . join(',', $result) . '}';
        }
    }
} 

Can someone explain to me what prevents the script work?

and

if($_SERVER['REQUEST_METHOD'] == 'POST'){

    try{
        $contactForm->validate();
        $contactForm->send();
        $thankYou = $config['thankYouMessage'];

        if(IS_AJAX){
            echo json_encode(array('success'=>1));
            exit;
        }
    }
    catch(FormValidateException $e){
        if(IS_AJAX){
            echo json_encode($e->errors);
            exit;
        }
        else{
            $contactForm->populateValuesFromArray($_POST);
        }
    }
    catch(Exception $e){
        die('{"exception":"'.$e->getMessage().'"}');
    }

}
  • Can you give us a JSFiddle? – mwilson Sep 03 '15 at 00:05
  • It is unfortunately impossible . The script works with other PHP files. I make local tests . Thank you for your help ! –  Sep 03 '15 at 00:10
  • `.appendTo()` still works in the latest jQuery, why would you replace that? – nnnnnn Sep 03 '15 at 00:42
  • I thought the problem was there. After some modifications my script works, but this part of script to allow me to display a help icons if there is an error in the form does not. It worked with jQuery 1.4 ... I do not understand why! –  Sep 03 '15 at 00:51
  • Add an error handler to the Ajax call and see if it gets triggered. – epascarello Sep 03 '15 at 02:41

1 Answers1

0

There are two major tings, which can brake backwards compatibility of JQuery:

  • $() no longer the same as $(document), it's an empty jQuery object
  • jQuery will only parse VALID JSON, as opposed to things that look like JSON but aren't technically valid

So check, if your JSON is valid.

Source: https://forum.jquery.com/topic/jquery-1-8-1-and-jquery-1-4-backward-compatible-with-existing-jquery-1-3-2-custom-code

Shultc
  • 214
  • 1
  • 12
  • Also there are plugins for backwards compatibility http://stackoverflow.com/a/281671/2151834 – Shultc Sep 03 '15 at 00:16