Jquery Ajax in Ie runs only once - php

My code i have set cache :false still in IE it runs only once . Please help me.
<script type="text/javascript" src="javascripts/jq1.7.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#butn").click(function(){
var txt1 = $("#txt1").val();
$.ajax({
type: "Post",
url: "jqueryphp.php",
dataType: "html",
data: "txt1="+txt1,
cache: false,
success: function(result) {
$("div").html(result);
}
});
});
});
</script>
</head>
<body>
<form>
<input type="text" id="txt1" /><br />
<input type="button" id="butn">
</form>
Please help me with this i am stuck here. It runs properly on every browser except IE

Try something like:
$.ajaxSetup({
cache: "false"
});
$.ajax({
type: "POST",
url: "jqueryphp.php",
data: "txt1="+txt1,
cache: false,
success: function(result){
$("div").html(result);
}
});
Hope it helps

IE is cache greedy. Considering adding a timestamp to each URL. Here are some other options too.
http://formatinternet.wordpress.com/2010/01/14/ie-cache-for-ajax-requests/

Make sure the file making the call has a proper doctype and xmlns declaration.
Assign a xmlns id to the elements that are involved in the call
example:
also use: jQuery.post('call.php',{ action: "get"}
find the full tutorial here: http://vavumi.com/?p=257

Related

How to check php is loaded when using AJAX

I use AJAX, and this work without problems, like this:
$('#main .left').load('top.php');
or this:
$.ajax({
type: "POST",
url: '/ajax_main.php',
data: "i=ajax_call",
success: function(msg){
console.log('ok');
}
});
// php side reprorts $_GET['i'] = "ajax_call";
When I set:
function periodicMethod(){
$.ajax({
type: "POST",
url: 'ajax_main.php',
data: "i=ajax_call",
success: function(msg) {
console.log('ok');
}
});
}
setInterval(periodicMethod, 1000);
The method is called and console.log reports "ok" any second. This looks like 'ajax_main.php' is loaded. But in the PHP file:
edit
echo '<script>console.log("php");</script>';
Does not give any results. The path in URL is ok. When I change it or remove 'ajax_main.php' from server, error is reported. What may be wrong?
edit
another version of php:
<script>
function ppp()
{ console.log('sss');
}
</script>
<?
echo '<script>ppp();</script>';
?>
is not working to
try this code
<script>
function periodicMethod(){
$.ajax({type: "POST",url: 'ajax_main.php',data: "i=ajax_call",
success: function(msg){ console.log('ok');
jQuery("#demo_data").html(msg);
}});}
setInterval(periodicMethod, 1000);
</script>
<html>
<body>
<form action="" method="post">
<input type="hidden" name="demo_data" id="demo_data"/>
</form>
</body>
</html>

Return false not working in my jquery ajax post, it sends to my php file

I'm sorry everyone.. :( my bad I actually didn't include the jquery library...
Sorry for that :3 i'm just very exhausted right now.
can someone tell me what's wrong and/or lack of my codes? returning false is not working.... is there any conflict in the codes below?...
html
<form class="login-form" method="POST" action="loginauthenticate.php">
<fieldset>
<h3>WELCOME!</h3>
<div id="get_content"></div>
<p>
<input class="username" type="text" name="username" placeholder="Username" autocomplete="off" required/>
<br>
<input class="password" type="password" name="password" placeholder="Password" required/>
<br>
</p>
<input id="register" class="login-button" type="submit" value="Log In" />
</fieldset>
</form>
<footer>
<P>© 2016 ACT Students,Thesis</P>
</footer>
js
<script type="text/javascript">
$(document).ready(function() {
$('form').submit(function() {
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data) {
$('#get_content').html(data);
}
});
return false;
});
});
</script>
This works for preventing default and Ajax also fires. Try it again.
<script type="text/javascript">
$(document).ready(function() {
$('form').submit(function() {
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data) {
$('#get_content').html(data);
}
});
return false;
});
});
</script>
You need to use event.preventDefault() and add event variable in submit(function(event). The event parameter comes from the event binding function. This is the standart method that stops the default action of an element from happening. In oter words if this method is called, the default action of the event will not be triggered.
return false; does 3 separate things when you call it :
1.event.preventDefault(); – It stops the browsers default behaviour.
2.event.stopPropagation(); – It prevents the event from propagating (or “bubbling up”) the DOM.
3.Stops callback execution and returns immediately when called.
preventDefault(); does one thing: It stops the browsers default behaviour.
So when to use them? Simply it depends on what you want to accomplish. Use preventDefault(); if you want to “just” prevent the default browser behaviour. Use return false; when you want to prevent the default browser behaviour and prevent the event from propagating the DOM. In most situations where you would use return false; what you really want is preventDefault().
<script type="text/javascript">
$(document).ready(function() {
$('form').submit(function(event) {
event.preventDefault();
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data) {
$('#get_content').html(data);
}
});
});
});
</script>
try to put return false before ajax work
$('form').submit(function() {
return false;
$.ajax({
data: $(this).serialize(),
type: $(this).attr('method'),
url: $(this).attr('action'),
success: function(data) {
$('#get_content').html(data);
}
});
});

how to get php response by using ajax

