2

I'm working on an assignment for my Web Engineering class and I'm using jQuery to make an AJAX request to my PHP file using POST. The assignment requires that "each time the [submit] button is pressed the form information must be displayed in a display portion of the same web page without reloading the page" which is why I'm using the AJAX request, but every time I try to make the AJAX request it just opens my PHP file instead up giving me a response like it's supposed to. I've looked at every resource I can find on the internet and nothing is helping me.

My javascript:

function submit() {
            $.ajax({
                type: "POST",
                url: "assign13.php",
                data: $('#registration_form').serialize(),
                dataType: "json",
                success: function(data) {
                    var jsonResponseObj = JSON.parse(data);
                    var table_item = document.getElementById("schedule_items");
                    var string = "";
                    for (var i = 0; i < jsonResponseObj.length; i++) {
                        string += 
                            ("<tr><td>" + jsonResponseObj[i].studName0 + (jsonResponseObj[i].hasOwnProperty('studName1') ? " and " + obj[i].studName1 : "") + 
                            "</td><td>" + jsonResponseObj[i].location + 
                            "</td><td>" + jsonResponseObj[i].time + 
                            "</td><td>" + jsonResponseObj[i].performace + 
                            "</td></tr>");
                    }
                    $("#schedule_items").html(string);
                }
            });
        }

(#schedule_items refers to tbody of a table)

Can someone please tell me what I'm doing wrong because I have no idea.

Edit: I'm not using a input type="submit", I'm using a regular button with onclick="submit()".

Also, my PHP was requested (I'm still new, so I'm sure it could look a lot better):

class festSched {
    public $studName0;
    public $studName1;
    public $location;
    public $time;
    public $performance;
}

$filename = "./data/fest_sched.json"; // storing my data as a json cause I like json

$file = fopen($filename, "w+");

$sched_json = file_get_contents($filename, true); // read the whole file into a string

$schedule = json_decode($sched_json, true);

if ($schedule == NULL) {
    $schedule = Array();
}

$new_entry = new festSched();
$new_entry->studName0 = $_POST['first_name'] . " " . $_POST['last_name'];
if ($_POST['performance'] == "duet") {
    $new_entry->studName1 = $_POST['first_name_2'] . " " . $_POST['last_name_2'];
}
$new_entry->location = $_POST['location'] . " Room " . $_POST['room'];
$new_entry->time = $_POST['time_slot'];
$new_entry->performance = $_POST['performance'] . " for " . $_POST['skill'] . " " . $_POST['instrument'];
array_push($schedule, $new_entry);

fwrite($file, json_encode($schedule));

fclose($file);



$str = json_encode($schedule);
echo $str;
Spencer Beaumont
  • 135
  • 1
  • 1
  • 7

1 Answers1

2

It sounds like the form is doing a native submit before it attempts to do the AJAX one, you need to prevent the form from submitting natively.

<script>
// Select Your Form
var form = document.querySelector('form') // Change this selector to something more specific if required

// Add a 'submit' listener to the form and prevent it from submitting
form.addEventListener('submit', function(event) {
  event.preventDefault()
})
</script>

EDIT: As others have pointed out, you can edit your button to prevent the form submission by adding the type attribute.

<button type="button" onclick="submit()">Submit</button>

cam
  • 3,179
  • 1
  • 12
  • 15
  • I'm not even using an input of type submit. I'm using a button and onclick. – Spencer Beaumont Jul 17 '20 at 03:00
  • 1
    If your `button` is the only button nested inside the `form` then it will attempt to submit the form. – cam Jul 17 '20 at 03:06
  • Okay, well I added the code you recommended, and it seems to be working now, but I'm still not getting a response from the server for some reason. – Spencer Beaumont Jul 17 '20 at 03:12
  • You've got a `success` handler, try adding an `error` one. – cam Jul 17 '20 at 03:14
  • Tried that using this but I'm still not getting anything at all https://stackoverflow.com/a/1637051/9513528 – Spencer Beaumont Jul 17 '20 at 03:20
  • Can you see the network request in the development inspector of your browser? Can you see a response there? – cam Jul 17 '20 at 03:26
  • I don't know exactly what I'm looking for there, but as far as I can tell I don't see anything. – Spencer Beaumont Jul 17 '20 at 03:38
  • You want to look at the Network tab and then filter by XHR, then try your form submission and watch it appear in the list of requests. From there you can look at the request you sent and check the response from the server. If there is no response from the server then it'll tell you that specifically. Additionally, if you console.log() as the first line inside your error handler does that get output to the console? That would mean that the server did response albeit with an error. – cam Jul 17 '20 at 04:15
  • I'm not getting anything at all. It seems like my code isn't recognizing my submit_form() (I renamed it) function. I've tried three different ways of adding the onclick listener, but it makes no difference. – Spencer Beaumont Jul 17 '20 at 06:38