Script not working between php pages - javascript

The code below runs fine on my first PHP file, but when I load another file and use the same script, it does not appear to work, I get no alert from alert (idstate);. The CSS/HTML works fine.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("li").click(function(){
if (event.target.id!=""){
var idstate=(event.target.id)
alert (idstate);
$.post( "somepage.php", { variable: idstate}, function(data)
{
alert(data);
});
}
});
});
</script>

when You load another file you have to link that with your php page.<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
<script src="path/custom.js"></script>

Related

jQuery getJSON function not working in Blogger HTML/Javascript gadget

Relatively new to HTML and javascript, I was experimenting with some small code snippets.
I intend to add an HTML/JavaScript gadget with the following code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.getJSON('[server_url]', function(data, status) {
document.write(status);
});
});
</script>
However, this does not work. I have tested the server URL, it returns JSON correctly.
On the contrary, I could make the following work:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$.post('[post_server_url]', "some data");
});
</script>
What is it about the getJSON method that does not work?

Run PHP Script using an external Javascript file in HTML

I have an external Javascript file initialize_database.js that uses JQuery to call a PHP script to create a database and some tables. I've tested my PHP script by adding some HTML to it to run it on its own, and it works fine. My HTML is as follows:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<title>Test Webpage</title>
</head>
<body>
<script src="initialize_database.js"></script>
<div>
<h2>Nothing here yet!</h2>
</div>
</body>
</html>
Here is initialize_database.js:
$(document).ready(function() {
$.get('testPhp.php' {
alert('Databases were initialized');
});
});
I'd like to have the Javascript run as soon as the page loads so that the database can be created right away. All files are in the same directory. What am I doing wrong?
Comma and function initialization is required after the URL for executing the function correctly.
$(document).ready(function() {
$.get('testPhp.php', function() {
alert('Databases were initialized');
});
});
Check this
you can use this code . if you want to run when dom ready then
$(document).ready(function() {
$.get('testPhp.php', function() {
alert('Databases were initialized');
});
});
if you want to run this full load then use bellow
$( window ).load(function() {
$.get('testPhp.php', function() {
alert('Databases were initialized');
});
});

jquery not running when linked to an external js

Whenever I put the script in another file in the same folder as script.js, it doesn't run. It only runs when I include my code in the script tag.
I tried it out with this simple jquery code. Whenever the third script tag is uncommented, it works, but the set up I have right now doesn't run.
HTML
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" scr="script.js"></script> // <!-- doesn't run -->
<!--script> // <!-- only runs inside the html file -->
$(document).ready(function(){
$('p').click(function(){
$(this).hide();
});
});
</script-->
</head>
<body>
<p>Click to hide.</p>
</body>
</html>
script.js
$(document).ready(function(){
$('p').click(function(){
$(this).hide();
});
});
Does anyone know what I am doing wrong?
scr is not src. Use a validator to make sure you haven't misspelt attribute names.
Your jQuery code is being duplicated inside the script.js file and .html file so that's why the code won't run.
Remove the jQuery code from the .html file and your code will run. Also like was mention above replace scr with src in your .html file.
Finally, I'd replace the code in your script.js file with the new code listed below as click(function(){} is older code:
$(document).ready(function(){
$("p").on("click", function(){
$(this).hide();
});
});

JavaScript function stops working when it is moved to an external .js file

I moved a function from an html page to an included reference file. It stopped working.
The complete contents of the file are:
alert('file included');
function doAlert() {
alert('functions work');
}
In the html page, I have
<html>
<head><title>Page</title></head>
<body>
HTML Template Header
Some Content
ASP.NET MVC Partial View Starts
<script type="text/javascript">
doAlert();
</script>
ASP.NET MVC Partial View ends
HTML Template Footer
<script type="text/javascript" src="/Scripts/wtf.js"></script>
</body>
</html>
The 'file included' alert works, but the 'functions work' does not. What am I doing wrong?
Make sure you include the file before you execute the function.
<script src="file.js" type="text/javascript"></script>
<script type="text/javascript">
doAlert();
</script>
you should call your function(s) in the following way:
window.onload = function() {
doAlert();
// func_1();
// func_2();
// func_3();
// ...
// func_n();
};
See this for more information about the window.onload event.
Code example on jsFiddle

Why does $.post not do anything?

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js">
<script type="text/javascript">
$(document).ready(function() {
$.post("ajax.php", function(data) {
alert("Data Loaded");
});
});
</script>
I understand that this code does nothing useful, but why does nothing happen? The php doesn't run, and the alert doesn't show.
Your first SCRIPT element is missing its closing tag </script>, which leads to the whole code being just ignored.

Categories