0

I have tried to run an AJAX call within a PHP file, which sends data to another PHP file on the server that is taken from an html input when I press the submit button. Unfortunately when I click on the button, nothing happens, not even in the console. I have tried to debug the issue by creating a window.alert() of the input within the AJAX call, but it somehow shows a certain "object Object" result in the alert box. I have tried to change the path to see if the file is being detected or not, but it seems it is being detected as when I deliberately add a wrong path it throws a 404, and even tried to add echo calls to the PHP file being called but nothing appears. The only issue I can really think of now is something wrong with my implementation, but I'm not sure what it is.

Update: I have tried looking up certain questions such as here and here, but they don't work for me

Code from where the AJAX calls are being made:

<?php
require "../../../AutoLoader.php";

use mvcApplication\core\controllers\ControllerFactory;

?>
    <script>
        $(document).ready(function () {
            $('#submit').click(function () {
                $.ajax({
                    url: '../app/views/generic/deletefunc.php',
                    type: 'GET',
                    data: {
                        Id: $('#Id'),
                        value: "0"
                    },
                    processData: false
                });
            });
        });


    </script>
    <br>
    <br>
    <center>
        <h3>Enter Teacher ID:</h3><input type="text" id="Id" 
        placeholder="Input ID here"/>
    <br>
    <button class="col-sm-4" id="submit">Submit</button>
    </center>
    <br>

deletefunc.php (code where the data should be received)

<?php

require_once '../../../AutoLoader.php';

use mvcApplication\core\controllers\ControllerFactory;

function deleteTeacher($a)
{
    echo $a;
    $entity = ControllerFactory::initTeacherC();
    $entity->delete($a);

}


function deleteStudent($a)
{
    echo $a;
    /*$entity = ControllerFactory::initStudentC();
    $entity->delete($data);*/
}


function deleteCourse($a)
{
    echo $a;
    /*$entity = ControllerFactory::initCourseC();
    $entity->delete($data);*/
}


if (isset($_GET['Id']) && isset($_GET['value'])) {
    switch ($_GET['value']) {
        case "0":
            deleteTeacher($_GET['Id']);
            break;

        case "1":
            deleteStudent($_GET['Id']);
            break;

        case "3":
            deleteCourse($_GET['Id']);
            break;
    }
}
Muhammad Hamza
  • 823
  • 1
  • 17
  • 42
  • 1
    To get the value from an input box using jQuery, it should be: `$('#Id).val()`. You should also use `console.log()` when debugging to see what the variables contain. `alert()` don't show objects. – M. Eriksson Sep 27 '18 at 09:10
  • You can also add `success` and `error`-callbacks to your ajax request to check what the server actually returns. You can also do this by looking at the "network" tab in your browsers development tools. – M. Eriksson Sep 27 '18 at 09:13
  • `Id: $('#Id')` should be replaced with `Id: $('#Id').val()` – Praveen Kumar Purushothaman Sep 27 '18 at 09:14
  • change data: {Id: $('#Id'),value: "0"} to data:{'Id':$('#Id').val(),'value':'0'} – Munish Chechi Sep 27 '18 at 09:16
  • @MagnusEriksson Thanks, now atleast one issue is solved and now I can see the actual value inside the ajax call. Still no luck with why it isn't being passed on to the PHP file though. – Muhammad Hamza Sep 27 '18 at 09:20
  • @PraveenKumarPurushothaman Changed that and now the values are being fetched correctly by Ajax, but still unsure why it's not being sent to the PHP file. – Muhammad Hamza Sep 27 '18 at 09:22

1 Answers1

0

According to ajax docs http://api.jquery.com/jquery.ajax/;

processData (default: true) Type: Boolean By default, data passed in to the data option as an object (technically, anything other than a string) will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded".

I think you have to set processData to True on your ajax call because you're using GET (get needs querystring, url data) or just don't set it so it would be the default which is true.

Jerdine Sabio
  • 5,688
  • 2
  • 11
  • 23