I am trying to return an image when Ajax is done, but i don't know how to include a session variable in the response, this is my code:
$(document).ready(function(){
$("#process").click(function(){
$.ajax({
type: 'POST',
url: 'process.php',
success: function (msg) {
$('#new-image').html('<img src="uploads/img/$_SESSION["sess_img"];" class="img-upload" alt="new image">')
}
});
});
});
Then, I want show the image $_SESSION["sess_img"]; within the div #new-image
You can echo that image filename in process.php like this:
<?php
// using time() as the cache buster, the image will never be cache by the browser
// to solve it, you need to add condition that will check if the image has change or not.
// or maybe you need to change the filename if it changes without adding a cache buster.
echo $_SESSION['sess_img_chofer']."?v=".time();
And in your javascript code:
$(document).ready(function(){
$("#process").click(function(){
$.ajax({
type: 'POST',
url: 'process.php',
success: function (image) {
$('#new-image').html($('<img>').attr('src', 'uploads/img/' + image).attr('class', 'img-upload'));
}
});
});
});
Related
hi i am working on a codeigniter project. I want to execute a php function after an interval of 10seconds. When a user visits that specific page after 10 seconds i want that php function to be executed. In that php function i have set a counter which adds 1 to the specific table into the database. I have tried using AJAX but didnt get the desired result. Kindly explain me with examples as i am new in ajax. Thanks in advance...
#Majid Golshadi s answer is the right answer.
Working version is here
Please in view that is loaded all the time (like header_view.php)
add this few lines
<script type="text/javascript">
var _baseUrl = "<?= base_url() ?>";
</script>
This makes that your base_url is usable in JavaScript anywhere in page (but make sure to have it somewhere on "TOP" of page)
and literraly use #Majid Golshadi s answer in this way
$(document).ready(function() {
setTimeout(function() {
$.ajax({
url: _baseUrl + "/your/controller/param",
type: 'post',
data: {"token": "your_token"}, });
}, 10000);
});
using jquery this will be the easiest and fastest way to do that
`//setting timeout to 3 seconds = 3 thousand milli seconds
setInterval(function(){
//ajax call
$.ajax({
url: _baseUrl + "/controller_name/function_name/", //the url where you want to fetch the data
type: 'post', //type of request POST or GET
data: {"data": "value"}, }); //data passed to controller
},3000);`
in your controller you may use
function function_name(){
$var = $this->input->post();//getting data passed from ajax
//process here...
echo json_encode($var)//parses and returns the processed value
}
try this
setTimeout(function(){
$.ajax({
url: "<?php echo base_url('your/controller/address');?>",
type: 'post',
data: {"token": "your_token"},
});
}, 10000);
Example
in your view
$(document).ready(function(){
setTimeout(function(){
$.ajax({
url: "<?php echo base_url('MyController/Mymethod');?>",
type: 'post',
data: {"token": "12majid18"},
});
}, 10000);
});
and in Your Controller write method like this
public function Mymethod()
{
$token = $this->input->post('token');
if ( $token == '12majid18' )
{
/*call your model and insert your data in Table*/
}
}
you can try this:
window.jQuery(function(){
var y=setInterval(function() {
window.jQuery.post(url,{"token": "your_token"},function(res){
alert(res);
});
}, 10000);
});
I have modified the code
to POST prodID to ProductsList.php
// its a dynamically generated drop menu
while($rowmnu2=mysql_fetch_assoc($resulmnusub2))
{
echo '<li><a id="'.$rowmnu2['liid'].'" href="#" onclick="passto(this.id)">'.$rowmnu2['title'].'</a></li>
';
}
and here is my ajax function :
function passto(val){
//window.location.href="ProductsList.php?idd=" + val;
$.ajax({
url: 'ProductsList.php',
type: "POST",
data: ({prodID: val}),
success: function(data){
//or if the data is JSON
window.location.href="ProductsList.php";
}
});
}
the passed element to the function is an integer
in the ProductsList.php I have
<?php
if(!$_POST['prodID']) die("There is no such product!");
echo $_POST['prodID'];
?>
and I get There is no such product! while there should be an INT #
why is that ?
any one knows? all the bellow suggestions are not responding correctly
$(document).ready(function() {
$("a").click(function(event) {
myid = $(this).attr('id');
$.ajax({
type: "POST",
url: "ProductsList.php",
data: {prodID: myid},
dataType: "json",
complete:function(){
window.location("ProductsList.php");
}
});
});
});
if you want to POST id , you can change:
...onclick="passto(this)"...
to
...onclick="passto(this.id)"...
That behavior is normal because you are requesting ProductsList.php twice. the first time with an AJAX request using $.ajax. for that time the id is sent correctly. The problem is that you request ProductsList.php again just after AJAX complete using window.location.href="ProductsList.php"; without sending anything. So the result is as expected, a page printing There is no such product!
You can fix the problem by replacing window.location.href="ProductsList.php"; by this one :
$('body').html(data);
or any other instruction to use properly the returned data.
You can either use my edited code or just edit yours :
echo '<li ><a id="'.$rowmnu2['link'].'" href="#">'.$rowmnu2['title'].'</a></li>';
JS part :
$('a').click(function() {
var val = $( this ).attr('id');
$.ajax({
type: "POST",
url: "ProductsList.php",
data: {prodID:val},
complete:function(){
$('body').html(data);
}
});
});
I have two files. One PHP-file, which contains a SQL-Select statement and returns the output as html. The second file is my index file which contains a div with the class "loadMembers" and some jquery code:
$(document).ready(function () {
function startInterval() {
var refreshId = setInterval(function () {
$.ajax({
url: 'sidebars/members.php',
type: 'html',
success: function (resp) {
$("div.loadMembers").html(resp);
}
});
}, 5000);
}
startInterval();
});
I want to refresh the div with database data in a 5 seconds interval. I tried it also with .load()...
The request contains some data but nothing from my database...
Where's the problem?
Thanks for any help :)
your request type can be get or post, not html, it should be dataType
url : 'sidebars/members.php',
type: 'POST', // or GET
dataType : 'html',
I installed OpenCart CMS and wanted to modify some option in the admin panel mainly the look and feel of the template. I also added the jpicker script for color picking. Also i created the css file with the standard php code and it works fine
<?php header("Content-type: text/css; charset: UTF-8"); ?>
<?php
$headerColor;
$menuColor = '#121212';
$bodyColor = '#fffaaa';
?>
#header {
background-color: <?=$headerColor; ?>;
}
In order to create the color picker view you need to add the id of the div in a script
$(document).ready(function(){
$('#Expandable').jPicker(
{
window:
{
expandable: true
}
});
});
And here is the standard code for the html
<div id="Expandable"></div>
I am not very good at JQuery but i know that it can be done either with .ajax or post. What i want to do is send the hex value from the jpicker to a php variable in the css file so it can change the color. Here is the link to jpicker http://www.digitalmagicpro.com/jPicker/. I also found an example
var colorValue = '#ababab';
$.post("view/stylesheet/stylesheet-template.css", {var_value: colorValue}, function(data) {
alert(colorValue);
});
It only sends alert and the colorValue from the variable, but i still don't know how to send the colorValue to php from the jpicker.
Thanks
function sendToPhp(jCell){
$.ajax({
url: "php/ajax.php",
data: {cell: jCell, queryNr: "0"},
type: "POST",
async: false,
success: function(data){ callback(data); }
});
}
now cell and queryNr is sent to php
if u do a switch case function depending on query nr you can access multiple functions in php this way
for the comment:
function sendToPhp(jCell, phpUrl){
$.ajax({
url: phpUrl,
data: {cell: jCell, queryNr: "0"},
type: "POST",
async: false,
success: function(data){ callback(data); }
});
}
now you can hand over a specific path by calling the function like:
sendToPhp("VAR YOU WANT TO SEND TO PHP", "php/ajax.php"); // or any other path
this is an ajax method that inserts the data into a db and should supposedly display the new content.
<script type = "text/javascript">
$(document).ready(function() {
$('#submit').live('click', function(eve) {
eve.preventDefault() ;
var form_data = {
title: $('#title').val()
};
$.ajax({
url: "http://localhost/ci/index.php/chat/comment",
type: 'POST',
data: form_data,
success: function(msg) {
alert(msg);
}
});
});
});
</script>
However in my /chat/comment, i am loading the view again, i.e, user submits a comment, load the view again and the comment should be there. My response from server is the view's HTML. However the view comes with all the divs and there are many of them. I need to retrieve only part of the div, say, #commentspace from the ajax on success.
Look at the jQuery $.load() function?
Example
Inside "firstpage.html"
$('#content').load('secondpage.html #content');