Jquery Not Working On Include File.Here is My Code Sample.But in LocalHost That's Work Fine.and also i am using Bootstrap.
<body>
<?php
include('header.php');
if(UserType=="User")
{
// In This Portion Jquery Work :)
include('BProfile.php');
}
elseif(userType=="Premium")
{
//In this Portion Jquery Dose Not Work but file load properly :(
include('PProfile.php');
}
else
{
echo 'unable To Load Profile';
}
include('footer.php');
?>
//I also Put The Jquery On Top But Dose Not Work for PProfile
<script type="..." src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</body>
Thanks and best reagards
Related
How do I call my functions from functions.php to html.php with the use of AJAX? I want to declare my functions inside my html.php file without using include or require. only ajax pls. Thanks allot! see below
functions.php
<?php
function samplefunc(){
echo "Hello world";
}
?>
html.php
<html>
<body>
<?php samplefunc(); ?>
</body>
</html>
add the below code in html.php
$.post("functions.php", {call_function: samplefunc})
.done(function(response) {
alert(response);
}
and add below code in functions.php
function samplefunc() {
echo 'Answer';
}
if ($_POST['call_function'] == "samplefunc") {
samplefunc();
}
I'm trying out a more maintainable way to structure my code as learnt by some tutorials at nettuts+.
I've succeeded in making my header and footer files separate from the main content of each page so that they are loaded along with each different page, thus making it easier to manage changes to these two files.
I currently want to add some jQuery code to my footer, for only one page. This is where I've hit a wall in thinking. How can I allow this jQuery code to be present in my footer for only one page, and not all pages? Could someone please explain?
I was thinking of doing something like:
<?php if($title = "The jQuery Page"){?>
<script>
jquery code here....
</script>
<?php } ?>
But I think that is considered messy, as you are using one language to set off another.
Edit: Here's the requested code:
In my Controller I call the views:
$data['title'] = "The jQuery Page";
$this->load->view('header', $data);
$this->load->view('cool_page');
$this->load->view('footer');
Then in my footer I want to load a specific script in only this one pages footer:
<script src="<?php echo base_url();?>js/jquery-1.10.2.min.js"></script>
<script>
$(document).ready(function() {
/*cool jquery stuff*/
});
</script>
Try to load a view into the main view
/application/controllers/test.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Test extends CI_Controller {
public function index() {
$data = array(
'title' => "The jQuery Page",
'script' => TRUE // if you omit or comment this variable the 'views/script.php' is not loaded
);
$this->load->view('test', $data);
}
}
/application/views/test.php
<html>
<head>
<title><?php echo $title;?></title>
</head>
<body>
<?php if (isset($script)) { $this->load->view('script'); } ?>
</body>
</html>
/application/views/script.php
<script>
jquery code here....
</script>
You could specify JS to include as part of the data passed to the view:
CONTROLLER1
$this->data['js'][] = 'alert(\'Hi\');';
$this->data['js'][] = 'alert(\'Hey\');';
$this->load->view('footer', $this->data);
CONTROLLER2
$this->data['js'][] = 'alert(\'Yo\');';
$this->data['js'][] = 'alert(\'Sup\');';
$this->load->view('footer', $this->data);
Footer VIEW
if(isset($js) && is_array($js) && count($js) > 0){
echo '<script>';
foreach($js as $key=>$value){
$value;
}
echo '</script>';
}
I don't think there is a way around making a conditional 'if' statement around the script, so I used the suggested solution in my question.
Check the title to see if it is the desired page:
<?php if($title = "Photo Albums - Hands of Humanity"):?>
Then add the script to the DOM if it is so:
<script>
$(document).ready(function() {
//jquery stuff
});
</script>
<?php endif;?>
If there are other ways which are better for maintainability, I would be happy to hear them
I'm not able debug what is wrong with my code.
Here is what I'm doing:
For Ajax queries, I have set a session variable: sessiontoken, and I encode and decode to make sure the request is from correct session/page as shown below, below code is present on every page, And then in the javascript I echo this variable and use in ajax call:
My problem is that while the script is working perfectly fine on every page on the local server(xampp), on the live server it is giving problem.
When I login on live site, on home page the decoded value is correctly displayed in the javascript function, however when I click on the link to myprofile or go to any other page, the decoded sessiontoken value displays weird characters.
Can you please let me know what could be the problem, as on local it is working just fine.
thanx.
<?php
session_start();
if(!(isset($_SESSION['id']) && isset($_SESSION['username']))) {
header("location: index.php");
exit();
}
if(!isset($_SESSION['sessiontoken'])) {
$thisRandNum = rand(999999,99999999);
$_SESSION['sessiontoken'] = base64_encode($thisRandNum);
} else {
$thisRandNum = base64_decode($_SESSION['sessiontoken']);
}
?>
<html>
<head>
<script type="text/javascript">
function refreshWall() {
$.post('scripts/refreshlist.php', {thistoken: <?php echo $thisRandNum ?>},
function(data) {
}, "json");
}
</script>
</head>
<body>
</body>
</html>
I upgraded the PHP version to 5.3 and it solved my problem.
I'm building a web application inside CodeIgniter and I've decided to have a fiddle with jQuery and Javascript - something I've never really used before.
I have included jQuery in my header using Google Libraries:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
And now I'm fiddling with the SIMPLEST of jQuery but it's not working.
<p><?php if ($post->love) { echo $post->love; } else { echo 0; } ?></p>
<script type="text/javascript">
$("#lovecounter").click(function() {
alert("Click");
});
</script>
I am clicking on the lovecounter link but there is no alert. If I add simple javascript to the actual anchor tag it works though.
<p><?php if ($post->love) { echo $post->love; } else { echo 0; } ?></p>
Any ideas? What am I doing wrong?
Wrap your code in ready handler:
$(document).ready(function(){
$("#lovecounter").click(function() {
alert("Click");
});
});
Working Example
This works perfectly fine for me:
<p>asdf</p>
<script type="text/javascript">
$(document).ready(function() {
$("#lovecounter").click(function() {
alert("Click");
});
});
</script>
You should include a $(document).ready(function() {}); just like Sarfraz said. Otherwise, it would not find anything with an id of lovecounter because nothing on the page has loaded yet. If you wait for it to load, then it will find the element. Here is some documentation: http://www.w3schools.com/jquery/event_ready.asp. Here is a full jQuery lesson: http://www.codecademy.com/tracks/jquery.
This is my php file where it creates a session for the user:-
<?php
//starting the session
session_start();
//checking if session of user is set ot not
if(isset($_SESSION['UID'])) {
echo "Welcome, ".$_SESSION['UID'];
}
else {
header("Location: ErrorPage.html");
}
?>
And this is my jQuery file:-
$(document).ready(
function hiding() {
$("#links").hide();
}
);
I want my "#links" to be hidden after a user logs in. How do I include the jQuery file in my if statement in php? I tried:-
include("jqueryFile.js");
require("jqueryFile.js");
But it didn't work.
Require and include is for server side files, like php.
you should simply do something like
echo '<script type="text/javascript" src="jqueryFile.js"></script>';
If you want to use the require/include commands you should store your javascript in a .php file
require('jqueryFile.php');
<?php if (isset($_SESSION['uid'])) { ?>
<script type="text/javascript" src="jqueryFile.js"></script>
<?php } ?>
you have to output that file (ready to deploy into client)
if (isset($_SESSION['uid'])){
?>
<script src='jqueryFile.js'></script>
<script>hiding();</script>
<?php
}