I using PHP/Javascript - Widget of alexmarandon from link http://alexmarandon.com/articles/web_widget_jquery/
And in project of me is:
script.js
html.php
demo.html
In script i using code:
(function() {
// Localize jQuery variable
var jQuery;
/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.7.1') {
var script_tag = document.createElement('script');
script_tag.setAttribute("type","text/javascript");
script_tag.setAttribute("src",
"https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
if (script_tag.readyState) {
script_tag.onreadystatechange = function () { // For old versions of IE
if (this.readyState == 'complete' || this.readyState == 'loaded') {
scriptLoadHandler();
}
};
} else {
script_tag.onload = scriptLoadHandler;
}
// Try to find the head, otherwise default to the documentElement
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);
} else {
// The jQuery version on the window is the one we want to use
jQuery = window.jQuery;
main();
}
/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
// Restore $ and window.jQuery to their previous values and store the
// new jQuery in our local jQuery variable
jQuery = window.jQuery.noConflict(true);
// Call our main function
main();
}
/******** Our main function ********/
function main() {
jQuery(document).ready(function($) {
/******* Load CSS *******/
var css_link = $("<link>", {
rel: "stylesheet",
type: "text/css",
href: "style.css"
});
css_link.appendTo('head');
/******* Load HTML *******/
var jsonp_url = "data.php?callback=?";
$.getJSON(jsonp_url, function(data) {
$('#example-widget-container').html(data);
});
$('.submit').click(function(){
alert('test');
});
});
}
})(); // We call our anonymous function immediately
Then i call data in data.php
$str = '<div class="widget_advance">';
$str .= '<form method="post" id="form_widget_advance">';
$str .= '<input type="submit" value="Tìm" class="submit" />';
$str .= '</form>';
$str .= '</div>';
echo $_GET['callback'] . '(' . json_encode($str) . ');';
finally, in demo.html is result:
<script src="script.js" type="text/javascript"></script>
<div id="example-widget-container"></div>
=> When I click on button submit is result not show alert('test'); How to fit it ?
Put this inside as such:
$.getJSON(jsonp_url, function(data) {
$('#example-widget-container').html(data);
$('.submit').click(function(){
alert('test');
});
});
You see, it has to be inside the callback. You can't bind to the submit button if it's not in the DOM yet. You could also do something along the lines of: $("#someForm").submit(function() { ... });
Related
I have function validate my form and after form submitted
else
{
$('.submit-button').val('Wait please...');
$('.submit-button').attr('disabled', 'disabled');
return true;
}
PHP redirect to main page
function wpcallback_get_target() {
global $wpcallback_plugin_option;
if($value = $callback_plugin_option['target'])
return get_permalink($value);
return get_site_url();
}
PHP get site url then how I can make thanks modal window opened?
I tried with .modal but can't find right place for it.
$('#myModal').modal('show');
place $('#myModal').modal('show'); in the bottom of you view file or you can add following code in head tag
$(document).ready(function(){
$('#myModal').modal('show');
});
to prevent model every time on page load, update your PHP file:
function wpcallback_get_target() {
global $wpcallback_plugin_option;
if($value = $callback_plugin_option['target'])
return get_permalink($value);
$url = get_site_url();
return $url."?response=true";
}
in view file:
<?php if(isset($_GET['response']) && $_GET['response'] == 'true'){ ?>
$(document).ready(function(){
$('#myModal').modal('show');
});
<?php } ?>
use Javascript code:
$(document).ready(function(){
var myURL = window.location;
var res = myURL.search("response=true");
if(res > 0){
$('#myModal').modal('show');
}
});
Try this on your php.
echo "<script>
$(window).load(function(){
$('#yourthanksmodel').modal('show');
});
</script>";
I want to make facebook like button.when i click like.undefined $ is appearing in console.it should echo 22 value default value on click.
function like_add(article_id) {
$.post('ajax/like_add.php', {article_id:article_id}, function(data){
if(data == 'success')
{
like_get(article_id);
}
else
{
alert(data);
}
});
}
function like_get(article_id) {
$.post('ajax/like_get.php',{article_id:article_id}, function(data){
$('#article_'+article_id+'_likes').text(data);
});
}
foreach($articles as $article){
echo '<li>',$article['article_title'],'</p><p><a class="like" href="#"
onclick="like_add(', $article['article_id'] ,');">Like</a>
<span id="article_', $article['article_id'] ,'_like">', $art
jQuery is not included in current page.
You can include it with this code:
<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
Place it between <body> </body> before your code.
But also you can load it async way:
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "https://code.jquery.com/jquery-2.2.1.min.js";
script.onreadystatechange = handler;
script.onload = handler;
head.appendChild(script);
function handler() { console.log("jQuery loaded!"); }
handler() will be called when jQuery will be loaded.
I've a jquery script that call the number2.php page, which is supposed to execute and show the result in a <div>. The problem is that is not working. Can you help me? Thanks.
<script>
$(function() {
$('#submit').click(function() {
if ($('#taille').val() != 0) {
var param = 'l=' + $('#taille').val();
}
else {
var param = 'b=' + $('#datepicker').val() + 'c=' + $('#datepicker1').val() + 'num' + $('#num').val();
}
$('#retour').load('number2.php', param);
);
});
</script>
Your code has a syntax error; it's missing closing } in the click handler function.
now it works but after loading number2.php my main page is refreshed and i lose the result !!!
In this case, you should hook to the submit event of the form element and call preventDefault() on the event to prevent the normal form submission. Try this:
$(function() {
$('#myForm').submit(function(e) { // change #myForm to target the <form>
e.preventDefault();
var param = {};
if ($('#taille').val() != 0) {
param.l = $('#taille').val()
}
else {
param.b = $('#datepicker').val();
param.c = $('#datepicker1').val();
param.num = $('#num').val();
}
$('#retour').load('number2.php', param);
});
});
I have a php file (a form) that includes javascript to check if all inputs are filled. When I view the php file directly the js works perfectly, but when I include the PHP file in another page the javascript no longer works.
My javascript code:
<script type="text/javascript" src="../js/modernizr.js"></script>
<script type="text/javascript">
window.onload = function(){
document.getElementById("topField").setAttribute("autocomplete","off");
}
window.onload = function() {
// get the form and its input elements
var form = document.forms[0],
inputs = form.elements;
// if no autofocus, put the focus in the first field
if (!Modernizr.input.autofocus) {
inputs[0].focus();
}
// if required not supported, emulate it
if (!Modernizr.input.required) {
form.onsubmit = function() {
var required = [], att, val;
// loop through input elements looking for required
for (var i = 0; i < inputs.length; i++) {
att = inputs[i].getAttribute('required');
// if required, get the value and trim whitespace
if (att != null) {
val = inputs[i].value;
// if the value is empty, add to required array
if (val.replace(/^\s+|\s+$/g, '') == '') {
required.push(inputs[i].name);
}
}
}
// show alert if required array contains any elements
if (required.length > 0) {
alert('The following fields are required: ' +
required.join(', '));
// prevent the form from being submitted
return false;
}
};
}
}
</script>
This is an explanation to anyone who finds this question later. In javascript you can only attach a single function to an event handler. If you want to attach more, you need to chain them. Pretty much all javascript framework/libraries has some sort of method to handle event chaining.
Javascript allows you to treat functions like variables. So you can assign an old onload function to a variable, then call it later within the new onload function.
If you are not using a framework, you can do something like this to handle event chaining.
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
You would call this with the following:
addLoadEvent(function(){
// Some code here
});
Ok this is my issue if anyone can help, please.
I have a href that div id to switch content - I would like to add another document ready function javascript without conflicting with make tab I have already.
Example of make tab already:
<script type="text/javascript">
{literal}
$(document).ready(function(){
function makeTabs(selector) {
var tabContainers = $(selector + ' > div');
tabContainers.removeClass("selected").filter(':first').addClass("selected");
galleryRendered = false;
$(selector + ' > ul a').click(function () {
tabContainers.removeClass("selected");
tabContainers.filter(this.hash).addClass("selected");
$(selector + ' > ul a').removeClass('selected');
$(this).addClass('selected');
if (this.hash == '#Pictures' && !galleryRendered)
{
var galleries = $('.pictures > .ad-gallery').adGallery({
effect : 'slide-hori',
enable_keyboard_move : true,
cycle : true,
animation_speed : 400,
slideshow: {
enable: false
},
callbacks: {
init: function() {
this.preloadImage(0);
this.preloadImage(1);
this.preloadImage(2);
}
}
});
galleryRendered = true;
}
if (this.hash == '#OnTheMap') document.getElementById("Map").map.onContainerChanged();
return false;
}).filter(':first').click();
}
makeTabs('.tabs');
});
{/literal}
</script>
Want to create a second one so I can create tabs inside of an existing div id area/content to switch from photo to video to youtube.
<div class=".tabs"><ul><li>[[Photo]]</li><li>[[Youtube]]</li><li>[[Video]]</li></ul><div id="photo">Test</div><div id="tube">Test</div><div id="vid">Test</div></div>
This will be inside a div id that already exist that uses the first tab creator shown above.
In jQuery you just have to do this:
$(function(){
// code here
});
$(function(){
// more code here
});
Every function declared like this will be executed on domready.