Data is not display after insert through bootstrap modal with ajax in php mysqli.
jQuery Ajax code:
$(document).ready(function() {
fetch_data();
function fetch_data() {
var action = "fetch";
$.ajax({
url: 'action.php',
method: 'post',
data: {action: action},
success: function (data) {
$("#all_table_data").html(data);
}
});
}
});
function addData() {
var formData = new FormData($('#employee_insert_form')[0]);
formData.append('action', 'add');
$.ajax({
method: 'post',
url: 'action.php',
data: formData,
processData: false,
contentType: false,
cache: false,
success: function (response) {
$("#all_table_data").html(response);
$('#empInsert').modal('hide');
fetch_data();
}
});
}
Your PHP code never return anything to your view. So try to echo result of select * from tbl_employee after the insertion
i want to post an array from java script to php by ajax. But i don't know how do that, especially send it to php function like controller class. Correct me if i'm wrong, this is my java script source, as a function to send an array :
<script>
function send(){
var obj = JSON.stringify(array);
window.location.href = "post.php?q=" + obj;
}
</script>
i was try, but still fail. really need help..
As described in the JQuery API documentation, you can use
var rootPath="http://example.com/"
var jsonData = $.toJSON({ q: array });
var urlWS = rootPath + "post.php";
$.ajax({
url: urlWS,
type: "POST",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: jsonData,
success: function(result) {
// do something here with returned result
}
});
var array= [];
array[0] = 'hi';
array[1] = 'hello';
$.ajax({
url: 'http://something.com/post.php',
data: {array: array},
type: 'POST'
});
try like this,
var data_to_send = $.serialize(array);
$.ajax({
type: "POST",
url: 'post.php',
data: data_to_send,
success: function(msg){
}
});
or
you can pass as json like below,
$.ajax({
type: "POST",
url: 'post.php',
dataType: "json",
data: {result:JSON.stringify(array)},
success: function(msg){
}
});
var arr = <?php echo json_encode($postdata); ?>;
ajax: {
url:"post.php"
type: "POST",
data: {dataarr: arr},
complete: function (jqXHR, textStatus) {
}
You can try this .this will work
example
ajax code:
$.ajax({
url: 'save.php',
data: {data: yourdata},
type: 'POST',
dataType: 'json', // you will get return json data
success:function(result){
// to do result from php file
}
});
PHP Code:
$data['something'] = "value";
echo json_encode($data);
I want to redirect page to URL that return from PHP script.
That when user created a post i want to return value of mysql_insert_id() for post_id.
$('input#submitButton').click( function() {
$.ajax({
url: 'newpost.php',
type: 'post',
dataType: 'json',
data: $('form#myForm').serialize(),
success: function(data) {
window.location.replace("http://localhost/?post_id=...");
}
});
});
I looked here but not found what i want.
Link
In your php script do :
echo json_encode(array(
'id' => $THE_ID
));
then here do:
$('input#submitButton').click( function() {
$.ajax({
url: 'newpost.php',
type: 'post',
dataType: 'json',
data: $('form#myForm').serialize(),
success: function(data) {
window.location = "http://localhost/?post_id=" + data.id;
}
});
});
});
I have a simple php file that decode my json string, passed with ajax, and stamp the result, but I can't keep the $_POST variable, why???
I try to inspect with fireBug and I can see that the POST request is sent correctly, when the php script is called, he respond Noooooooob to me, it seem any POST variable is set.
All I want is to have my array =)
JSON String generated by JSON.stringify:
[
{
"id":21,
"children":[
{
"id":196
},
{
"id":195
},
{
"id":49
},
{
"id":194
}
]
},
{
"id":29,
"children":[
{
"id":184
},
{
"id":152
}
]
},
...
]
JavaScript
$('#save').click(function() {
var tmp = JSON.stringify($('.dd').nestable('serialize'));
// tmp value: [{"id":21,"children":[{"id":196},{"id":195},{"id":49},{"id":194}]},{"id":29,"children":[{"id":184},{"id":152}]},...]
$.ajax({
type: 'POST',
url: 'save_categories.php',
dataType: 'json',
data: {'categories': tmp},
success: function(msg) {
alert(msg);
}
});
});
save_categories.php
<?php
if(isset($_POST['categories'])) {
$json = $_POST['categories'];
var_dump(json_decode($json, true));
} else {
echo "Noooooooob";
}
?>
Your code works if you remove dataType: 'json', just tested it.
$('#save').click(function() {
var tmp = JSON.stringify($('.dd').nestable('serialize'));
// tmp value: [{"id":21,"children":[{"id":196},{"id":195},{"id":49},{"id":194}]},{"id":29,"children":[{"id":184},{"id":152}]},...]
$.ajax({
type: 'POST',
url: 'save_categories.php',
data: {'categories': tmp},
success: function(msg) {
alert(msg);
}
});
});
it's work for me you can try this.
JavaScript
$('#save').click(function() {
$.ajax({
type: 'POST',
contentType: 'application/json',
url: 'save_categories.php',
dataType: 'json',
data: JSON.stringify({'categories': $('.dd').nestable('serialize')}),
success: function(msg) {
alert(msg);
}
});
});
you need to write the below code on save_categories.php
$_POST is pre-populated with form data.
To get JSON data (or any raw input), use php://input.
$json = json_decode(file_get_contents("php://input"));
$categories = $json->categories;
dataType is json, so jQuery posts this:
{"categories":"[{\"id\":21,\"children\":[{\"id\":196},{\"id\":195},{\"id\":49},{\"id\":194}]},{\"id\":29,\"children\":[{\"id\":184},{\"id\":152}]},...]"}
This is not standard urlencoded, so $_POST is empty.
You can set data to your complex structure, and jQuery will correctly encode it:
$('#save').click(function() {
$.ajax({
type: 'POST',
url: 'save_categories.php',
dataType: 'json',
data: $('.dd').nestable('serialize'),
success: function(msg) {
alert(msg);
}
});
});
And in php: $categories = json_decode(file_get_contents('php://stdin'));
Try this:
$('#save').click(function() {
var tmp = JSON.stringify($('.dd').nestable('serialize'));
// tmp value: [{"id":21,"children":[{"id":196},{"id":195},{"id":49},{"id":194}]},{"id":29,"children":[{"id":184},{"id":152}]},...]
$.ajax({
type: 'POST',
url: 'save_categories.php',
dataType: 'json',
data: 'categories=' + encodeURIComponent(tmp),
success: function(msg) {
alert(msg);
}
});
});
I changed just this line:
data: 'categories=' + encodeURIComponent(tmp),
because thats the way, how you have to write data there. I tested it, its working...
http://jsfiddle.net/YcK5X/
I'm wondering why this AJAX request doesn't return anything.
$.ajax({
type: 'POST',
url: '/echo/html',
data: 'Echo!',
success: function(data) {
$('#ajax').html(data);
},
dataType: 'text/html'
});
The data you want echo:ed must be supplied in a POST parameter named html:
$.ajax({
type: 'POST',
url: '/echo/html/',
data: {
'html': 'Echo!'
},
success: function(data) {
$('#ajax').html(data);
},
dataType: 'text/html'
});