Reputation: 2953
Want to send a ajax request to php to create a new table dynamically. I have the following code:
Javascript:
var file = $('#'+option_file_name.id).prop('files')[0];
var form_data = new FormData();
form_data.append('file', file);
form_data.append('column_name', option_file_name.id.slice(12));
send_ajax_request(form_data);
function send_ajax_request(pdata){
console.log(pdata);
$.ajax({
url: "set_points_data.php",
data: pdata,
contentType: false,
processData: false,
dataType: "json",
type: 'post',
success: function (data) {
console.log(data);
},
error: function(a,b,c){
alert("something went wrong");
}
});
}
php script:
in the data.php file i connect to the database and have all the supporting functions.
<?php
include 'data.php';
$data = null;
if(isset($_FILES)){
$data = $_FILES;
$type = (string)$_POST["column_name"];
create_table($conn, $type);
return_ajax("succes");
}else{
return_ajax("error");
}
function create_table($conn, $name){
send_sql($conn, "CREATE TABLE `".$name."` (
options varchar(255));"
);
}
?>
UPDATE:
it turns out that if i change the following code to this the error disappears. see the new code below:
send_sql($conn, "CREATE TABLE testTable (
options varchar(255)
);");
but this is not what i want i need to create tables dynamically.
when ever i run run the ajax request i get the error something went wrong. But when i run the php script without the ajax call it works fine. and when i run the php script from the ajax call but without the create table function it also works fine. So i am not sure what is happening here. Hope this is clear if not let me know.
Upvotes: 0
Views: 53
Reputation: 58
After doing some testing i figured out that ajax is trying to return some data after doing the create table statement. Resulting in a error. if you change it to the following.
if(isset($_POST)){
$data = $_FILES;
$db_name = $_POST["tblname"];
return_ajax(create_table($conn, $db_name));
}else{
return_ajax("error");
}
function create_table($conn, $name){
send_sql($conn, "CREATE TABLE `".$name."` (
options varchar(255)
);");
return "database created";
}
This wil work. not sure what exactly happens but this fixes the ajax error.
Upvotes: 1