I have no idea why the below is not working. I have tested mysql on an alternative project, and it works.
Maybe you guys can see what I am not: The User clicks the submit button:
HTML:
<form class='find_form'>
<input class='find_value'/>
<input class="find_submit" type="submit"/>
</form>
JS:
$(".find_submit").click(function() {;
var value = $('.find_value').val();
if (value == false) { return false; } else {
var request = $.ajax({
type: "POST",
url: "/forum/index.php/ajax/find_active",
dataType: 'json',
data: {value: value}
});
request.done(function (response){
console.log(response);
//update history
//update threads
//update activecontent['thread_name']
active(response['active']);
});
return false;
}
});
AJAX/find_active:
function find_active()
{
$active = $this->input->post('value');
$active_list = $this->search_model->find_active($active);
//...//
}
search_model:
public function find_active($active)
{
$sql = "SELECT *
FROM keywords
WHERE keywords.keyword
LIKE '%$active%'
ORDER BY count DESC";
$query = $this->db->query($sql);
return $query->result_array();
}
After hitting submit, it runs ajax/find_active and in firebug, the wheel keeps turning until I get a memory error.
I have reverted back to a working revision, and added ONLY the query. So I am 99% sure its whats causing the problem.
Any ideas?
Related
I am building now a Queuing system for my helpdesk system. i have problem in detecting the changes of input value. I want to play the play_sound() function sound when the value of input is incremented. the curent value of input is coming from the rowCount in my SQL Query stored in variable.
screenshot picture link
Input
<input disabled type="text" id="needapproval" id="approval" value="0" class="center" />
My Script
<script type="text/javascript">
function play_sound() {
var audioElement = document.createElement('audio');
audioElement.setAttribute('src', 'Kalimba.mp3');
audioElement.setAttribute('autoplay', 'autoplay');
audioElement.load();
audioElement.play();
}
activateMagic();
function activateMagic() {
setInterval(realTimeData, 1000);
function realTimeData() {
$.ajax({
url: './includes/needapproval.php',
method: 'GET',
dataType: "json",
success: function(res) {
$("#needapproval").val(res.data_count);
},
error: function(err) {
console.log(err);
}
});
}
}
</script>
PHP
require_once "connection.php";
class NeedApprovalStatus extends Connection{
public function needApproval() {
$count_approval = "SELECT * FROM job_request WHERE approval_status LIKE '%Need Approval%' ";
$stmt_count_approval = $this->db->prepare($count_approval);
$stmt_count_approval->execute();
$count = $stmt_count_approval->rowCount();
$data_count = [];
if ($count == 0) {
$data_count = [
'data_count' => 0
];
} else {
$data_count = [
'data_count' => $count
];
}
echo json_encode($data_count);
}
}
$need_approval = new NeedApprovalStatus;
$need_approval->needApproval();
I tried to use onchange event in jquery but it doesn't work. because i think onchange only trigger when you change value on input manually. Any ideas guys?
It would be easier to check the value inside the success function and call play_sound() from there.
function activateMagic() {
var value = 0;
setInterval(realTimeData, 1000);
function realTimeData() {
$.ajax({
url: './includes/needapproval.php',
method: 'GET',
dataType: "json",
success: function(res) {
var newValue = res.data_count;
if(newValue != value) {
play_sound()
$("#needapproval").val(value);
value = newValue;
}
}
...
I am trying to update an existing message. A message has a message_id, category, subject and the message. Upon clicking on edit, an AJAX call is made ...
Here's my AJAX call from .js file (VIEW)
if(checkEditMessage()){ // checkEditMessage is to check for validation
console.log("EDIT");
var cat_id = $("#cat_id").val();
var commentsubject = $("#commentsubject").val();
var chat_post_message = $("#chat_post_message").val();
$.ajax({
url: base_url + "chat/editMessage",
async: false,
type: "POST",
data: {
"v_id" : id,
"v_cat_id" : cat_id,
"v_commentsubject" : commentsubject,
"v_chat_post_message" : chat_post_message
},
error: function(err){
console.log(err);
},
success: function (result) {
console.log(result);
}
});
}
Here's my chat.php (CONTROLLER)
function editMessage(){
$posted_data = $this->input->post();
if(isset($posted_data) && !empty($posted_data))
{
$id = $posted_data['v_id'];
$cat_id = $posted_data['v_cat_id'];
$commentsubject = $posted_data['v_commentsubject'];
$chat_post_message = $posted_data['v_chat_post_message'];
$data = $this->chat->updateMessage($id, $cat_id, $commentsubject, $chat_post_message);
echo $data;
}
}
Here's my (MODEL)
function updateMessage($id, $cat_id, $commentsubject, $chat_post_message){
$data=array('cat_id'=>$cat_id,'subject'=>$commentsubject,'message'=>$chat_post_message);
$this->db->where('message_id',$id);
$this->db->update('chat_message',$data);
$err = $this->db->_error_message();
if(empty($err))
{
return "EDIT COMPLETE";
}
return false;
}
Your question is not totally clear, because I don't see any insert, and in order to insert it's really needed the insert clause in your query. I can only tell you some improvements you may want to do.
Which version are you using of codeigniter? Because I don't see you using the word _model which is necessarily.
$message['v_id'] = $posted_data['v_id'];
$message['v_cat_id'] = $posted_data['v_cat_id'];
$message['v_commentsubject'] = $posted_data['v_commentsubject'];
$message['v_chat_post_message'] = $posted_data['v_chat_post_message'];
// `chat_model` and not `chat`
$result = $this->chat_model->updateMessage($message['v_id'], $message);
On the other hand, all you need to do in your model function is
public function updateMessage($id, $data)
{
$this->security->xss_clean($data);
$this->db->where('message_id', $id)
->update('chat_message', $data);
return empty($this->db->_error_message());
}
In you ajax change this
url: "<?php echo base_url() ?>chat/editMessage",
async: false,// remove this
data: {
"v_id : id,
v_cat_id : cat_id,
v_commentsubject : commentsubject,
v_chat_post_message"} ,
and your model and controller looks ok
You can try the following:
Make sure your message_id is getting posted properly. i.e. echo $posted_data['v_id']; in editMessage() to see if the correct value is being received.
Make sure the id you are trying to update exists, and that message_id in your database is the PRIMARY KEY field.
I am new to jquery and am having trouble with the autocomplete function. edit:I should mention I am using MVC with Codeigniter. My AJAX response is returning like this [{"customer_name":"Adecco Management & Consulting S.A."}]. It is also not all in a row it is each character in the dropdown like this
[
{
"
c
u
s
t
and so on. Here is my autocomplete script.
$('#cust_name').autocomplete({
source: function(request,response){
var request = {
toSearch: $('#cust_name').val()
};
$.ajax({
url: '/researchDB/index.php/rdb_con/autoComplete',
data: request,
datatype:"json",
type: 'POST',
success: function(data){
response(data);
}
});
}
});
and my controller:
function autoComplete(){
$data['id'] = $this->rdb_mod->autoComplete();
echo json_encode($data['id']);
}
model:
public function autoComplete(){
$toSearch = $_POST['toSearch'];
$this->db->select('customer_name');
$this->db->like('customer_name', $toSearch, 'after');
$query = $this->db->get('research');
return $query->result();
}
input in view:
<input data-input-type="cust_name" id="cust_name" class="ids form-control search-query " type="text" name="customer_name">
I am not sure I set up the jquery function correctly but the response includes the desired results, in the wrong format, when I type in the input. Thanks for any help you can give!
I received the answer outside of SO and want to post the solution here for others.
controller: I needed to put the results into an array and pass that to the ajax response as one object.
function autoComplete(){
$data['id'] = $this->rdb_mod->autoComplete();
$results = array();
foreach($data['id'] as $row){
$results[]=$row->customer_name;
}
echo json_encode($results);
}
jquery: As far as I understand this section, I wasn't making use of the built in functions and therefore overwriting the request variable that autocomplete sets up.
$('#cust_name').autocomplete({
source: function(request,response){
$.ajax({
url: '/researchDB/index.php/rdb_con/autoComplete',
data: request,
datatype:"json",
type: 'POST',
success: function(data){
var items = JSON.parse(data);
response(items);
}
});
}
});
model: didn't change much. I added distinct to limit dup values.
public function autoComplete(){
$toSearch = $_POST['term'];
$this->db->distinct();
$this->db->select('customer_name');
$this->db->like('customer_name', $toSearch, 'after');
$query = $this->db->get('research');
return $query->result();
}
Thanks to all those who helped me with this!
I'm assuming I have to put something in the success option. However what I have isn't working.
I declare this JS function on the page:
<script type="text/javascript">
function performAjaxSubmission() {
$.ajax({
url: 'addvotetotable.php',
method: 'POST',
data: {
},
success: function() {
}
});
}
</script>
Then in the ajax call which is working properly I declare this for the success:
success: function(data) {
performAjaxSubmission();
},
addvotetotable.php looks like:
<
?php
// If user submitted vote give the user points and increment points counter
require_once("models/config.php");
//Check to see if there is a logged in user
if(isUserLoggedIn()) {
$username_loggedin = $loggedInUser->display_username;
}
if (strlen($username_loggedin)) {
include_once "scripts/connect_to_mysql.php";
$query = mysql_query("SELECT vote FROM points_settings WHERE id=1")or die (mysql_error());
while($info = mysql_fetch_array($query)){
$points_value=$info['vote'];
}
include_once "scripts/connect_to_mysql.php";
$query = mysql_query("INSERT INTO points (id,username,action,points) VALUES ('','$username_loggedin','vote','$points_value')")or die (mysql_error
());
include_once "scripts/connect_to_mysql.php";
$query = mysql_query("UPDATE userCake_Users SET points=points + $points_value WHERE Username='$username_loggedin'")or die (mysql_error());
}
?>
You have to provide a value that you want to store in your database in the "data" of the ajax-request. Than you can use this in your php-code using $_POST["something"], check if it's valid... and return f.e. html or json which you than handle in the success-function.
<script type="text/javascript">
function onSubmitVote()
{
$.ajax({
url: 'addvotetotable.php',
method: 'POST',
data: "theVoteValue=" + <read_your_vote_value_here>,
success: function(result) {
>>>return something in your addvotetotable.php which indicate success and show a message whether the vote was successful or not.<<<
}
});
return false; // prevent submit if it is a submit-type button.
}
</script>
What this does is post data of the vote (it's value) to the php-file. There you can read it with $_POST["theVoteValue"] and put it in the database. You check if the value is valid and may insert, else you return an error-message or something so that in javascript you can notify the user of it's failure.
I'm trying to delete a mysql record with javascript but I fail.
So this is my js function
function delpost(id){
if(confirm('Are you sure?')){
$('#comment_'+id).hide();
http.open("get","/index.php?p=delcomment&id=" + id, true);
http.send();
}
}
And this is my delcomment.php(included through index.php)
$comment_id = $_GET['id'];
if(logged() && $status=="administrator"){
$delquery = mysql_query("DELETE FROM comments WHERE id='$comment_id' LIMIT 1");
die();
}else{
die();
}
I hope you can help me with this :)
update:
try using
http.send(null)
instead of
http.send()
also, use firebug to see if your ajax request is actually being sent to the server
better solution:
(php rusty!)
delcomment.php
$comment_id = $_POST['id'];
$comment_id = mysql_real_escape_string($comment_id);
if(logged() && $status=="administrator"){
$query = "DELETE FROM comments WHERE id='{$comment_id}'";
$result = mysql_query($query, $con);
die();
}else{
die();
}
using jquery to post (make sure to include the jquery.js), your javascript function should be like this:
function delpost(id){
if(confirm('Are you sure?')){
$.ajax({
type: "POST",
url: "/index.php",
data: {p: "delcomment", id: id},
success: function(){
$('#comment_'+id).hide();
},
error: function(){
alert('failure');
}
});
}
}