Answer the question
In order to leave comments, you need to log in
How to extract array from mysql json?
There is a code
$(".send-id-form").submit(function(){
var data = {
"action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "POST",
dataType: "json",
url: "../modules/configurate/json.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
$(".test-ajax-js").html(
"Favorite beverage: " + data["Name"] + "<br />Favorite restaurant: " + data["Descrpition"] + "<br />Gender: " + data["id"]
);
console.log("Form submitted successfully.\nReturned json: " + data["json"]);
}
});
return false;
});
if (is_ajax()) {
if (isset($_POST["action"]) && !empty($_POST["action"])) { //Checks if action value exists
$action = $_POST["action"];
switch($action) { //Switch case for value of action
case "test": test_function(); break;
}
}
}
//Function to check if the request is an AJAX request
function is_ajax() {
return isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest';
}
function test_function(){
$id = $_POST['id_edit_now'];
$data = array();
$send = mysql_query("SELECT * FROM structure WHERE id = '$id'");
while($row = mysql_fetch_assoc($send)){
$data[] = $row;
}
echo json_encode($data);
}
Answer the question
In order to leave comments, you need to log in
If you only need one field, do SELECT myField FROM ...
insteadSELECT *
isset($_POST["action"]) && !empty($_POST["action"])
- redundant construction, it is enough !empty($_POST["action"])
in empty() to have its own built-in isset().
php.net/manual/en/function.mysql-query.php
Warning
This extension has been deprecated since PHP 5.5.0 and will be removed in the future.
php.net/manual/en/ref.pdo-mysql.php
You are selecting all columns in a table. If you want to display only the necessary data, use the construct:
$send = mysql_query("SELECT `FIELD` FROM structure WHERE id = '$id'");
$data = $row['FIELD'];
or
$data = $row['0'];
$id = $_POST['id_edit_now'];
$data = array();
$send = mysql_query("SELECT * FROM structure WHERE id = '$id'");
https://www.google.ru/webhp?sourceid=chrome-instan...
phpfaq.ru/pdo
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question