In a html web page, i want PHP response show in <span>..</span>.
Can anyone tell me how to do it or what's wrong with other codes?
thanks~~ ^^
web code:
<html>
<head>
<script type="text/javascript">
$("#submit").click(function(){
$.ajax({
type: 'get',
url: 'php/test.php',
dataType:'json',
success: function(data) {
//dosomething.....
},
});
});
</script>
</head>
<body>
<form method="post">
<input id="submit" type="button" value="next">
</form>
<br><br><span id ="status"></span>
</body>
</html>
php code:
<?php
echo ("success");
?>
After you get the response do the following
$("#submit").click(function(){
$.ajax({
type: 'get',
url: 'php/test.php',
dataType:'json',
success: function(data) {
$("span#status").html(data);
},
});
});
This won't work as the datatype provided won't be JSON... the dataType attribute in jQuery must match the type of data you send using PHP.
To get it working with a simple "success" message, you may do :
$("#submit").click(function(){
$.ajax({
type: 'get',
url: 'php/test.php',
dataType:'html',
success: function(data) {
$("span#status").html(data);
},
});
});
Because you use dataType: "json", the AJAX function thinks it's getting a JSON array. But since you only respond with "succes" in your php, it will not parse as JSON.
You should JSONify your output in PHP with json_encode(), after that you can call the succes callback in ajax, or use the .done function;
$("#submit").click(function(){
$.ajax({
type: 'get',
url: 'php/test.php',
dataType:'json',
success: function(data) {
$("span#status").html(data);
}
});
});
or with .done functionality:
$("#submit").click(function(){
$.ajax({
type: 'get',
url: 'php/test.php',
dataType:'json',
}).done(function(data) {
$("span#status").html(data);
});
});
The .done function is better for readability and maintainability in my opinion.

I am trying to fetch json data's from .php file.php file i.e stored in server.I have writtened the following code.But I am unable to fetch data

I am new to this jquery and phonegap.I am finding little difficulty in parsing the data from .php file from a local server.Please help me in doing so.
This is my index.html page:
<!DOCTYPE HTML>
<html>
<h2>JSON Parser</h2>
<script type="text/javascript" src="jquery.js"/></script>
<script type="text/javascript">
function parseJSON()
{
var json;
$.ajax({
type: 'POST',
url: 'http://192.168.1.12/training/services/login.php',
cache: false,
// data: $('#abc').serialize(),
dataType: 'json',
success: function(data){
alert(data);
$('#data').append(data);
}
});
}
</script>
</head>
<body onload="parseJSON()">
<p>Employee's Information</p>
<form id="abc" method ="post">
<div id="data"></div>
</form>
</body>
</html>
The login.php file contains a sample json data's as follows:
{"username":"test#test.com","password":"password"}
If you are trying to get the data using php and ajax use jsonp,
in your PHP file add callback for json output:
echo $_GET['callback'] . '('.json_encode($data).')';exit;
apppend callback in your ajax call
Javascript:
$.ajax({
url: 'http://192.168.1.12/training/services/login.php?callback=?',
cache: false,
type: "GET",
dataType: "jsonp",
contentType: "application/json; charset=utf-8",
success: function(data) {
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
And last but not least, dont forget to add domain white list for your localhost
in corodova.xml:
<access origin="http://192.168.1.12"/> OR
<access origin="*"/> to allow all domains
If I have understand correctly you may do something like that.
If your php page send data to your html one, you may use GET instead of POST
<!DOCTYPE HTML>
<html>
<h2>JSON Parser</h2>
<script type="text/javascript" src="jquery.js"/></script>
<script type="text/javascript">
function parse()
{
var json;
$.ajax({
type: 'GET',
url: 'http://192.168.1.12/training/services/login.php',
cache: false,
dataType: 'json',
success: function(data)
{
var obj = jQuery.parseJSON(data);
$('#data').html(obj["username"]);
}
});
}
</script>
</head>
<body onload="parse()">
<p>Employee's Information</p>
<form id="abc" method ="post">
<div id="data"></div>
</form>
</body>
</html>

$.ajax( type: "POST" POST method to php

I'm trying to use the POST method in jQuery to make a data request. So this is the code in the html page:
<form>
Title : <input type="text" size="40" name="title"/>
<input type="button" onclick="headingSearch(this.form)" value="Submit"/><br /><br />
</form>
<script type="text/javascript">
function headingSearch(f)
{
var title=f.title.value;
$.ajax({
type: "POST",
url: "edit.php",
data: {title:title} ,
success: function(data) {
$('.center').html(data);
}
});
}
</script>
And this is the php code on the server :
<?php
$title = $_POST['title'];
if($title != "")
{
echo $title;
}
?>
The POST request is not made at all and I have no idea why. The files are in the same folder in the wamp www folder so at least the url isn't wrong.
You need to use data: {title: title} to POST it correctly.
In the PHP code you need to echo the value instead of returning it.
Check whether title has any value or not. If not, then retrive the value using Id.
<form>
Title : <input type="text" id="title" size="40" name="title" value = ''/>
<input type="button" onclick="headingSearch(this.form)" value="Submit"/><br /><br />
</form>
<script type="text/javascript">
function headingSearch(f)
{
var title=jQuery('#title').val();
$.ajax({
type: "POST",
url: "edit.php",
data: {title:title} ,
success: function(data) {
$('.center').html(data);
}
});
}
</script>
Try this code.
In php code, use echo instead of return. Only then, javascript data will have its value.
try this
$(document).on("submit", "#form-data", function(e){
e.preventDefault()
$.ajax({
url: "edit.php",
method: "POST",
data: new FormData(this),
contentType: false,
processData: false,
success: function(data){
$('.center').html(data);
}
})
})
in the form the button needs to be type="submit"
Id advice you to use a bit simplier method -
$.post('edit.php', {title: $('input[name="title"]').val() }, function(resp){
alert(resp);
});
try this one, I just feels its syntax is simplier than the $.ajax's one...
function signIn()
{
var Username = document.getElementById("Username").value;
var Password = document.getElementById("Password").value;
$.ajax({
type: 'POST',
url: "auth_loginCode.jsp",
data: {Username: Username, Password: Password},
success: function (data) {
alert(data.trim());
window.location.reload();
}
});
}
contentType: 'application/x-www-form-urlencoded'

Categories