I'm getting problems with my php script and ajax. I have a unique home page where there is a central div that change content base on different clicks. I can't refresh my page in any case beacuse there is and audio player that can stop playing music. Now I have to execute the playlist creating by users. After that the user load all the attributes for its playlist, i submit the form to a php script to load the playist into the db. But I doesn't want to leave my home page. So I tried with an AJAX function in order to submit the form and load into the central div a message. I have already used AJAX for other goals with success, but in this case it's a little bit more difficult.
This is my form:
<div class="pers_playlist_add">
<h3> Aggiungi Playlist </h3>
<form class="pers_form_down" id="formaddplaylist" action="playlistaddscript.php" method="post">
Nome Playlist: <input type="text" name="playlist" required></input>
Descrizione Playlist: <input type="text" name="desc" required></input><br>
Privata: <input type="radio" name="priv" value="y" checked="checked">Si</input>
<input type="radio" name="priv" value="n">No</input>
<button id="created" style="float: right;" class="btn waves-effect waves-light" type="reset">Pulisci</button>
<button style="float: right; margin-left: 5px;" class="btn waves-effect waves-light" onClick="aggiungiplaylist()">Crea Playlist</button>
</form>
This is my php script
<?php
session_start();
$name = $_POST['playlist'];
$desc = $_POST['desc'];
$priv = $_POST['priv'];
$conn = mysqli_connect('localhost','root', '')
or die("Cannot connect to the dbms");
$query = mysqli_query($conn, "use my_alessiocorvagliatsn")
or die ("Cannot choose the tsn_db");
if($priv == 'y'){
$p = 1;
}
else{
$p = 0;
}
$email = $_SESSION['email'];
$q = "Insert into tsn_playlists (playlistname, playlistdescription, private, email) values ('$name', '$desc', '$p', '$email')";
$query = mysqli_query($conn, $q)
or die("Cannot add a playlist");
?>
This is my AJAX function:
function aggiungiplaylist() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("formaddplaylist").submit();
document.getElementById("central").innerHTML = this.responseText;
}
};
xhttp.open("GET", "addedplaylist.html", true);
xhttp.send();
}
The function work, but at the end of the processing takes me to the playlistaddscript.php with an (obviuous) blank page. If you need more information, please ask me. How can I solve it?