I have two .cshtml webforms, in one webform I have Bootstrap header and in other webform I have one div containg form. Now I want to implement such thing like when I click any of links available in Header that time Jquery function will be called and it should get desired page and load it into defined div block.
My code is seems as bellow (header.cshtml) :
<html>
<head>
<script type="text/javascript" src="../assest/js/jquery-3.1.1.js"></script>
<script type="text/javascript">
function myFunction(str) {
$(document).ready(function () {
//alert(str);
$.ajax({
type: "GET",
url: str + ".cshtml",
dataType: "html",
success: function (data) {
$("#form_load").html(data);
},
error: function (data) {
alert(data);
}
})
});
}
</script>
</head>
<body>
<div>
<a id="nav_link" onclick="myFunction(this.name)" name="farmer_master">Farmer</a>
</div>
<div id="form_load"></div>
</body>
</html>
other webform (farmer_master.cshtml) :
<div>
Hi I shoud be called !
</div>
Related
There are many stackoverflow entries about div refresh. I tryed some codes but I dont get it to work.
In my DIV I want to call a php function (get current database entries). What should I change? Is it even possible to reload a div to make a new php function call?
my index_design.php:
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"</script>
<body onload="startTime()">
<div id="ajaxcontainer">
<div>
<?php $mycontroller->getEntries(); ?>
</div>
<div>
<?php $mycontroller->getEntries2(); ?>
</div>
</div>
</body>
<script type="text/javascript" src="js/javascript.js"></script>
<script type="text/javascript" src="js/javascript_document_ready.js"></script>
my javascript_document_ready.js:
$(document).ready(function () {
function loadlink(){
$('#ajaxcontainer').load('index_design.php',function () {
$(this).unwrap();
});
}
loadlink();
setInterval(function(){
loadlink()
}, 10000);
}
Projectfolder:
index_design.php
Folder (JS) -> javascript.js
Folder (JS) -> javascript_document_ready.js
Try to use AJAX.
setInterval(_ => {
$.ajax({
url: "index_design.php",
type: "GET",
dataType: "html",
success: html => {
$("#ajaxcontainer").html(html);
}
});
}, 10000);
I have an external end point
localhost:5000 which loads a simple html page with an image. A flask server is running there which serves html content.
I have another html file called index.html . In the index.html, I have a div called movie-data . I want to make a Ajax request localhost:5000 and append the html content in the div movie-data of index.html .
index.html :-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function init() {
$.ajax({
dataType: "html",
url: "http://localhost:5000",
success: function (data) {
console.log(data);
$("#movie-data").html($(data).append(data));
}
});
init();
</script>
<div id="movie-data"></div>
It is showing error,
Uncaught SyntaxError: Unexpected end of input
init();
no closing tag of function was found
fix : -
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
function init() {
$.ajax({
dataType: "html",
url: "http://localhost:5000",
success: function (data) {
console.log(data);
$("#movie-data").append(data);
}
});
}
init();
</script>
<div id="movie-data"></div>
I have simple code with html, JavaScript and PHP. I am trying to pass a variable from HTML to PHP through ajax.
A text box appears on screen with button, user inputs in the text field and clicks the button. The field text should appear before the field. But in my case, nothing happens when clicking the button. I am posting my code below. These are the three files I have made:
index.html
<!doctype html>
<html lang="en">
<body>
<input id="name" type="text" /> <input id="button" type="button" value="Load" />
<div id="content"></div>
<script type="text/javascript" src="ajax.js"></script>
</body>
</html>
ajax.js
$('#button').click(function() {
document.write("this is javascript");
var name = $('#name').val();
$.ajax({
url: 'page.php',
data: name,
success: function(data) {
$('#content').html(data);
}
});
});
page.php
<?php
if(isset($_GET['name'])) {
echo "lllllllllllllllll";
echo $name=$_GET['name'];
}
?>
Add CDN as mentioned above. Also make sure you load the jquery CDN or jquery.lib.js before including external js file, ajax.js in index.html.
There is an issue in your $.ajax code:
$.ajax({
url: 'page.php',
data: name,
success: function(data) {
$('#content').html(data);
}
});
change this to:
$.ajax(
{
type:'GET',
url: 'page.php',
data:"name=test"
},
success: function(data) {
$('#content').html(data);
}
);
only add before axaj.js
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
add this in your javascript ajax: type : 'GET'.
In your php code :
$name=$_GET['name'];
echo $name;
I have been trying to change the content of a <div> with a JQuery Ajax Call.
But nothing happens when I press the onclick <a> element.
This is my current code that I'm testing with:
Front.php:
<html>
<head>
<script type="text/javascript" src="jquery.js">
function testAjax()
{
$.ajax({
type: "GET",
url: 'Back.php',
data: "id=", // Not using this yet >.<
success: function(data) {
$('#test').html(data);
}
});
}
</script>
</head>
<body><div id="test">HELLO</div>
Change this</body>
</html>
and here is my Back.php:
<?php
echo "test";
?>
Nothing happens when I click on "Change this" however the content in <div id="test"> should change to "test". Can someone help me with what I'm doing wrong? Thanks.
You didn't close your first script tag and open a new one for your inline script.
<script type="text/javascript" src="jquery.js"></script>
<script>
function testAjax()
{
...
You should apply contentType: "html" on it. May be that will help.
I have the following javascript test.js file:
$("#addItem").click(function () {
alert("yup");
$.ajax({
url: this.href,
cache: false,
success: function (html) { $("#bandRows").append(html); }
});
return false;
});
That I want to use to inject some HTML into the "bandRows" div on a page. I'm using Razor in an MVC 3 app like so:
The Index View, which contains the link that, when clicked injects a partial view HTML:
#model IEnumerable<TariffBand>
<script type="text/javascript" src="=#Url.Content("~/Scripts/jquery-1.3.2.js")"></script>
<script type="text/javascript" src="=#Url.Content("~/Scripts/MicrosoftAjax.js")"></script>
<script type="text/javascript" src="=#Url.Content("~/Scripts/test.js")"></script>
<h2>Index</h2>
#using (Html.BeginForm())
{
<div id="bandRows">
#foreach (var band in Model)
{
Html.RenderPartial("BandEditorRow", band);
}
</div>
#Html.ActionLink("Add band...", "Add", null, new { id = "addItem" })
<input type="submit" value = "Done" />
At the moment, when I click on the link the javascript is not being called - the alert box is not being displayed - and the link just navigates to the "Add" partial view rather than injecting it into the 'bandRows' div.
Can anyone tell me why? I haven't used javascript before so I've obviously done something daft but can't work it out for the life of me.
EDIT - I have amended the .js file so the handler is for click not onclick. I have also tried amending the html helper to:
#Html.ActionLink("add band...", "Add", null, new { onclick = "addItem" } but still no dice.
Thanks
David
You have
$("#addItem").onclick(function ()
There is nothing like $.onclick.
This will be
$("#addItem").click(function ()
Edit
$().ajax({ should be $.ajax({
and the whole code should be within document.ready() like
$(document).ready(function(){
$("#addItem").click(function (){
.
.
.
});
Edit - 2
As you have admitted that you are very new to javascript world, I am giving the detail code:
<script type="text/javascript" src="=#Url.Content("~/Scripts/MicrosoftAjax.js")"></script>
#* <script type="text/javascript" src="=#Url.Content("~/Scripts/test.js")"></script> *#
<h2>Index</h2>
<script type="text/javascript">
$(document).ready(function(){
$("#addItem").click(function (){
alert("yup");
$.ajax({
url: this.href,
cache: false,
success: function (html) {
$("#bandRows").append(html);
}
});
return false;
};
});
</script>
document ready is a very preliminary thing you need to learn when starting jQuery. This is the API documentation of ready event. And here is a tutorial for understanding document ready.