Notsogood13
Notsogood13

Reputation: 109

Ajax script not working

I know I should be more specific about the problem but I don't know what it is but once I have an answer I fix the post.

Here is the problem simple ajax script but somehow it isn't working. I can't see any error on the code =/

jQuery part:

    $(document).ready(function()
{   
    var addFavPhp = '../functions/addfav.php';
    var orange = 'orange';
    var favLinkError = false;
    var favNameError = false;

    $('#addFavButton').click(function()
    {
        var favLink = $('.favLinkInput').val();
        var favName = $('.favNameInput').val();
        var fileName = $('.file').val();

        if(favLink=="")
        {
            $('.favLinkInput').css('border-color',orange);
            favLinkError = true;
        }

        if(favName=="")
        {
            $('.favNameInput').css('border-color',orange);
            favNameError = true;
        }

        if((favLinkError==false) && (favNameError==false))
        {
            $.post(addFavPhp,{favLink:favLink,favName:favName,fileName:fileName},function(addFav)
            {
                $('.favLinkInput,.favNameInput').val('').css('border-color','');
                $('.file option[value=""]').attr("selected", "selected");
                location.reload();
            });
        }
    });
});

and php part:

    <?php session_start(); 

    include('../functions/connect.php');

    if(!empty($_SESSION['username']))
    {
        $username = $_SESSION['username'];
    }else
    if(!empty($_COOKIE['PHPCOOKID']))
    {
        $cookie = htmlspecialchars(trim($_COOKIE['PHPCOOKID']));
        $explode = explode('-', $cookie);
        $username = $explode['0'];
    }

    $favLink = htmlspecialchars(trim($_POST['favLink']));
    $favName = htmlspecialchars(trim($_POST['favName']));
    $fileName = htmlspecialchars(trim($_POST['fileName']));

    $d = array($username,$favLink,$favName,$fileName);
    $req = $DB->prepare('INSERT INTO favs (username,favLink,favName,favFile) VALUE (? , ? , ? , ?)');
    $req->execute($d);

?>

What I figure out is that it stop working around $.post but I can't figure out why. And it works find on local server.

Thanks for any help.

Upvotes: 0

Views: 168

Answers (1)

VeXii
VeXii

Reputation: 3109

looks like the line var addFavPhp = '../functions/addfav.php'; is wrong. you have to use http urls when doing ajax. somthing like /path/to/addfav.php

Upvotes: 1

Related Questions