This javascript dropdown list auto complete work on windows local server in PHP aplication, but when i try this funcionality when aplication is on linux server nothing happen. Maybe i should change live method into on? But how?
This is javascript
<script type="text/javascript">
$(function(){
$(".search_keyword").keyup(function()
{
var search_keyword_value = $(this).val();
var dataString = 'search_keyword='+ search_keyword_value;
if(search_keyword_value!='')
{
$.ajax({
type: "POST",
url: "search.php",
data: dataString,
cache: false,
success: function(html)
{
$("#result").html(html).show();
}
});
}
return false;
});
$("#result").on("click",function(e){
var $clicked = $(e.target);
var $name = $clicked.find('.country_name').html();
var decoded = $("<div/>").html($name).text();
$('#search_keyword_id').val(decoded);
});
$(document).o("click", function(e) {
var $clicked = $(e.target);
if (! $clicked.hasClass("search_keyword")){
$("#result").fadeOut();
}
});
$('#search_keyword_id').click(function(){
$("#result").fadeIn();
});
});
</script>
And this is search.php
include('db.php');
if(isset($_POST['search_keyword']))
{
$search_keyword = $polaczenie->real_escape_string($_POST['search_keyword']);
$sqlCountries="SELECT name FROM shop WHERE name LIKE '%$search_keyword%'";
$resCountries=$polaczenie->query($sqlCountries);
if($resCountries === false) {
trigger_error('Error: ' . $polaczenie->error, E_USER_ERROR);
}else{
$rows_returned = $resCountries->num_rows;
}
$bold_search_keyword = '<strong>'.$search_keyword.'</strong>';
if($rows_returned > 0){
while($rowCountries = $resCountries->fetch_assoc())
{
echo '<div class="show" align="left"><span class="country_name">'.str_ireplace($search_keyword,$bold_search_keyword,$rowCountries['nazwa']).'</span></div>';
}
}else{
echo '<div class="show" align="left">Brak</div>';
}
}
Related
I am using Codeigniter and javaScript file upload and insert database but not responce so please check my code abd share valuable idea. share all code heare...view page form data , javascript code responce error and modals submit data code here.....
view
<form id="frm_submit" enctype="multipart/form-data">
<input type="file" name="reportfile" class="form-control">
<button type="submit" class="btn btn-info" name="submit"> Save</button>
</form>
javaScript
$("#frm_submit").on('submit', function (e){
e.preventDefault();
$.ajax({
url: '<?php echo base_url() ?>vendors/upload-report-file',
type: 'POST',
data: $("#frm_submit").serialize()
}).always(function (response){
var r = (response.trim());
if(r !=0){
$(".success").show();
$(".danger").css("display","none");
setInterval('location.reload()',2000);
}else{
$(".danger").show();
$(".success").css("display","none");
}
});
});
models
public function vendors_upload_report_file()
{
$db2 = $this->load->database('bstdc',TRUE);
date_default_timezone_set('Asia/Kolkata');
$vendorsid = $this->session->userdata['vendors_data'][0]['vendor_id'];
$file_request_id = $this->input->post('file_department_id');
$file_request_id = $this->input->post('file_request_id');
$reportfile = $this->input->post('reportfile');
$today_date = date("Y-m-d");
if(!empty($reportfile)){
$filename = $_FILES['reportfile']['name'];
if (!empty($filename)){
$pic = $a.'-'.rand(5,10).time()."".$filename;
move_uploaded_file($_FILES['reportfile']['tmp_name'],'upload/files/'.$pic);
$report_pic_url = ''.base_url().'upload/files/'.$pic.'';
$db2->query('INSERT INTO bstdc_reports_file (request_id,sendor_type,sendor_id,reports_file,reports_file_url,created_date,act_status,del_status)
VALUES ("'.$file_request_id.'","Vendors","'.$vendorsid.'","'.$pic.'","'.$report_pic_url.'", "'.$today_date.'", "Y","N")');
return true;
}else{
return false;
}
}
}
Use FormData object to upload file
$("#frm_submit").on('submit', function (e) {
e.preventDefault();
$.ajax({
url: '<?php echo base_url() ?>vendors/upload-report-file',
type: 'POST',
data: new FormData(this), // <== changed from $("#frm_submit").serialize()
}).always(function (response) {
var r = (response.trim());
if (r != 0) {
$(".success").show();
$(".danger").css("display", "none");
setInterval('location.reload()', 2000);
}
else {
$(".danger").show();
$(".success").css("display", "none");
}
});
});
I would like to send my variable "var targetId" to my php file.
I try to make ajax request but nothing happens.
My js file :
$( ".project_item" ).click(function(e){
var targ = e.target;
var targetId = targ.dataset.id;
console.log(targetId);
$('.popUp').fadeIn("200");
$('header, main, footer').addClass('blur');
$.ajax({
url: 'function.php',
type: "POST",
data: {idVoulu: targetId},
success: function(data){
alert(data);
console.log(data);
}
});
});`
And my php file to get the data
$idProject = (isset($_POST['idVoulu'])) ? $_POST['idVoulu'] : 0;
if($idProject==0) { echo ' ID not found';}
Can you tell me what's going wrong?
Well I can't Find a problem on your code but you can try this may be this will help you
var mydata = "idVoulu='+targetId+'"; // make a string
$.ajax({
url: 'function.php',
type: "POST",
data: mydata,
success: function(data){
alert(data);
console.log(data);
}
});
So, i made this and it works:
My js:
$( ".project_item" ).click(function(e) {
var targ = e.target;
var targetId = targ.dataset.id;
$('.popUp').fadeIn("200");
$('header, main, footer').addClass('blur');
$.post('../function.php', { id: targetId }, function(response) {
console.log("reponse : ", response)
});
My php :
if (isset($_POST['id'])) {
$newID = $_POST['id'];
$response = 'Format a response here' . $newID;
return print_r($response);
}
i return from php page an echo for test:
$('#form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'https://capelladb.000webhostapp.com/textExample.php',
data: $('#form').serialize(),
success: function (data) {
var compare = data.localeCompare('testest');
console.log(data);
console.log(compare);
}
});
});
});
Just for test, the data returns in the success method is typeof "string" with "testest", but when im comapre to 'testest', is return -1 into "compare" variable. why it happen ?
Console.log photo: http://imgur.com/a/h7wWh
the PHP:
<?php
header("Access-Control-Allow-Origin: *");
$servername = "localhost";
$username = "id1635462_capellam";
$password = "capella2017";
$dbname = "id1635462_capella";
$userInput = $_POST['username'];
$passwordInput = $_POST['password'];
echo $userInput . $passwordInput;
?>
Your data has empty spaces within.
You need to trim it, using .trim().
See the code below:
$('#form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'https://capelladb.000webhostapp.com/textExample.php',
data: $('#form').serialize(),
success: function (data) {
var compare = data.trim().localeCompare('testest');
console.log(data);
console.log(compare);
}
});
});
});
Working example:
window.onload = function() {
var data = 'testest ';
var test = data.localeCompare('testest');
console.log('Not trimmed: ' + test);
var test = data.trim().localeCompare('testest');
console.log('Trimmed: ' + test);
};
My code seems to not run the jQuery at all for some reason, I have spent lots of time attempting to figure out what's wrong and have tested my delete PHP file separately
jQuery script:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
$.ajaxSetup({ cache: false });
$(document).ready(function(){
$('#button').on('click', '#btn',function()) {
var clickBtnName = $(this).attr('name');
var ajaxurl = 'http://127.0.0.1/SQLDeleteHandler.php';
var data = {'id': clickBtnName};
$.post(ajaxurl, data, function(response) {
window.location.href="http://localhost/store.php";
});
});
});
</script>
Php:
$query = "SELECT * FROM accounts";
$resultset= mysqli_query($connection,$query);
while($row = mysqli_fetch_array($resultset,MYSQLI_NUM)){
echo $row[0]." ".$row[1]." ".$row[2]." ".'<input type="submit" class="btn" name="".$row[0]."" value="delete" />';
echo "</br>";
}
use this :
$(document).ready(function(){
$('.btn').click(function(e){
e.preventDefault();
alert('Button is pressed');
var clickBtnName = $(this).attr('name');
var ajaxurl = 'http://127.0.0.1/SQLDeleteHandler.php';
var data = {'id': clickBtnName};
$.post(ajaxurl, data, function(response) {
window.location.href="http://localhost/store.php";
});
});
});
I've got a lil question.. I've been working on a JQuery auto-completion system, but I can't get to manage the box that comes up while writing to fade away.
HTML Code:
<div class="content">
<input type="text" class="search" id="searchid" placeholder="Search for items being sold.." />
<div id="result"></div>
</div>
JQuery Code:
$(function(){
$(".search").keyup(function()
{
var searchid = $(this).val();
var dataString = 'search='+ searchid;
if(searchid!='')
{
$.ajax({
type: "POST",
url: "search.php",
data: dataString,
cache: false,
success: function(html)
{
$("#result").html(html).show();
}
});
}return false;
});
jQuery("#result").live("click",function(e){
var $clicked = $(e.target);
var $name = $clicked.find('.name').html();
var decoded = $("<div/>").html($name).text();
$('#searchid').val(decoded);
});
jQuery(document).live("click", function(e) {
var $clicked = $(e.target);
if (! $clicked.hasClass("search")){
jQuery("#result").fadeOut();
}
});
$('#searchid').click(function(){
jQuery("#result").fadeIn();
});
});
PHP Code:
<?php
require_once('includes/db_connect.php');
if($_POST) {
$q = $_POST['search'];
$sql_res = mysqli_query($conn, "SELECT * FROM entries WHERE item like '%$q%'");
while($row = mysqli_fetch_array($sql_res)) {
$item = $row['item'];
?>
<div class="show" align="left">
<span class="name"><?=$item;?></span>
</div>
<?php
}
}
?>
Print-screen when typed in: http://prntscr.com/7cqbgc
Print-screen when backspaced the input: http://prntscr.com/7cqbsa
I hope someone can helps me to make the item's retrieved from the database disappear.
Also, if someone has a clue. How would I make the text in the items bold if it matches to my text input?
Thank you.
Just try like this:-
if(searchid!='')
{
$.ajax({
type: "POST",
url: "search.php",
data: dataString,
cache: false,
success: function(html)
{
if(html !=''){
$("#result").html(html).show();
}else{
$("#result").hide();
}
}
});
}else{
$("#result").hide();return false;
}