Hello,
So i have been struggling for a while and i dont really think i can get this to work on my own, so here is a cry for help.
I've been busy with trying to make a search bar that will display the name and when you click on the desired result you get to that (dynamic) page.
What i've got working so far with alot of copy and paste and tutorials from the internet is..
The search bar actually see the name's in the database. You can click on it but then the trouble begins.
It will redirect you to item.php?id= how to put the 'id' in there that is combined to the name you searched for. i'll first post some code, and then explain what i think i need to do but cant figure out.
There is a input text field on the html page thats all fine with the id=keyword
And then we have the .js
Column 0 = id - INT AI
Column 1 = name - VACHAR
First what i think i need to do but also cant figure out is give the id a variable so i can ask for that in my javascript?
And then i need to add here
$('.item').click(function() { window.location = "../item.php?id=" + $('#keyword').val($id);
Or something like that? sorry for any typo's its kinda late and this is kinda driving me crazy.
Be aware this is one of my first times using any sort of code in this case php etc.. I'd love to know what im doing wrong but keep it polite, since i know this maybe can be done better by a 7 year old. Also there are a few lines that i dont understand yet, even not a bit. But im working on that.
So i have been struggling for a while and i dont really think i can get this to work on my own, so here is a cry for help.
I've been busy with trying to make a search bar that will display the name and when you click on the desired result you get to that (dynamic) page.
What i've got working so far with alot of copy and paste and tutorials from the internet is..
The search bar actually see the name's in the database. You can click on it but then the trouble begins.
It will redirect you to item.php?id= how to put the 'id' in there that is combined to the name you searched for. i'll first post some code, and then explain what i think i need to do but cant figure out.
There is a input text field on the html page thats all fine with the id=keyword
if (!isset($_GET['keyword'])) {
die();
}
$keyword = $_GET['keyword'];
$data = searchForKeyword($keyword);
echo json_encode($data);
function searchForKeyword($keyword) {
$db = getDbConnection();
$stmt = $db->prepare("SELECT name, id FROM `items` WHERE name LIKE ? ORDER BY name");
$keyword = $keyword . '%';
$stmt->bindParam(1, $keyword, PDO:ARAM_STR, 100);
$isQueryOk = $stmt->execute();
$results = array();
if ($isQueryOk) {
$results = $stmt->fetchAll(PDO::FETCH_COLUMN);
} else {
trigger_error('Error executing statement.', E_USER_ERROR);
}
$db = null;
return $results;
}
And then we have the .js
var MIN_LENGTH = 3;
$( document ).ready(function() {
$("#keyword").keyup(function() {
var keyword = $("#keyword").val();
if (keyword.length >= MIN_LENGTH) {
$.get( "auto-complete.php", { keyword: keyword } )
.done(function( data ) {
$('#results').html('');
var results = jQuery.parseJSON(data);
$(results).each(function(key, value) {
$('#results').append('<div class="item">' + value + '</div>');
})
$('.item').click(function() { window.location = "../item.php?id=";
var text = $(this).html();
$('#keyword').val(text);
})
});
} else {
$('#results').html('');
}
});
$("#keyword").blur(function(){
$("#results").fadeOut(500);
})
.focus(function() {
$("#results").show();
});
});
Column 0 = id - INT AI
Column 1 = name - VACHAR
First what i think i need to do but also cant figure out is give the id a variable so i can ask for that in my javascript?
And then i need to add here
$('.item').click(function() { window.location = "../item.php?id=" + $('#keyword').val($id);
Or something like that? sorry for any typo's its kinda late and this is kinda driving me crazy.
Be aware this is one of my first times using any sort of code in this case php etc.. I'd love to know what im doing wrong but keep it polite, since i know this maybe can be done better by a 7 year old. Also there are a few lines that i dont understand yet, even not a bit. But im working on that.