Cannot Call script tag inside php - javascript

I have this script below and its working good. It works when I load the .php file directly even with external javascript tags.
index.html
<div id="content"></div>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="test.js"></script>
test.js
$(document).ready(function() {
$('#content').load('content/index.php')
});
/content/index.php
<?php
require_once("geoip.inc");
$gi = geoip_open("GeoIP.dat", GEOIP_STANDARD);
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
geoip_close($gi);
if ($country_code == 'RO' ||
$country_code == 'DO' ||
$country_code == 'PK' ||
$country_code == 'MA' ||
$country_code == 'PE' ||
$country_code == 'IR' ||
$country_code == 'DZ' ||
$country_code == 'RU' ||
$country_code == 'EG') {
echo 'country not supported';
} else {
echo 'supported';
}
?>
My Question is:
When I am trying to
echo 'supported';
It is working good.
But when I tried to echo
<SCRIPT SRC="http://externalsite.com/ads/sample.js" TYPE="text/javascript"></SCRIPT>
the script tag wasn't show.
I need to load the .php file inside my .js file

try this..
echo "<SCRIPT SRC='http://externalsite.com/ads/sample.js' TYPE='text/javascript'></SCRIPT>";

Echo tags will not be displayed on page but contains in HTML. see if script tag present in HTML using browser inspector.
If you want to display tags on page then use htmlspecialchars()
echo htmlspecialchars("<SCRIPT SRC='http://externalsite.com/ads/sample.js' TYPE='text/javascript'></SCRIPT>");
Refer: http://php.net/manual/en/function.htmlspecialchars.php

When you try like below:
<?php
echo 'test';
echo '<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>';
die;
?>
Output will be:
test
Source code will be look like
test<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
UPDATE
You are loading an entire HTML page into your div, including the html, head and body tags. What happens if you do the load and just have the opening script, closing script, and JavaScript code in the HTML that you load?
Here is the driver page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery Load of Script</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.3.2");
</script>
<script type="text/javascript">
$(document).ready(function(){
$("#myButton").click(function() {
$("#myDiv").load("trackingCode.html");
});
});
</script>
</head>
<body>
<button id="myButton">Click Me</button>
<div id="myDiv"></div>
</body>
</html>
Here is the contents of trackingCode.html:
<script type="text/javascript">
alert("Outside the jQuery ready");
$(function() {
alert("Inside the jQuery ready");
});
</script>
This works for me in Safari 4.
Update: Added DOCTYPE and html namespace to match the code on my test environment. Tested with Firefox 3.6.13 and example code works.
For more refer this link

Related

How to handle google recaptcha with jquery?

i need a little info on google recaptcha. I want to grab the value of "g-recaptcha-response" that compares in the captcha.php file i inserted below in my jquery file and then send it to the captcha.php file using jquery $.post() method. I apologize if this is duplicate but i really cannot find someone with my same problem ;)
THE HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="AlterVista - Editor HTML"/>
<script src='https://www.google.com/recaptcha/api.js'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="handle_spam.js" type="text/javascript"></script>
<title></title>
</head>
<body>
<div class="g-recaptcha" data-sitekey="6Lf8LxIUAAAAALg93pw24l53KTeqrIwl7kUY-opk"></div>
<button id="go">Register</button>
</body>
</html>
THE PHP
<?php
$captcha=$_POST['g-recaptcha-response'];
echo $captcha;
if(!$captcha){
echo 'You must verify yourself';
exit;
}
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=6Lf8LxIUAAAAACB9iqeOermR-rPOW0zcWRfoetBO&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
if($response.success==false)
{
echo 'abort_all';
}else
{
echo 'success';
}
?>
THE JS
$(document).ready(function(){
$('#go').click(function(){
send=$('')
$.post('captcha.php',function(data){
alert(data);
});
});
});
Use this
<div class="g-recaptcha" data-callback="captchaCallback" data-sitekey="...">
and provide the function:
function captchaCallback(response) {
alert(response);
}

Sending value from jquery to php not working

I'm learning jquery and ajax. I have tried the following code, to post value to php from jquery. But it doesn't work. Can someone tell me what mistake am I doing here, and what is the solution.
I just want value1 to be printed by the PHP server side code, with that value being sent by the Jquery based client side code.
<html>
<head>
<title>Practice 1</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<?php
if (isset($_POST['data'])) {
$data = $_POST['data'];
print( "data is: $data" );
return;
}
?>
<body onload='process()'>
<script type="text/javascript">
$.post(window.location, {'data': "value1"}, function (data) {
$('#response').text(data);
});
</script>
</body>
</html>
You're trying to output to an element that doesn't exist '#response', also the process function is missing.
Try something like this:
<html>
<head>
<title>Practice 1</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
</head>
<?php
if (isset($_POST['data'])) {
$data = $_POST['data'];
print( "data is: $data" );
return;
}
?>
<body>
<div id="response"></div>
<script type="text/javascript">
$.post(window.location, {'data': "value1"}, function (data) {
$('#response').text(data);
});
</script>
</body>
</html>

