This code is for getting all posted post and its content using ajax in WP
Function.php in WP
add_action('wp_ajax_my_action','data_fetch');
add_action('wp_ajax_nopriv_my_action','data_fetch');
function data_fetch(){
$this_query = new WP_Query(array('posts_per_page' => 10));
if($this_query->have_posts()):
while($this_query->have_posts()): $this_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php the_content(); ?></p>
<?php endwhile;
wp_reset_postdata();
endif;
die();
}
index1.php in core WP folder Where actual error occur
<html>
<head>
<title>My Ajax Page</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
function myajax(){
$.post('/wordpress/wp-admin/admin-ajax.php', {'action':'my_action'}, function(response){
$(#datafetch).append(response);
});
}
</script>
</head>
<body>
<div id="datafetch">
<button onclick="myajax()">Click Here</button>
</div>
</body>
</html>
The error message in console is
index1.php:15 Uncaught ReferenceError: myajax is not defined
at HTMLButtonElement.onclick
Related
i make control panel and there is a problem with jquery (data-toggle) not working and this message appear in console" bootstrap.min.js:6 Uncaught Error: Bootstrap's JavaScript requires jQuery at bootstrap.min.js:6 "
this is footer file
<div calss="footer">
</div>
<script src="<?php echo $js; ?>jquery-3.2.1.min.js"></script>
<script src="<?php echo $js; ?>jquery-ui.min.js"></script>
<script src=" <?php echo $js; ?>bootstrap.min.js"></script>
<script src="<?php echo $js; ?>jquery.selectBoxIt.min.js"></script>
<script src="<?php echo $js; ?>backend.js"></script>
</body>
</html>
and i include footer
include $tpl.'footer.php';
and thats working in main site with the same folders and pathes and names
i dont know whats wrong
Try this
Change the order of files it should be like below..
<script src="<?php echo $js; ?>jquery-3.2.1.min.js"></script>
<script src="<?php echo $js; ?>bootstrap.min.js"></script>
<script src="<?php echo $js; ?>jquery-ui.min.js"></script>
<script src="<?php echo $js; ?>jquery.selectBoxIt.min.js"></></script>
<script src="<?php echo $js; ?>backend.js"></script>
I am making my first steps learning to code. I made some courses on Internet an now I decided to build a Wordpress theme so like this I can continue learning from the practice.
I decided to make an image slider, so for this I decided to install a Jquery plugin. I know that I can install a wordpress plugin but I think that it's convenient to learn how to install a Jquery plugin too for many reasons.
I followed these steps to install the bxSlider plugin, but unfortunately there is something that it's not ok and it makes my plugin doesn't work:
1) Functions.php: I loaded all the files that I need
function loadbxslider() {
wp_enqueue_style('bxstyle', '/jquery.bxslider/jquery.bxslider.css');
wp_enqueue_script('bxscript', '/jquery.bxslider/jquery.bxslider.min.js', array('jquery'));
}
add_action('init', 'loadbxslider');
2) Header.php: I wrote this before <?php wp_head(); ?>:
<!--?php wp_enqueue_script('jquery'); ?-->
<!--?php wp_head(); ?-->
And this inmediately after <?php wp_head(); ?>:
<script type="text/javascript">// <![CDATA[
jQuery(document).ready(function(){
jQuery('#slidebx').bxSlider({
mode: 'horizontal',
infiniteLoop: true,
speed: 2000,
pause: 8000,
auto: true,
pager: false,
controls: true
});
});
// ]]></script>
So the first part of my header.php is like this:
?><!DOCTYPE html>
<html <?php language_attributes(); ?> class="no-js">
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="profile" href="http://gmpg.org/xfn/11">
<?php if ( is_singular() && pings_open( get_queried_object() ) ) : ?>
<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>">
<?php endif; ?>
<!--?php wp_enqueue_script('jquery'); ?-->
<!--?php wp_head(); ?-->
<?php wp_head(); ?>
<script type="text/javascript">// <![CDATA[
jQuery(document).ready(function(){
jQuery('#slidebx').bxSlider({
mode: 'horizontal',
infiniteLoop: true,
speed: 2000,
pause: 8000,
auto: true,
pager: false,
controls: true
});
});
// ]]></script>
</script>
</head>
3) bxSlider inside my content: using the id slidebx
<div id="slidebx">
<div><?php the_field('image1'); ?></div>
<div><?php the_field('image2'); ?></div>
<div><?php the_field('image3'); ?></div>
<div><?php the_field('image4'); ?></div>
<div><?php the_field('image5'); ?></div>
<div><?php the_field('image6'); ?></div>
<div><?php the_field('image7'); ?></div>
<div><?php the_field('image8'); ?></div>
<div><?php the_field('image9'); ?></div>
<div><?php the_field('image10'); ?></div>
</div>
I have a week trying to discover why this plugin doesn't work. If you have some suggestion would be good for my learning process.
How automatically refresh only one module in joomla?
<?php if ($this->countModules('parite')) : ?>
<section id="parite" >
<jdoc:include type="modules" name="parite" style="xhtml" />
</section>
<?php endif; ?>
That was the answer I was looking for.
<script type="text/javascript" src="https://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
var refreshId = setInterval(function()
{
jQuery('#parite').load('<?php echo $this->baseurl ?>/modules/mod_parite/mod_parite.php');
}, 1000);
</script>
<div id="parite"></div>
We've built a small project prototype for a client, which needs to be password protected because of our NDA. However - our use of the $_SESSION variable seems to break the parameters provided when entering from a secondary source even if we're logged in.
This is what we want to happen:
www.externalsite.com -> oursite.com/#/route?param="value"
This is what happens:
externalsite.com -> oursite.com/#/route?param="value" -> oursite.com/#/defaultRoute
It would be awesome if anyone could tell me how to progress past this issue - either by providing actual solution or by linking to resources that might help or get us pointed in the right direction.
Here's our index.php
!doctype html>
<html>
<head>
<title>prototype</title>
</head>
<body ng-app="angularApp" ng-controller="mainCtrl">
<?php require('access.php'); ?>
<div class="wrap">
<!-- CONTENT -->
</div>
</body>
</html>
access.php:
<?php
$password = '43844e5d424a5c7d228f265f8c899d47a65cf52f';
session_start();
if (!isset($_SESSION['loggedIn'])) {
$_SESSION['loggedIn'] = false;
}
if (isset($_POST['password'])) {
if (sha1($_POST['password']) == $password) {
$_SESSION['loggedIn'] = true;
} else {
die ('Incorrect password');
}
}
if (!$_SESSION['loggedIn']): ?>
<html>
<head>
<title>Login</title>
</head>
<body>
<form method="post">
<div class="form-group">
<label for="passwordInput">Password</label>
<input type="password" name="password">
</div>
<input type="submit" value="Login">
</form>
</body>
</html>
<?php
exit();
endif;
?>
The session_start() function must be the very first thing in your document. Before any HTML tags. You need to move it to index.php:
<?php
session_start();
?>
<!doctype html>
<html>
<head>
<title>prototype</title>
</head>
<body ng-app="angularApp" ng-controller="mainCtrl">
<?php require('access.php'); ?>
<div class="wrap">
<!-- CONTENT -->
</div>
</body>
</html>
i am unable to see what is wrong with the below code. I simply want to submit a php variable to another php page. please don't say sessions as i know for sure sessions wont work for me here. all i want is to send the session variable to the next php page via ajax and without the user knowing it.
<?php
session_start();
$fname=$_SESSION['mail'];
?>
<!DOCTYPE HTML>
<html>
<title>Addressbook</title>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(function(){
$.ajax({
url:"DbManipulate.php",
type:"POST",
data:"source1:"<?php echo $fname ?>""
});
}
</script>
<link rel="stylesheet" type="text/css" href="crudstyle.css" />
</head>
<body>
<div id="hidden_form_container" style="display:none;"></div>
<div id="mhead"><h2>Your Adressbook</h2></div>
<div id="note"> <span> your addressbook is connected to our servers :) </span></div>
<?php
echo $fname;
?>
<table id='demoajax' cellspacing="0">
</table>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
Passing data as a object is not correct in your code. Please change as follows and try
data:{source1:"<?php echo $fname ?>"}
There is some syntax error in your data:value part in $.ajax() call
You should use
data: "source1=<? php echo $fname ?>"
or
data:{source1:"<? php echo $fname ?>"}
<?php
session_start();
$fname="surat";
?>
<!DOCTYPE HTML>
<html>
<title>Addressbook</title>
<head>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
$(function(){
var sessioni='<?php echo $fname ?>';
$.ajax({
url:"DbManipulate.php",
type:"POST",
data:{source1:sessioni}
});
});
</script>
<link rel="stylesheet" type="text/css" href="crudstyle.css" />
</head>
<body>
<div id="hidden_form_container" style="display:none;"></div>
<div id="mhead"><h2>Your Adressbook</h2></div>
<div id="note"> <span> your addressbook is connected to our servers :) </span></div>
<?php
echo $fname;
?>
<table id='demoajax' cellspacing="0">
</table>
<script type="text/javascript" src="script.js"></script>
</body>
</html>