我有一个 index.html 文件,其中包含一个触发 ajax 请求并启动 php 文件的按钮。php 文件将数据发送到服务器。问题是我不知道如何调试 php 文件,因为 echo 似乎不起作用(我可能做错了)。我只是想将各种变量回显到 html 文件中的 div 中,看看它们在程序中的不同点是什么。如果有更好的方法来做到这一点,请发布。谢谢你。
PHP
<?php
require_once 'connect.php';
?>
<?php
// Create table
$patchData = $_POST[mydata];
$encoded = json_encode($patchData);
$sql="INSERT INTO patches (patch_name)
VALUES ('{$encoded}')";
// Execute query
if (mysqli_query($con,$sql))
{
echo "Table updated successfully";
}
else
{
echo "Error updating table: " . mysqli_error($con);
}
?>
Javascript
var patch = {
"patch_name": "",
"sound_type": {
"synths": [ // each 'synth name' is a draggable div that plays an oscillator
// etc
],
"samplers": [
// same as synth only for divs that trigger audio files instead of oscillators
]
},
}
// START Save DOM nodes
function save(){
instrumentName = $('#instrument-name-form').val();
patch.sound_type.synths.length = 0;
patch.sound_type.samplers.length = 0;
$(".synth").each(function(){
var temp = $( "#" + this.id );
var pos = $(temp).position();
console.log(this.id);
patch.sound_type.synths.push({'synth_name':this.id,'xpos':pos.left,'ypos':pos.top});
});
$('.patch-list').html(patch.name);
patch.patch_name = instrumentName ;
console.log(patch);
$.ajax({
type: 'POST',
url: 'php/add.php',
data: {mydata: patch},
success: function(){
console.log('yay');
}
});
};
$('#save-synths').click(function(){
save();
})