Receving notice: array to string conversion when using jquery autocomplete - php

I am using jquery autocomplete on a form and trying to do a simple echo of what is selected upon submitting a form entry to verify my data is being read correctly. I am receiving the following message:
Notice: Array to string conversion in C:\xampp\htdocs\New\search.php on line 4
Array
Search.php Contents:
<?php
$dest_name = $_GET["dest_name"];
echo ["dest_name"];
?>
Html contnts:
<body>
<form method="GET" action="search.php">
<div>
<input type="text" id ="destination" name="dest_name"/>
</div>
</form
</body>
autocomplete script
var destinations = [
{value: "49 Degrees North Ski Area",label: "49 Degrees North Ski Area",id: "1"},
{value: "Afton Alps",label: "Afton Alps",id: "2"},
{value: "Al Quaal Recreation Ski Area",label: "Al Quaal Recreation Ski Area",id: "3"},
{value: "Alpental",label: "Alpental",id: "4"},
{value: "Alpine Meadows",label: "Alpine Meadows",id: "5"},
];
$(document).ready(function() {
$("#destination").autocomplete({
source: destinations,
focus: function(event, ui) {
$("#destination").val(ui.item.label);
return false;
},
select: function(event, ui) {
$("#destination").val(ui.item.label);
$("#dest_id").val(ui.item.id);
return false;
}
});
$('#button').click(function() {
alert($("#dest_id").val());
});
});

In line 4 use have displayed the array index instead of that you have to echo the variable which you have declared in line 3.i.e.,
echo $dest_name;

On line 4 of your PHP file you are echoing an array (the square brackets can be used to create an array):
echo ["dest_name"];
If you want to echo the brackets, you need to wrap the entire line in quotes.
echo '["dest_name"]';

Related

jquery getting the text writed in the input type text

