JSON encode no output [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I've tried to look up different ways to get this json to output correctly but im not sure if im accessing the right variable in php/or the success function value.pTitle as well as how to I get the access to the other value out such as artTitle im failing somewhere not sure where or why.UPDATE fixed the php file added and an array $data[].
this is my php code.
$sqlPAQuery = "SELECT pTitle, GROUP_CONCAT(artTitle) AS
artTitle
FROM p
JOIN art ON art.pId = p.pId
GROUP BY pTitle";
if ($result=mysqli_query($conn,$sqlPAQuery))
{
$data = [];
while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$data[] = $row;
}
echo json_encode($data);
}
This is the outcome from the php encode of row:
[{"pTitle":"ent","artTitle":"11,12"},{"pTitle":"pro","artTitle":"10"},{"pTitle":"sports","artTitle":"1,13"}]
This is the html code:
<h3>Output: </h3>
<div id="output"></div>
<script id="source" language="javascript" type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: 'Data.php',
data: "",
dataType: 'json',
success: function(data)
{
$.each(data, function(index, value) {
var pageTitle = value.pTitle; //get name
$('#output').append("<b>pageTitle: </b>"+pageTitle+"<br/>");
}
});
});
Output should be:
pageTitle: ent
pageTitle: pro
pageTitle: sport
FIXED THE PHP FILE WORKS

If you want to proces the json like that this is what you need to do:
if ($result=mysqli_query($conn,$sqlPAQuery)) {
$data = [];
while ($row=mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$data[] = $row;
}
echo json_encode($data);
}

Related

