skip to Main Content

Really struggling to get a relative path to work for an Ajax request.

From like.js I’m trying to get to likeunlike.php

Error message:

jquery-3.3.1.js:9600 POST http://localhost:8000/serverside/likeunlike.php 404 (Not Found)

File structure:

enter image description here

JQuery:

$(document).ready(function(){

    // like and unlike click
    $(".content").on("click",".like",function(){
        var id = $(this).attr("id");  // Getting Button id
        var split_id = id.split("_");

        var postid = split_id[1]; 
        var userid = split_id[2];

        // AJAX Request
        $.ajax({
            url: '../serverside/likeunlike.php',
            type: 'post',
            data: {postid:postid,userid:userid},
            dataType: 'json',
            success: function(data){
                var likes = data['likes'];
                var type = data['type'];

                $("#likes_" + postid + "_" + userid).text(likes);

                if(type == 1){
                   $("#like_" + postid + "_" + userid).css("color","lightseagreen");

                }

                if(type == 0){
                    $("#like_" + postid + "_" + userid).css("color","#ffa449"); 
                }
            }
        });

    });

});

Have provided the index file as requested by one of the answers. Hope it helps.
Index.php:

<?php

include "detail/config.php";

?>

<html>
    <head>
        <title>Talk</title>
        <link href="style/style.css" type="text/css" rel="stylesheet" />
        <script src="jquery/jquery-3.3.1.js" type="text/javascript"></script>
        <script src="search/script/like.js" type="text/javascript"></script>
        <script src="search/check/check.js" type="text/javascript"></script>
    </head>

<script>

$(function() {
  $('form').on("submit", function(e) {
    e.preventDefault();
    $('#error').text(""); // reset
    var name = $.trim($("#search").val());
    if (name.match(/[^a-zA-Z0-9 ]/g)) {
      $('#error').text('Please enter letters and spaces only');
      return false;
    }
    if (name === '') {
      $('#error').text('Please enter some text');
      return false;
    }
    if (name.length > 0 && name.length < 3) {
      $('#error').text('Please enter more letters');
      return false;
    }

    $.ajax({
      url: 'search/search.php',
      method: 'POST',
      data: {
        msg: name
      },
      dataType: 'json',
      success: function(response) {

      $(".content").html("")
      $(".total").html("")

        if(response){
          var total = response.length;
          $('.total') .append(total + " Results");
         }

        $.each(response, function() {
          $.each($(this), function(i, item) {

            var mycss = (item.Type == 1) ? ' style="color: #ffa449;"' : '';
            $('.content').append('<div class="post"><div class="post-text"> ' + item.MessageText + ' </div><div class="post-action"><input type="button" value="Like" id="like_' + item.ID + '_' + item.UserID + '" class="like" ' + mycss + ' /><span id="likes_' + item.ID + '_' + item.UserID + '">' + item.cntLikes + '</span></div></div>');
          });
        });
      }
    });
  });
});


</script>

<body>

<form action="index.php" method="post" id="myForm" autocomplete="on"><pre>

<input name="msg" id="search" type="text" autofocus value= "<?php if(isset($_POST['msg'])) { 
 echo htmlentities ($_POST['msg']); }?>"></input> <span id="error"></span>

<input type="submit" style="border:0; padding:0; font-size:0">

</pre></form>

<div class="total">
</div>

<div class="content">
</div>

</body>
</html>

2

Answers


  1. Chosen as BEST ANSWER

    I have resolved the situation by:

    1. Making my directory structure simpler as well as the file names. It was over-engineered. I have my root folder now and then just one level below that for folders.

    2. The Ajax url path, which I was struggling with was amended to 'serverside/like.php', which picked up the php file but nothing happened.

    3. Reviewing the like.php code I had an include("../con/config.php") without a ';'. I added this and it's now working fine.

    Thank you for everyone's help. Much appreciated as always.

    New folder structure:

    enter image description here


  2. jquery / js has no information about his location.

    this should be work.

    var base_url = window.location.origin;
    
    // like and unlike click
        $(".content").on("click",".like",function(){
            var id = $(this).attr("id");  // Getting Button id
            var split_id = id.split("_");
    
            var postid = split_id[1]; 
            var userid = split_id[2];
    
            // AJAX Request
            $.ajax({
                url: base_url + '/search/serverside/likeunlike.php',
                type: 'post',
                data: {postid:postid,userid:userid},
                dataType: 'json',
                success: function(data){
                    var likes = data['likes'];
                    var type = data['type'];
    
                    $("#likes_" + postid + "_" + userid).text(likes);
    
                    if(type == 1){
                       $("#like_" + postid + "_" + userid).css("color","lightseagreen");
    
                    }
    
                    if(type == 0){
                        $("#like_" + postid + "_" + userid).css("color","#ffa449"); 
                    }
                }
            });
    
        });
    
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search