I'm looking for a solution but I haven't got lucky.
if I have this:
<input type="text" name="x" id="x" value="123"/>
and I have this in jquery.
$('#x').val()
it returns me -> 123 thats correct.
But if I want to edit that field firstly with 123 as the default value and I re-write to 321 and I make the same command in Jquery, im still having 123 instead of 321.
So the question is how can I get what I have written in the type text input.
Thanks you all!
Here my html code:
<input type="text" name="hotel_name" id="edit_hotel_name" class="edit_hotel_name w100p" value="<?php echo $hotel['hotel_name'];?>"/>
Here my jquery dialog processor:
$("#dialog_hotel_content_edit_" + id_hotel).removeClass("hidden").dialog({
dialogClass: "no-close",
title: "Editar Hotel",
height: "auto",
width: "auto",
open: function(){
get_corporations();
get_zones($("#edit_state_id").val());
},
buttons: [{
text: "Editar",
click: function() {
hotel_name = $(".edit_hotel_name").val()
alert(hotel_name);
$(this).dialog( "close" );
}
}
]
});
The task here is to edit some information through a dialog, so the information comes by the value attribute and then when I click the "Editar" button it gets the information written in the form.
Easy peasy in jQuery. Just use the .on('input' function
$('#x').on('input', function() {
//do whatever you need to in here
});
example on how to use this: JSFiddle
You can use keyup() or keypress() event to get the new value of your input after typing:
$('#x').keyup(function() {
console.log($(this).val())
}).keyup();
Fiddle Demo
Here is a working demo
$("#boxx").keypress(function() {
var $this= $(this);
window.setTimeout(function() {
$("div").text($this.val());
}, 0);
});

jQuery issue with PHP data and Highcharts

I'm having an issue with the X axis data which is dynamic as it seems to use all the values in the first column.
I'm doing the below:
<div id="season_data_block" style="display: none;">
<?php
foreach($champ_name as $champ_id => $stat_value) {
foreach ($stat_value as $cn => $cs) {
if($champ_id != 0) {
echo '"'.$cn.'",';
}
}
}
?>
</div>
This is putting the PHP data into a hidden div. Then I am using jQuery to access the text from that div.
<script type="text/javascript">
$(document).ready(function(){
var chart_data = $("#season_data_block").text();
$('#stats_chart').highcharts({
chart: {
type: 'column',
backgroundColor: "#F5F5F5",
},
title: {
text: ' '
},
xAxis: {
categories: [chart_data]
},
The issue I'm having is that when the data is inserted into the javascript xAxis, it shows all the php data on the first xAxis value then the rest is defaulted, like below.
First: "name 1","name 2","name 3",
Second: 2
Third: 3
Fourth 4
I want it to be
First: "name 1"
Second: "name 2"
Third: "name 3"
etc. Why is it doing that?
Thanks
Fixed it. I turned my PHP data into another array and then json_encode() that array into a javascript array. Simple :)

json data parsing with jquery and HTML

i am trying to parse json data from remote URL using Jquery. My data is in below format:
[
{
"UserId": "5",
"Name": "Syed",
"Lat": "23.193458922305805",
"Long": "77.43331186580654",
"EmailId": "syedrizwan#ats.in",
"LocationUpdatedAt": ""
},
{
"UserId": "98",
"Name": "Michael Catholic",
"Lat": "23.221318",
"Long": "77.42625",
"EmailId": "michaelcatholic#gmail.com",
"LocationUpdatedAt": ""
}
]
i have checked the data in json lint and it says it is correct data format. when i try to run it on my HTML page , it returns a blank page. My HTML code is a below:
<html>
<head>
<title>Jquery Json</title>
<script type="text/javascript" src="jquery.js"></script>
<script>
$(document).ready(function(){
$.getJSON('http://localhost/fbJson/json.php', function(results){
document.write(results.Name);
});
});
</script>
</head>
<body>
</body>
</html>
I am trying to retrieve names from the json string
results is an array of items, so you have to consider which item you want.
Normally, you'd go over the array in a loop, in a fashion similar to that:
$.getJSON('http://localhost/fbJson/json.php', function(results){
for(var i = 0; i < results.length; i++) {
console.log(results[i].Name);
}
});
There is an array of objects, you can reach them directly by using the index
$.getJSON('http://localhost/fbJson/json.php', function(results){
document.write(results[0].Name);
});
If you wish to iterate over the array you can use $.each and pass the results into it
$.each(results, function(key, val) {
document.write(val.Name);
});
Your json format is correct.
Use this code to access name in first row array:
document.write(results[0].Name);

live search with jquery, ajax, json, php

I am looking for a live search solution or jquery autocomplete using ajax to take data from a file (later from db)
Let's say i have this php file with data:
[
{ID: "1", "Name": "User 1"},
{ID: "2", "Name": "User 2"},
{ID: "3", "Name": "User 3"},
{ID: "4", "Name": "User 4"}
]
I found on the web this code it works but it scanns only wikipedia, how can i make is to scann my php file? http://jsfiddle.net/TzQJP/
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<title>Comperio Super Simple Instant Search</title>
</head>
<body>
<h1>Search Wikipedia!</h1>
<br />
<input id="searchterm" />
<button id="search">search</button>
<div id="results"></div>
<script>
$("#searchterm").keyup(function(e){
var q = $("#searchterm").val();
$.getJSON("http://en.wikipedia.org/w/api.php?callback=?",
{
srsearch: q,
action: "query",
list: "search",
format: "json"
},
function(data) {
$("#results").empty();
$("#results").append("<p>Results for <b>" + q + "</b></p>");
$.each(data.query.search, function(i,item){
$("#results").append("<div><a href='http://en.wikipedia.org/wiki/" + encodeURIComponent(item.title) + "'>" + item.title + "</a><br>" + item.snippet + "<br><br></div>");
});
});
});
</script>
<div style="position:absolute;bottom:0;right:0;text-align:right">
Fergus McDowall 2012<br>
<br>
</div>
</body>
</html>
By change "http://en.wikipedia.org/w/api.php?callback=?" path to your PHP file path you can accomplish it. Make sure that your JSON result it correctly formatted and a valid one.
Don't just take code snippets from the internet and use it. At least get an idea what it does. It may help you to extend the code as you want.
Why not use: http://jqueryui.com/autocomplete/
example from the same website:
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C"
];
$( "#tags" ).autocomplete({
source: availableTags //OR path to your PHP script
});
});
and you can have path to your PHP script instead of availableTags in source that returns
echo json_encode($array_of_items);
If you still want to use the code you provided, make sure you do echo json_encode($array); on your PHP script since response ajax is expecting should be type of json

Jquery Autocomplete "almost" working but list doesn't populate completely

I'm trying to use autocomplete in jquery, and it works with the demo data, but I haven't been able to make it work with my own data source. I'm trying to write a mailer where the user just enters a few letters of a person's name, and the contacts database helps autocomplete so that the corresponding emails show up in the "To" field.
I've included the following files:
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
My jQuery code in document.load is below:
$(function() {
function log( message ) {
$('#to').append(message);
console.log(message);
}
$( "#search" ).autocomplete({
source: "search.php",
minLength: 2,
select: function( event, ui ) {
log( ui.item? ui.item.email : "NO" );
}
});
});
And my HTML is:
<div class='ui-widget'><input type='text' class='medium' id='search' /></div> <br />
To:<br />
<div class='ui-widget'> <textarea type='text' style='width:80%; height:24px;' id='to' class='ui-widget-content'></textarea></div>
The result of search.php is fine as far as json is considered, here's a sample of the output when the letters "Ahmed" are pressed:
[{"email":"saddi#yahoo.com","name":"Ahmed Qasim"},{"email":"aaaab#alangari.com.sa","name":"Ahmed Abbas"},{"email":"mokhlef#yahoo.com","name":"Ahmed Sahdi"}]
I know I'm getting this response from search.php because I check Firebug and see it, but it doesn't show up below the search input field... Instead, what does show up is just a stump of a list... as in the image below.
But this exact same thing worked as expected when I used the demo code here: demo Why doesn't the list show up properly? Is there a limit on how much data can be displayed? I've pasted only 3 entries from the JSON output I got, but there were tens.
I think keune has the correct diagnosis. If you don't want to change the output of search.php, you could do something like this:
$( "#search" ).autocomplete({
source: function (request, response) {
$.ajax({
url: "search.php",
type: "POST",
dataType: "json",
data: { searchText: myQuery, maxResults: 10 },
success: function(data) {
var mappedData = $.map(data,
function(item) {
return {
label: item.name,
value: item.email,
id: item.email
};
});
response(mappedData);
}
});
}, ....
What you are returning from server contains email and name fields, jquery ui needs an value and label field.
[{"label":"Ahmed Abbas", value: "Ahmed Abbas"}]
label is what you see on autocomplete list, and value is the value you will get when you select an item.

Categories