This is a simple sort fiddle, with a hard to debug error: it doesn't .append, on the other hand alert(data) is working, so this is not a ajax issue. Here is the code:
<!doctype html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div>Enter a sequence with spaces:</div>
<form method="post">
<input type="text" id="n" name="numbers">
<button>Sort</button>
</form>
<div>The sorted sequence is: <span id="s"></span></div>
<script>
$(document).ready(function(){$("form").submit(function({$.post("/tests/sort.php",$(this).serialize(),function(data){JSON.parse(data);$("#s").append(data);});})});
</script>
</html>
with the php
<?php
$s=$_POST["numbers"];
$a=explode(" ",$s);
sort($a);
echo json_encode($a);
?>
Can anybody please spot the error?
As I mentioned in comment for your question you have an error in JS code. To avoid errors like this you have to edit code for better reading. Here is an example:
$(document).ready(function () {
$("form").submit(function () {
$.post("/tests/sort.php", $(this).serialize(), function (data) {
JSON.parse(data);
$("#s").append(data);
});
})
});
You are missing a right parenthesis ) and a semicolon ;. You can use jshint.com to check your syntax, and should indent your code so it is easily readable:
$(document).ready(function(){
$("form").submit(function(){
$.post("/tests/sort.php",$(this).serialize(),function(data){
JSON.parse(data);$("#s").append(data);
});
});
});
Related
I am running into a javascript issue where it's getting a "Uncaught ReferenceError: $ is not defined". With my reading I think it's the order of when I am trying to do $(document).ready before the javascript is loaded up but I am not sure what to do about it. I have tried moving the jquery and the fileuploadmulti above the script but then I get "Uncaught TypeError: $(...).uploadFile is not a function" and then I go down the road of chicken and egg and don't know what is correct. Any help or point in the right direction would be helpful. Thank you.
<script>
//This is where my error shows up
$(document).ready(function(){
var settings = {
url: "/index.php/upload",
method: "POST",
allowedTypes:"jpg,png,gif,doc,pdf,zip",
fileName: "myfile",
multiple: true,
onSuccess:function(files,data,xhr)
{
$("#status").html("<font color='green'>Upload is success</font>");
},
afterUploadAll:function()
{
alert('All Files uploaded');
},
onError: function(files,status,errMsg)
{
$("#status").html("<font color='red'>Upload is Failed</font>");
}
};
$("#mulitplefileuploader").uploadFile(settings);
});
</script>
<script src="/js/jquery.js"></script>
<script src="/js/fileuploadmulti.min.js"></script>
<div class="boxed link">
<div id="mulitplefileuploader">Upload</div>
<div id="status"></div>
</div>
<div class="col-lg-12">
<h1>Adding pages to <?php echo $model->name; ?></h1>
</div>
First check whether JQuery is loaded or not.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
And wrap the all JQuery code inside $(document).ready(function(){ \\code here }); function.
$(...).uploadFile is not a function error because you are not loading the fileupload plugin properly.
Check all script tag URL'S are right or not.
You are using the $ before including jQuery. Try to include it before.
Try to include
<script src="/js/jquery.js"></script>
<script src="/js/fileuploadmulti.min.js"></script>
before $(document).ready(function(){ });
Hie all,
Here is my code part.page1.php
<?php
$ID="1,2,3,4,";
$ID1=json_encode($ID);
?>
<html>
<script src="page.js" type="text/javascript"></script>
<body onload="<?php echo'add('.$ID1.')';?>">
</body>
</html>
//page.js code
function add(oi1)
{
alert(oi1);
}
It gives me syntax error near add(..
I want to pass "1,2,3,4,"; on add function.
Try:
<body onload="add('<?php echo $ID1?>')">
It's often better writing like this instead of your example.
I'm absoluteley new to JS and i cant figuere out how i should proceed to round the values (Counter1 & Counter2) which come from the external File IOCounter.html with a Json-Structure.
I tried to put the math.round() to different places within the code which seemed to be logical for me- but nothing worked.
Do i have to define Var's, round these and write the the result to the label?
Thanks for your help.
<script src="jquery-2.1.1.min.js"></script>
</head>
<body>
<label id="counter1">0</label>
<label id="counter2">0</label>
</body>
<script type="text/javascript">
$(document).ready(function(){
$.ajaxSetup({ cache: false });
setInterval(function() {
$.getJSON("IOCounter.htm", function(result) {
$('#counter1').text(result["counter1"]);
$('#counter2').text(result["counter2"]);
});
},50);
});
</script>
You don't have to define vars per se. you could to this:
$('#counter1').text(Math.round(result["counter1"]));
I'm trying to learn basic HTML and Javascript, and am not sure what is wrong with this code. It is probably a very simple error and I'm sorry if it is. When I try clicking the buttons, Chrome says in the console that "correct" and "incorrect" are not defined, but I have checked the syntax for the functions and I can't see what is wrong. Thanks for your help :)
<!DOCTYPE html>
<html>
<head>
<title>Question 1</title>
</head>
<body>
<p>Q1: What is the height of the Eiffel Tower?</p>
<br>
<script>
function incorrect()
{
document.getElementById("feedback").innerHTML =
"incorrect!
<br>
Next Question";
}
function correct()
{
document.getElementById("feedback").innerHTML =
"Correct!
<br>
Next Question";
}
</script>
<button onclick="incorrect()">767m</buttton>
<br>
<button onclick="incorrect()">442m</button>
<br>
<button onclick="correct()">324m</button>
<br>
<button onclick="incorrect()">278m</button>
<p id="feedback"></p>
</body>
You have confusing ""(double quotes) in the innerHTML strings. Try this:
instead of "q2.htm" use 'q2.htm'
<script>
function incorrect()
{
document.getElementById("feedback").innerHTML =
"incorrect!<br><a href='q2.htm'>Next Question</a>";
}
function correct()
{
document.getElementById("feedback").innerHTML =
"Correct!<br><a href='q2.htm'>Next Question</a>";
}
</script>
If you look at the console log in Chrome (press F12 to enter Developer Tools where you can see the log), you will see an error message “Unexpected token ILLEGAL”. The reason is that you have line breaks inside a JavaScript string, which is not permitted, so the function definitions fail in parsing. Moreover, you are using quotes inside a quoted string, which isn’t permitted either. Use single quotes (') as inner quotes or (in this case) just omit them, e.g.
function incorrect()
{
document.getElementById("feedback").innerHTML =
"incorrect!<br><a href=q2.htm>Next Question</a>";
}
This works:
<script>
function incorrect()
{
document.getElementById("feedback").innerHTML =
"incorrect!<br><a href='q2.htm'>Next Question</a>";
}
function correct()
{
document.getElementById("feedback").innerHTML =
"Correct!<br><a href='q2.htm'>Next Question</a>";
}
</script>
You have to put them on the same line or use concatenation.
I have a ..weird problem.
I have this function JavaScript:
function example() {
alert('a');
}
And i have this 2 inputs:
This does NOT work
<input type="image" src="img/erase.png" alt="Borrar" onclick="example();">
this WORKS
<input type="image" src="img/erase.png" alt="Borrar" onclick="alert('a');">
I know they do the SAME but in me real project i want create with PHP bucle a items and they should call JavaScript.
This example is for understan easy this problem.
Thank you ppl! :D
Solution:
In mi code in PHP i write bad the "echo" and put a lot of " and ', and when i go to write the parameters can write ' or " because they are used to write and describe the "echo" (PHP) and then i should write """.
example:
echo "<input type='image' src='img/erase.png' alt='Borrar' onClick='borrarPost("NOTICIA","".$row['Titulo']."",".$row["ID"].")'>";
the " is for a parameters String, normal echo is like echo "Hi" but this who write a input need use ",' and "
It works for me:
<!DOCTYPE html>
<html>
<head>
<title>test</title>
<script>
function example() {
alert('a');
}
</script>
</head>
<body>
<div>
<input type="image" src="img/erase.png" onclick="example()">
</div>
</body>
</html>
Note that it only works if you have the example function in the root scope of the javascript. For example, this wouldn't work:
window.onload = function() {
function example() {
alert('jo');
}
}