Upload data to database with JSON [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
How can I upload data to database with AJAX, JSON and PHP?
Here is the code.
AJAX
function saveToTheDB(ratedIndex) {
$.ajax({
url: 'fetch.php',
method: 'POST',
cache: 'false',
dataType: 'json',
data: {
ratedIndex: ratedIndex
},
success: function(data) {
console.log(data);
},
error: function(error) {
console.log(error);
}
});
}
PHP
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
require_once 'includes\dbh.inc.php';
$rate = $_POST['ratedIndex'];
if(isset($_GET['userid'])){
if($db->query(" INSERT INTO `recipes_ratings` (`recipe_rating_id`, `recipe_id`, `user_id`, `rating`)
VALUES (null, 3 , 8, '".$rate."')
"))
}
echo json_encode($rate);
}
What have I done wrong?
Can some one help me to solve this problem? Thank you very much!
EDIT
ERROR
I get back a full object
As the response points you have syntax error, here I refactored your code in order to work.
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
require_once 'includes\dbh.inc.php';
$rate = $_POST['ratedIndex'];
if (isset($_GET['userid'])) {
if($db->query("INSERT INTO `recipes_ratings` (`recipe_rating_id`, `recipe_id`, `user_id`, `rating`) VALUES (null, 3 , 8, '".$rate."')")) {
// implementation if query is successful
}
}
echo json_encode($rate);
}
JFYI: Avoid directly placing the input variables into the query, you should use Prepared Statements.

How to send an id broughted by json from ajax to a php post [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have this ajax to show a html table, the thing is, i have a method on viewmap.php to show the ubication depending the id you clicked at the table, but i need to send the uid to viewmap.php so the php can search it at the database, and i don't extactly know how to do that
$.ajax({
type: "GET",
url: "editinplace.php?tabla=1"
})
//Vector
.done(function(json)
{
json = $.parseJSON(json)
for(var i=0;i<json.length;i++)
{
$('.editinplace').append
(
"<tr><td class='id' value='uid'>"
+json[i].uid
+"</td><td>"
+json[i].name
+"</td><td>"
+json[i].ape
+"</td><td>"
+json[i].phone
+"</span></td><td class='editable' data-campo='status'><span>"
+json[i].status
+"</span><td>"
+"<a href='viewmap.php?uid=$uid'>View on map</a>"
+"</td>"
);
}
//
});
Is it not the uid you have from your json array?
"<a href='viewmap.php?uid="+json[i].uid+"'>View on map</a>"
If you want to pull it from php, you'd echo it like this
$.ajax({
type: "GET",
url: "editinplace.php?tabla=1"
})
//Vector
.done(function(json)
{
json = $.parseJSON(json)
for(var i=0;i<json.length;i++)
{
var uid = "<?php echo $uid; ?>";
$('.editinplace').append
(
"<tr><td class='id' value='uid'>"
+json[i].uid
+"</td><td>"
+json[i].name
+"</td><td>"
+json[i].ape
+"</td><td>"
+json[i].phone
+"</span></td><td class='editable' data-campo='status'><span>"
+json[i].status
+"</span><td>"
+"<a href='viewmap.php?uid="+uid+"'>View on map</a>"
+"</td>"
);
}
//
});
If you have shorthand PHP echo tags turned on, its simply -
"<a href='viewmap.php?uid=<?= $uid ?>'>View on map</a>"#
Otherwise its like this with full tags -
"<a href='viewmap.php?uid=<?php echo($uid); ?>'>View on map</a>"#

jquery select adding extra rows [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a small ajax call:
$.ajax({
type:'POST',
dataType: 'json',
url: 'inc/getfunctions.php?q='+l_id+'&func=load_po',
success:function(data){
//alert ('hi');
if (data.po_num) {
$('#po_num_s').append($('<option>').text('Select a PO').attr('value', 0));
var po_num = data.po_num;
var $subType = $("#po_num_s");
$.each(data, function () {
$subType.append($('<option></option>').attr("value", data.l_id).text(data.po_num));
});
}
}
});
it is apending 2 rows :
<'option value="11">112212<'/option>
<'option value="11">112212<'/option>
is the output
Thanks in advance
The $.each function will take an array or an object and iterate over its items, while providing the item key and value as parameters to the callback. Like so:
$.each(["aaaa", "bb", "ccc"], function(key, value){
console.log(value.length);
});
// Will output:
// 4
// 2
// 3
But you aren't using the each-loop for something useful, as you don't receive any item in the callback function. The only reason you're getting as few as 2 lines is that your data object only has 2 keys in it. Try adding another property to data at the same place as your commented alert, like so:
......
success:function(data){
//alert ('hi');
data.foo = "bar"
if (data.po_num) {
......
and watch the each-loop go three rounds.

Sending form using jQuery [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I'm trying to send a form using jQuery and it's not working. This code with a <a> tag works fine, but inside a form using the submit button doesn't.
$('#send-comment').click(function(){
var id_parent = $('#id_parent').val();
var comment_text = $('#comment-box').val();
var user_id = <?php echo $_SESSION['log_id']; ?>;
var new_id = <?php echo $id_new; ?>;
$.post( "dn-functions.php", { func: "registerComment", id_parent: id_parent, comment_text: comment_text, user_id: user_id, new_id: new_id })
.done(function( data ) {
alert( data );
});
});
Do you know what am I doing wrong?
$('#send-comment').click(function(e){
e.preventDefault();
var id_parent = $('#id_parent').val();
var comment_text = $('#comment-box').val();
var user_id = <?php echo $_SESSION['log_id']; ?>;
var new_id = <?php echo $id_new; ?>;
$.post( "dn-functions.php", { func: "registerComment", id_parent: id_parent, comment_text: comment_text, user_id: user_id, new_id: new_id })
.done(function( data ) {
alert( data );
});
});
I'm pretty sure you are submitting the form and your request is not being sent since you're leaving the page on form submit.
So pass the event and prevent the default action of form submit.

need advice and bug help please! (HTML and JS, maybe PHP and MySQL) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to make an auto complete forum (like the tags below) that works in lime-survey. I'm new to this so please explain everything like i'm 5 :)
My goals are:
auto complete to work
work with lime-survey
have an excel file or maybe a database using PHP/MySQL that anyone can manage by editing. It would only be ONE row. Please point me how to do this. I use a mac :)
Here is the code:
<!DOCTYPE HTML>
<html>
<body>
<div >
<input type="text" id="raw_input" width="100" height="30" name="box" onkeyup=show(this)>
</div>
<div id="drop_down" style="display:none">
<SELECT id=box size=3 name="box" onclick=show(this)></SELECT>
</div>
<script>
function drop_the_box() {
document.getElementById("drop_down").style.display = "none";
document.getElementById('box').length = 0;
if (!randomcharactersblablabla).test(document.getElementById("raw_input").value){
document.getElementById("drop_down").style.display="block";
var database = new Array("object_1","item_1","object_2","item_2");
var string = document.getElementById("raw_input").value;
for (var s = 0; s < database.length; s+= 1 ) {
var t += 1
if (database[s].indexOf(string) != 0) {
addItem(string[s],database[s]);
scan(streetArray[s],streetArray[s]);
}
}
}
}
function scan(x,y) {
var ghost_tag = document.createElement("ghost");
document.getElementById("box").options.add(ghost_tag);
ghost_tag.text = x;
ghost_tag.value = y;
}
function show(visable) {
document.getElementById("dropdown").value = visable.value;
}
</script>
</body>
</html>
Keep you data in mysql database. Create php file which will handle queries. Use jquery.ajax() to send queries and retrieve responses from php file.
Use this example
jQuery file
$('#search').change(function(){
var name = $('#search').val();
$.ajax({
type: 'POST',
url: 'request.php',
data: 'some data(may be variable)',
success: function(response){
$('#searach').val(response);
}
});
})
php file
if(isset($_POST['some_data'])){
$query = 'SELECT your_table_field FROM your_table WHERE your_table_field LIKE %$_POST['some_data']% LIMIT 1';
$result = mysql_query($query);
$myrow = mysql_fetch_array($result);
echo $myrow[0];
}

Categories