I have a table that looks like:
id | page_name | category
--------------------------
1 | Tornado | Air
2 | Car | Vehicles
Dropdown for example (HTML):
<select id="dropdown_categories">
<option value="Air">Air</option>
<option value="Vehicles">Vehicles</option>
</select>
I want that when i select Air i will see only result 1 (Tornado).
Tried something like that:
controller:
public function Get_Page_By_Category ( $category ) {
$data['pages_cat'] = $this->page_model->get_page_by_category($category);
}
model:
function get_page_by_category ( $category ) {
$this->db->select('*')->from('page')->where('page.category =', $category);
$result = $this->db->get();
return $result;
}
ajax:
<script type="application/javascript">
$(document).ready(function() {
$("#dropdown_categories").change(function(){
alert($('#dropdown_categories option:selected').val());
})
});
</script>
Not sure how to continue, what is the best approach to this?
You should use ajax for this. try like this.
$(document).ready(function() {
$("#dropdown_categories").change(function(){
var val = $('#dropdown_categories option:selected').val();
$.ajax({
url: "path to controller function/value of select box present in var val",
type: "POST",
dataType: "HTML",
async: false,
success: function(data) {
alert(data)
}
});
});
});
in controller you can echo the value, that will come in success of ajax in data variable, i have alerted there, you can use how even you want
You are using jQuery, so you can use the jQuery ajax. It's easy. You can use:
var val = $('#dropdown_categories option:selected').val();
$.ajax({
url: "url",
type: "POST",
dataType: "application/json,"
data: {value: val, callback: php_function_to_call, args: [array_of_arguments_of_php_function]}
success: function(data){
document.title = data.title;
}
})
Now the php:
function myFunction($arguments_if_you_have){
//get the databse result here;
//notice: ajax only returns data, when there is something sent to the output stream, so we must use echo
echo json_encode($result);
}
if(isset($_POST['callback'])){
if(isset($_POST['args'])){
call_user_func_array($_POST['callback'], $_POST['args']);
}
call_user_func_array($_POST['callback'], array());
}
Related
I want to display like count for that post the user likes, every post like href has a specific id, now if I am clicking the post having id 222. Then like count having id 222 will refresh.
like <div id="111">4</div>
like <div id="222">5</div>
like <div id="333">12</div>
My rate function
<script type="text/javascript">
function rate(c)
{
var c
jQuery.ajax({
type: 'POST',
url: 'rate.php',
data: {
cmnt: c
}
});
return false;
}
</script>
the C variable is getting the post id
you can update your rate() function as given below
<script type="text/javascript">
function rate(c)
{
jQuery.ajax({
type: 'POST',
url: 'rate.php',
data: {cmnt: c},
success: function(html) {
var count = $("#"+c).html();
var new_count = parseInt(count)+parseInt(1)
$("#"+c).html(new_count);
}
});
return false;
}
</script>
let me know if that helped you...
I have the following code:
$(document).ready(function() {
$.getJSON('model.php',function(data) {
$('h1').append(data['event']);
$('.img-circle').attr('src',data['image'])
$('a').attr('href',data['url']);
var eventname = data['event'];
$('.starter-template').on("swipeleft",function() {
var events = {'event': eventname};
$.ajax({
type: "POST",
url: "getchoices.php",
dataType: 'json',
data: {event: JSON.stringify(eventname) }
});
})
})
})
It takes a swipe, and sends that data to the database. The change in the database then updates the model.php JSON, which I would like to then read the new values into the document, but I am stuck on how to do this. Originally I repeated the original section like so:
$(document).ready(function() {
$.getJSON('model.php',function(data) {
$('h1').append(data['event']);
$('.img-circle').attr('src',data['image'])
$('a').attr('href',data['url']);
var eventname = data['event'];
$('.starter-template').on("swipeleft",function() {
var events = {'event': eventname};
$.ajax({
type: "POST",
url: "getchoices.php",
dataType: 'json',
data: {event: JSON.stringify(eventname) }
});
$.getJSON('model.php',function(data) {
$('h1').append(data['event']);
$('.img-circle').attr('src',data['image'])
$('a').attr('href',data['url']);
var eventname = data['event'];
})
})
})
})
But this seems to always send the same data['event'] to the PHP, rather than changing each time.
You assign eventname on every swipe like this:
$.getJSON('model.php',function(data) {
$('h1').append(data['event']);
$('.img-circle').attr('src',data['image'])
$('a').attr('href',data['url']);
var eventname = data['event'];
})
The thing is that you do not alter your global eventname variable which is defined
above. Changing your code (2nd $.getJSON) to following should do the job:
$.getJSON('model.php',function(data) {
...
eventname = data['event']; // omit var
})
I am using Twitter Bootstrap Typeahead for an autocomplete field.
End state goal: The user first enters details into field 1. When they enter details in field 2, ajax passes the query as it is written to a PHP file which queries a database based on what was also entered into field 1.
How do I pass both the query from field 2 and the contents of field 1 to the PHP file and access them.
Here is what I have so far:
HTML FILE:
<div class="field1">
<input type="text" id="field1" data-provide="typeahead" name="field1">
</div>
<div class="field2">
<input type="text" id="field2" data-provide="typeahead">
</div>
<script src="js/jquery-1.9.1.min.js"></script>
<script src="js/bootstrap.js"></script>
<script>
$(function() {
$("#field2").typeahead({
source: function(query, process) {
var textVal=$("#field1").val();
$.ajax({
url: 'field2.php',
type: 'POST',
data: 'query=' + query,
dataType: 'JSON',
async: true,
success: function(data) {
process(data);
console.log(textVal);
}
});
}
});
});
</script>
PHP FILE:
if (isset($_POST['query'])) {
$db_server = mysql_connect("localhost", "root", "root");
mysql_select_db("db_test");
$query = $_POST['query'];
$other = '**This needs to be field 1**';
$sql = mysql_query("SELECT * FROM table WHERE row1 LIKE '%{$query}%' AND row2 = '$other'");
$array = array();
while ($row = mysql_fetch_assoc($sql)) {
$array[] = $row['row1'];
}
echo json_encode($array);}
At the moment, the query part works perfectly and the results are returned (the console also displays the value from 'Field1'. Just need to get that value into the php file at the same time!
Any help would be great
If I understood this correctly, you want to parse both the values of field 1 and 2 to the same AJAX call. This is how you do it.
<script>
$(function() {
$("#field2").typeahead({
source: function(query, process) {
var textVal=$("#field1").val();
$.ajax({
url: 'field2.php',
type: 'POST',
data: 'query=' + query + '&field1=' + textVal,
dataType: 'JSON',
async: true,
success: function(data) {
process(data);
console.log(textVal);
}
});
}
});
});
</script>
Now you just make another $_POST['field1'] in your PHP file.
var userQuery = $('#ID of query input element').val();
var field1 = $('#ID of input 1 element').val();
$.ajax({
type: "POST",
url: '',
data: {query: QueryVariable, input1: input1variable},
success: function(data) {
// code within this block
},
error: function() {
alert('System Error! Please try again.');
},
complete: function() {
console.log('completed')
}
}); // ***END $.ajax call
My page consists of a list of records retrieved from a database and when you click on certain span elements it updates the database but at present this only works for the first record to be displayed.
(Basically changes a 0 to 1 and vice versa)
These are my two html elements on the page that are echoed out inside a loop:
Featured:<span class="featured-value">'.$featured.'</span>
Visible:<span class="visible-value">'.$visible.'</span>
Here is what I have:
$(document).ready(function() {
$('.featured-value').click(function() {
var id = $('.id-value').text();
var featured = $('.featured-value').text();
$('.featured-value').fadeOut('slow');
$.ajax({
type: "POST",
url: "process.php",
data: "id="+id+"&featured="+featured,
success: function(data) {
$('.featured-value').html(data);
$('.featured-value').fadeIn('slow');
}
});
return false;
});
// same function for a different span
$('.visible-value').click(function() {
var id = $('.id-value').text();
var visible = $('.visible-value').text();
$('.visible-value').fadeOut('slow');
$.ajax({
type: "POST",
url: "process.php",
data: "id="+id+"&visible="+visible,
success: function(data) {
$('.visible-value').html(data);
$('.visible-value').fadeIn('slow');
}
});
return false;
});
});
It was working fine with one using id attributes but now I'm using class the fadeIn part of the success query isn't working but I'm hoping the .each will fix this.
UPDATE
The full loop is as follows:
while ($event = $db->get_row($events, $type = 'MYSQL_ASSOC'))
{
// open event class
echo '<div class="event">';
echo '<div class="id"><span class="row">Event ID:</span><span class="id-value"> '.$id.'</span></div>';
echo '<div class="featured"><span class="row">Featured: </span><span class="featured-value">'.$featured.'</span></div>';
echo '<div class="visible"><span class="row">Visible: </span><span class="visible-value">'.$visible.'</span></div>';
echo '</div>';
}
Cymen is right about the id selector causing you trouble. Also, I decided to refactor that for you. Might need some tweaks, but doesn't everything?
function postAndFade($node, post_key) {
var id = $node.parents('.id').find('.id-value').text();
var post_val = $node.text();
$node.fadeOut('slow');
$.ajax({
type: "POST",
url: "process.php",
data: "id="+id+"&"+post_key+"="+post_val,
success: function(data) {
$node.html(data);
$node.fadeIn('slow');
}
});
return false;
}
$('.featured-value').click(function() { return postAndFade($(this), 'featured'); });
$('.visible-value').click(function() { return postAndFade($(this), 'visible'); });
The click function is getting the same id and value on each click because you've bound it to the class. Instead, you can take advantage of event.target assuming these values are on the item being clicked. If not, you need to use event.target and navigate to the items within the row.
$('.featured-value').click(function(event) {
var $target = $(event.target);
var id = $target.attr('id');
var featured = $target.text();
$target.fadeOut('slow');
$.ajax({
type: "POST",
url: "process.php",
data: "id="+id+"&featured="+featured,
success: function(data) {
$target.html(data).fadeIn('slow');
}
});
return false;
});
So something like that but it likely won't work as it needs to be customized to your HTML.
I am fetching user record from db-table like
<?php $row=1; ?>
<select id="status<?php echo $row; ?>" name="status<?php echo $row; ?>">
<option value="0">Active</option>
<option value="1">Block</option>
</select>
<?php $row++; ?>
----------------------------------------
Name Status
----------------------------------------
Abc Active
Def Block
Ghi Active
Jkl Block
----------------------------------------
where status is drop-down with two status for each user and If I want to change the status of any user then I select an option from that drop-down and at the same time the status must update in db-table.
For this I coded:
for(var r=1; r<=4; r++){
$("status").each(function() {
var sld = $("#status").val();
alert(sld);
$.ajax({
type: "POST",
url: "response.php",
data: "sld="+sld,
success: function(msg){
alert(msg); // this is the response
}
});
});
}
but for loop does not create script for each drop-down....
You'll need to remove that for loop, and use this code.
My event is now "change" and it will submit two variables, "id" and "sld". You then update your php script to check $_POST['id'] and $_POST['sld'] and then update these into the database.
add class="status" to each of the drop downs.
$(".status").change(function() {
var sld = $(this).val();
alert(sld);
$.ajax({
type: "POST",
url: "response.php",
data: { "sld": sld, "id":$(this).attr('id').replace('status','') },
success: function(msg){
alert(msg); // this is the response
}
});
});
$('status') would select elements that have the node name of status. Do you have that? Or did you mean $('.status')?
I think you are having more than one element with the same ID which will be an invalid HTML.
Put a class for your select boxes and then you can use something like this.
$("select.status").each(function() {
var sld = this.value;
alert(sld);
$.ajax({
type: "POST",
url: "response.php",
data: "sld="+sld,
success: function(msg){
alert(msg); // this is the response
}
});
});
Also I don't find any use of the for loop in the example.