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");
});
});
Related
I am trying to pass a string from a query into a javascript function.
An integer will pass into the function but string will not.
echo "<a href='#' onclick='delete_game({$title});'
class='btn'>Delete</a>";
<script type='text/javascript'>
function delete_game(title){
var answer = confirm('Really?');
if(answer){
window.location = 'delete.php?id=' + title;
}
}
</script>
I expected the javascript function to be executed, but instead nothing happens.
Why don't you use ajax for this? As mentioned in comments mix PHP/JS isn't good.
In your HTML, you can do something like
I'm assuming that you are using Blade.
Delete Game
Then in your javascript, you do this using jQuery:
function deleteGame(title){
var answer = confirm('Really?');
if(answer){
$.ajax({
url : "your-php-file.php",
type : 'post',
data : {
title : title
}
})
.done(function(msg){
$("#result").html(msg);
})
.fail(function(error){
console.log(error);
});
}
}
In your PHP you process receiving the data from post $_POST
$title = $_POST['title'];
You can understand better the Ajax function of jQuery here.
A few things:
I would change window.location to window.location.href
Change your echo to:
echo "Delete";
Check if $title is set
var_dump($title);
If you'd like to make it a bit cleaner and are prepared to use jQuery:
Delete
<script type='text/javascript'>
$(document).on('click', '#delete', function () {
var title = $(this).data('title');
var answer = confirm('Really?');
if (answer){
window.location.href = 'delete.php?id=' + title;
}
});
</script>
I want to pass on three variables using tag <a> and jquery, I pass two variables using the name and identifier and the class. I tried to use the value in tag <a> But it did not work.
this is tag <a>:
<a href="#inserts" class="add_result"
id="<?php echo $row1['id_visits'];?>"
name="<?php echo $id_patient;?>"
value="<?php echo $row1['id_testing'];?>">اضافة نتائج</a>
this is jquery
$( "a.add_result" ).click(function() {
var id_visits1 = $(this).id;
var id_patient1 = $(this).name;
var id_testing1 = $(this).value;
$.ajax({
type: "POST",
url: "result_form.php",
data: { id_visits:id_visits1,id_patient:id_patient1,id_testing:id_testing1 }
});
Any idea to help me?
For validity and safety of your code use data--attributes:
Jquery has a data() function which helps to get data-attributes values:
$( "a.add_result" ).click(function() {
var id_visits1 = $(this).data("id");
var id_patient1 = $(this).data("name");
var id_testing1 = $(this).data("value");
You can also use an onClick event. The code below is working.
اضافة نتائج
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
function clickMe(element){
var id = $(element).attr('id');
var name = $(element).attr('name');
var val = $(element).attr('value');
alert(id+' : '+name+' : '+val);
}
</script>
This is helpful if you want to get the value of the certain attribute or attributes of any element.
I have javascript that gets the Facebook name of a user when they login on my file indir.php. However, I cannot get that name javascript variable to transform into a php variable or a <input type="hidden" name="name" value="" />. Currently, I can get the name using:
<script>
...facebook stuff...
function login() {
FB.api('/me', function(response) {
document.getElementById('login').style.display = "block";
var mos = response.name;
document.getElementById('login').innerHTML = mos;
}
}
...
</script>
And then display the name using:
<div id="login" style ="display:none"></div>
This should let you get the name in the field
First Add an id to the input field
<input type="hidden" name="name" id="fb_name" value="" />
Then
<script>
...facebook stuff...
function login() {
FB.api('/me', function(response) {
document.getElementById('login').style.display = "block";
var mos = response.name;
document.getElementById('login').innerHTML = mos;
//Enter the value in field
var field= document.getElementById("fb_name");
field.value = mos;
}
...
</script>
I think part of the problem is that the opening brackets on the FB.api call are not closed see snippet below.
If this is this done then the javascript function will update the div with the value of response.name.
It would only need to be a php variable if you are doing something with the information server side. You could then send it using AJAX if needed.
function login(){
FB.api('/me', function(response) {
document.getElementById('login').style.display = "block";
var mos = response.name;
document.getElementById('login').innerHTML = mos;
});
});
You can send it to php in a POST-request using AJAX:
var xhr = new XMLHttpRequest();
var content = "c=" + mos;
xhr.open("POST", "serverscript.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(content);
Receive at server side using something like
<?php
if (isset($_POST['c'])) {
$data = htmlspecialchars($_POST['c'], ENT_QUOTES, 'UTF-8');
}
?>
Try this something like this:
Javascript:
<script>
var name = 'Luis';
var variablejs = 'Your javascript value: ' + name ;
</script>
PHP:
<?php
$variablephp = "<script> document.write(variablejs) </script>";
echo "variablephp = $variablephp";
?>
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
How to change my page title <title></title> after content is loaded in #container div?
$(document).ready(function () {
$("a.view").live('click', function () {
var id = $(this).data("id");
$('#container').load("view.php?" + id);
});
});
If without .load() I retrieve it by get the id via URL, example view.php?id=1
if (isset($_GET['id'])) {
$pageid = intval($_GET['id']);
$select = $db->query('SELECT id, title FROM content WHERE id="'.$id.'"');
$data = $db->fetch($select);
$title = $data['title'];
}
So in my header.php just call it <title><?php echo $title; ?></title>
If in this case file is load after header.php so how can I create dynamic my page title according to current .load("view.php?" + id)?
You can set the document.title property:
document.title = "My new title!";
add a callback function in the .load call, the responseText, textStatus, XMLHttpRequest are all arguments that will be passed to the callback.
function myCallback(responseText, textStatus, XMLHttpRequest)
{
document.title = "Set new title";
}
$(document).ready(function () {
$("a.view").live('click', function () {
var id = $(this).data("id");
$('#container').load("view.php?" + id,myCallback);
});
});