I am getting this error message at the console for line:
var dataString = \'search=\'+ searchid;
at following script:
$(function(){
$(".search").keyup(function() {
var searchid = $(this).val();
var dataString = \'search=\'+ searchid;
if(searchid!=\'\') {
$.ajax({
type: "POST",
url: "classes/search.php",
data: dataString,
cache: false,
success: function(html) {
$("#result").html(html).show();
}
});
}
return false;
});
});
What is wrong there?
I am getting this error message at the console for line:
var dataString = \'search=\'+ searchid;
at following script:
$(function(){
$(".search").keyup(function() {
var searchid = $(this).val();
var dataString = \'search=\'+ searchid;
if(searchid!=\'\') {
$.ajax({
type: "POST",
url: "classes/search.php",
data: dataString,
cache: false,
success: function(html) {
$("#result").html(html).show();
}
});
}
return false;
});
});
What is wrong there?
Share Improve this question edited Sep 6, 2016 at 4:17 nateyolles 1,9315 gold badges18 silver badges23 bronze badges asked Sep 6, 2016 at 3:03 mvascomvasco 5,1017 gold badges68 silver badges137 bronze badges 11-
Do you mean
'\search\' + searchid;
? – Andrew Li Commented Sep 6, 2016 at 3:04 -
\'search=\'
<- what's that? – zerkms Commented Sep 6, 2016 at 3:06 -
Just a wild guess, but maybe you have an invalid or unexpected token. BTW, many problems in posing strings like this can be avoided by using template strings:
`search=${searchid}
`. – user663031 Commented Sep 6, 2016 at 3:08 - 1 Why do you have slash before every quote character? What is it there for? – zerkms Commented Sep 6, 2016 at 3:15
- 1 @num8er If you didn't care what I thought, you wouldn't have asked me in the first place. You judged others, assuming that those that were downvoting, were not helping. Don't dish it out it you can't take it back. – Daedalus Commented Sep 6, 2016 at 3:32
2 Answers
Reset to default 2There is no need to escape quotes in this situation:
var dataString = 'search=' + searchid;
And as mented the next line also has issue. Remove the slashes.
I think when user types text You want to do request to somewhere.
So in this case even: var dataString = 'search='+searchid;
will not help, because it needs to be urlencoded (what if there is some character, for example: &
that breaks the param list?)
so I remend to do following, let jquery handle all stuff:
$(function(){
$(".search").keyup(function() {
// post or get
$.post(
'/url/to/search',
{search: $(this).val()},
function(response) {
//here You handle response
});
});
});
Here is fix:
$(function() {
$('input.search').keyup(function() {
if($(this).val() == '') {
return; // if search query is empty don't proceed down
}
$.post(
'classes/search.php',
{search: $(this).val()},
function(response) {
$('#result').html(response).show();
});
});
});