component.php
<script>
jq142(document).ready( function() {
new Paginator('<?php echo HOST_URL; ?>component_ajax.php');
});
</script>
I need to pass the id from component.php file to component_ajax.php file. component_ajax.php will load on the component.php file.
component_ajax.php
$coid = $_GET['id'];
$products = $productObj->getFrontProductsbyComponents( $coid );
Explain me to that how to pass id to $coid?
You need to append the id to the component_ajax.php URL.
new Paginator('<?php echo HOST_URL; ?>component_ajax.php?id=<?php echo $your_id; ?>');
If the ID you need to pass to component_ajax.php is in JavaScript instead of PHP, append the ID to the URL with javascript.
<script>
var your_id = 123;
jq142(document).ready( function() {
new Paginator('<?php echo HOST_URL; ?>component_ajax.php?id=' + your_id);
});
</script>
Pass the ID in to your Paginator object as an argument.
var ajaxParams = {"id":<?php echo $your_id ?>};
new Paginator('<?php echo HOST_URL; ?>component_ajax.php', {"xParams": ajaxParams});
Source
Related
I need when I click anchor tag I need load controller function and get data from calling function in the model and send it to view_content page and display data
here is my code
view
<div id="loading"></div>
Click Here
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".company").click(function(e){
e.preventDefault();
var id = $(this).attr('id');
var site_url = "<?php echo site_url('site2/home2/'); ?>" +id; //append id at end
$("#loading").load(site_url);
//alert(id);
//alert("aawa");
});
});
</script>
controller
public function home2($id){
$this->load->model('get_click_data');
$data['company_data'] = $this->get_click_data->get_company($id);
$this->load->view('view_content',$data);
}
model
<?php
class Get_click_data extends CI_Model{
public function get_company($id){
$query = $this->db->query("SELECT * from companydetails where id = '$id'");
return $query->result();
}
view_content
<div id="content">
<h3>Welcome to Home Page 12345</h3>
<?php
print_r($company_data);
?>
as you are using codeigniter, try changing to:
...
var id = $(this).attr('id'); //you need to have 'id' attribute in your anchor
$("#loading").load("<?php echo site_url('site2/home2/); ?>" + id, function() {
alert("aawa");
});
...
and in your controller file,
function some_function($your_id) {
//get data
//do something with $your_id
//pass view as string
echo $this->load->view('view_content', $data, TRUE);
}
Update::
to put js variable in your site url, do:
var id = $(this).attr('id');
var site_url = "<?php echo site_url('site2/home2/'); ?>" + id; //append id at end
$("#loading").load(site_url, function() {
alert("aawa");
});
one more update::
if you want to pass multiple parameters to your function from js, you can do:
var id = $(this).attr('id');
var second_param = "some value here";
var site_url = "<?php echo site_url('site2/home2/'); ?>" + id + "/" + second_param;
$("#loading").load(site_url, function() {
alert("aawa");
});
and in your controller function
function some_function($your_id, $other_param) {
do something with $your_id and $other_param
//pass view as string
echo $this->load->view('view_content', $data, TRUE);
}
addition::
in your home2() function your are trying to load view, change
$this->load->view('view_content',$data);
to
echo $this->load->view('view_content',$data, TRUE);
so that its returned as string to your .load() jquery function
its working
<script type="text/javascript">
$(document).ready(function(){
$(".company").click(function(e){
e.preventDefault();
var id = $(this).attr('id');
$("#loading").load('<?php echo site_url('site2/home2/'); ?>'+id);
alert(id);
//alert("aawa");
});
});
I want to get value of $security_code which is in captchaCode.php file, in captcha.php file through javascript.I can not include this file in captcha.php file . Here is the code of Captcha_code.php and this code not comes inside any function of this file:
$captcha = new CaptchaCode();
$security_code = str_encrypt($captcha->generateCode(6));
And here is my javascript function, through this function i want to get the value of $security:
function refresh_captcha()
{
var img = document.getElementById('captcha_img');
img.src = '<?php echo "/captcha_images.php"?>';
jQuery("#captcha_img").attr("src",img.src);
jQuery("#security_code").val("<?php echo $security_code;?>");
}
Actually this code is for getting new encrypted value when captcha is refreshed without refreshing the page.
You have to change the image source assignment related code. No need to assign PHP tag. Just assign the PHP file name instead like this.
function refresh_captcha()
{
var img = document.getElementById('captcha_img');
img.src = '/captcha_images.php';
jQuery("#captcha_img").attr("src",img.src);
/*This line will not work without Ajax request*/
/*jQuery("#security_code").val("<?php echo $security_code;?>");*/
}
Send an ajax request to a page to output only the code like:
File: outputcode.php (demo only)
$captcha = new CaptchaCode();
$security_code = str_encrypt($captcha->generateCode(6));
echo $security_code;
exit;
Next, grab that value using AJAX, like for example in jQuery
$("#refresh_code").on('click', function() {
$.get("outputcode.php", function(data) {
//data will now hold the new code
});
});
Hope this gives you an idea.
If you cannot include the files, you need to use ajax request.
captcha.php
$captcha = new CaptchaCode();
$security_code = str_encrypt($captcha->generateCode(6));
echo json_encode($security_code);
in your js:
<img src="" id="captcha_img" onclick="refresh_captcha()"/>
<script>
function refresh_captcha()
{
$.ajax({
url : 'captcha.php',
type : 'POST',
dataType : 'json',
success : function (security_code) {
var source = security_code;
var img = document.getElementById('captcha_img');
img.src = source;
jQuery("#captcha_img").attr("src",img.src);
jQuery("#security_code").val(source);
}
});
}
</script>
The onclick event on the img could be totally wrong, but for the excercise purpose, I've put it there.
Depends on what $security_code is, you can for example, not use JSON.
In your php file captcha.php,
$captcha = new CaptchaCode();
$security_code = str_encrypt($captcha->generateCode(6));?>
<script>
var code = '<?= $security_code ?>';
</script><?php // rest of the php codes
In your js file
function refresh_captcha()
{
var img = document.getElementById('captcha_img');
img.src = code;
jQuery("#captcha_img").attr("src",img.src);
}
I was wondering if it's possible to use data from the view in my javascript code? This is what I have:
View "index.phtml":
<?php $event = $this->event; ?>
<script>
// HERE I WANT TO USE $event
var data = <?=$event?>;
</script>
But that doesn't work!
If it contain a string value:
<script>
var data = '<?php echo $event; ?>';
</script>
If it contain a numeric value:
<script>
var data = <?php echo $event; ?>;
</script>
Also php short tag <?= should be On in php.ini to use it.
I want to make a link to call for each row.
Here is the code:
foreach($docs as $row) {
echo "<td><h5><a href='' id='onclick' class='onclickcalldocEdit_".$row->dId."'>".$row->dName."</a></h5></td>";
echo "<div id='response_proj' class='container_proj_".$row->dId."'>
/* container code here */
}
So, basically I create a container for each row and name differs only by id.
I've used this script to try to get it work:
$(document).ready(function(){
var className = $('#onclick').attr('class');
var contName = $('#response_proj').attr('class');
$(className).jqm({trigger:contName, toTop: true});
});
</script> ";
Is it even possible?
Try this:
$(document).ready(function(){
$("#onclick").each(function(){
var className = this.attr('class');
var contName = this.closest('td').next('#response_proj').attr('class');
$(className).jqm({trigger:contName, toTop: true })
})
})
Frankly speaking, I think the better way is to use data-id and class attributes(without id):
<a href='' class='onclick' data-id='".$row->dId."'>".$row->dName."</a>
And access id via
var id = $('.onclick').data('id');
I am trying to delete records from database without reloading the page. In the view file I have the following code. But when I click on the delete link nothing happens. Would you please kindly help me find out where I have done wrong?
Thanks in advance.
My view file:
<script type="text/javascript">
$(function(){ // added
$('a.delete').click(function(){
$.ajax({
var a_href = $('selector').attr('href');
type: "POST",
url: "<?php echo base_url(); ?>student_fee_status/payment_info_delete",
data: "id="+a_href,
success: function(html){
$("#show").html(html);
}
});
return false
});
}); // added
</script>
<?php if(count($records) > 0) { $i = 0; foreach ($records as $row){ $i++; ?>
<span> <?php echo $row['fee_type']; ?> : <?php echo $row['fee_amount']; ?> [<a id='<?php echo "$paymentid" ;?>'
class='delete' href='#'>Delete</a>]</span> <br>
<?php }} ?>
This is my Controller:
function payment_info_delete(){
$id = mysql_real_escape_string($_POST['id']);//Some clean up :)
$query= $this->db->delete('studentpayment2', array('pid' => $id));
echo "$id";
}
You need to put the 'var a_href...' line outside of the ajax function
You need to replace the word selector with this which refers to the currently clicked element.
var a_href = $(this).attr('href');
$.ajax({...
var a_href = $('selector').attr('href');
$.ajax({...
As mgraph pointed out in his answer, the var a_href = $('selector').attr('href'); line is incorrect. You want to replace 'selector' with this so that you're getting the href attribute from the anchor that was clicked on.
That said, I think that's still probably wrong - surely you want to be passing back the id of the entry to delete, which seems to be stored in the id attribute of the anchor tag, rather than always passing back a # symbol?
To pass the id back instead, rather than the href, replace this:
var a_href = $('selector').attr('href');
with this:
var a_href = this.id;