PHP - autocomplete with php and javascript

I am working on an autocomplete script where i first read files from a local directory using php as shown below:
<?php
$file = glob('pages/*');
//var_dump($file);
foreach($file as $value)
{
$output = substr($value, 6, strlen($value) - 6);
//echo($output)."<br>";
}
?>
the above script displays all the files in the 'pages' folder i.e pageone.html,pagetwo.html....
i then use a javascript file to dispaly a text field that when entered for example 'page', should show aome autocomplete options say 'pageone.html' or 'pagetwo.html' e.t.c
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<script>
$(function() {
var availableTags = ["
<?php echo $output; ?>"
];
$( "#tags" ).autocomplete({
source: availableTags
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags">
</div>
</body>
</html>
i combine the above php code with this js to a single php file
as shown, i try embedding the '$output' into the js 'availableTags' variable but when i type something on the text field, nothing happens..i'm sure it has to do with the php code embedded in the js so any help would be appreciated
Your $output contains only a single value (last file in your list). You can create an array of files like:
$res = array();
foreach($file as $value)
{
$res[] = substr($value, 6, strlen($value) - 6);
}
and pass it to javascript as: a javascript array (with json_encode function)
<script>
$(function() {
var availableTags = <?=json_encode($res)?>
...
You can use autocomplete.js. It's more lite and easy to use that jQuery UI.

Can I update a JavaScript on mobile phones?

I have a mobile site where I am using JavaScript to refresh a message counter, JavaScript is enabled but it does not update the counter. The phone I am testing on is a Nokia e90. the other test phone is an HTC innovation and it works perfect on there.
Is it a issue of the JavaScript version on the e90 is out of date?
Here is my JS:
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><?php echo TITLE_HOME;?></title>
<meta http-equiv="content-language" content="en" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript" src="include/js/jquery-1.3.2.min.js"></script>
<!-- like this-->
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0b2.min.css" />
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a1/jquery.mobile-1.0b2.min.js"></script>
<script>
$(document).ready(function()
{
refresh();
});
function refresh()
{
$.get('newMessageCnt.php', function (cnt) {
$("#msgcntDiv").html("You have " + cnt);
//console.log(cnt);
//$("#msgcntDiv").text("You have " + cnt);
window.setTimeout(refresh,30000);
});
}
</script>
and the php:
<?php
if ($numOfMessages <> 0)
{
echo "<span class='headings_sub' id='msgcntDiv'>You have " .$numOfMessages . "</span>";
echo "<a class='red_link' href='".ADDRESS."messages.php'> unopened Messages</a>";
}
else
{
echo "<span class='headings_sub'>You have no new messages</span>";
}
?>
on the HTC $numOfMessages updates without a blink. On the E90 nothing happens
Try and add the id to the else echo statement
else {
echo "<span class='headings_sub' id='msgcntDiv'>You have no new messages</span>";
}
If you start with 0 messages then if other messages come you won't have a span id to update the text.
Symbian s60v3 navigator is based in webkit engine and javascript engine also.
Not sure to be the root cause of your problem. Give us your code, it can help.

JavaScript link click counter

i need a script to count link clicks on my website and count report to a flatfile/excel.
The use of a Javascript framework like jQuery would be wise in this case.
Note that you cannot save data into a file on the client's computer. Instead, you can do an AJAX to the server, and save it via your SSI on the server into database/excel/file whatever data storage there is.
My demo will be using jQuery, jQuery Cookie Plugin and PHP:
countdetect.js
jQuery(function(){
$("a").click(function{
var cookiename = 'linkcounter';
if($.cookie(cookiename) == null){
$.cookie(cookiename, 0);
}
$.cookie(cookiename, $.cookie(cookiename)+1);
});
});
index.php
<?php
session_start();
$counter_file = 'counter';
if(!file_exists($counter_file)){
file_put_contents($counter_file, 0);
}
$counts = (int)file_get_contents($counter_file);
file_put_contents($counter_file, $counts++);
// you can use $counts if you want to display it on the page.
?><!DOCTYPE html>
<html>
<head>
<title>Link Click Counter Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<script type="text/javascript" src="countdetect.js"></script>
</head>
<body>
<br />
<br />
Link clicks: <?php echo $counts; ?>
</body>
</html>

Categories

Resources