<!DOCTYPE HTML>
<html>
<head>
<title>Scroll Pagination</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<script src="jquery.js"> </script>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
type: "GET",
url: "ajax.php",
data:{
'offset':0,
'limit':5
},
succes:function(data)
{
$('body').append(data);
}
});
});
$(window).scroll(function(){
if($(window).scrollTop() >= $(document).height() - $(window).height()){
$.ajax({
type: "GET",
url: "ajax.php",
data: {
'offset':0,
'limit':3
},
succes:function(data)
{
$('body').append(data);
}
});
}
});
</script>
<style type="text/css">
.blog-post{border-bottom: solid 4px black; }
.blog-post h1{font-size:40px;}
.blog-post p{font-size:30px;}
</style>
</head>
<body>
</body>
</html>
Here is the html and ajax code.
And here is the PHP code
<?php
$con = mysqli_connect('localhost', 'root', '', 'table') or die();
print_r($_GET['limit']);
if(isset($_GET['offset']) && isset($_GET['limit'])){
$limit=$_GET['limit'];
$offset=$_GET['offset'];
$result = mysqli_query($con, "SET NAMES utf8");
$data = mysqli_query($con, "SELECT * FROM portf LIMIT {$limit} OFFSET {$offset}");
while ($portf = mysqli_fetch_array($data)){
echo '<div class="blog-post"><h1>'.$portf['title'].'</h1><p>'.$portf['category'].'</p></div>';
?>
<?php }}else{echo "fail";} ?>
I have no idea why in the index I can't see nothing what can I do to solve this problem? I saw this example on youtube and is working but..when I'm trying to do the same nothing appears.
Edit:
Statuts in network
There is syntax error in your code at time of ajax call due to which it does not enter in success.
Change
succes:function(data)
To
success:function(data)
It should be "success".
Related
I want to have 2 buttons on a page. If u click button "addMe", then I want to add 1 to a variable? (theCount). The other button (InsertDB) I want to add "theCount" into my db.
Im able to add data to my db, but not "theCount", probly because its a "div id" and I dont know how to do it. I have 3 files: index.php, addscript.js and insert.php
Here is my script:
index.php:
<?php
include "insert.php";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Add 1 on click, then add sum to db</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<button id="addMe">Add 1</button>
<div id="theCount"></div>
<form method="post">
<button id="InsertDB">Add to DB</button>
</form>
</body>
</html>
<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
<script src="addscript.js"></script>
addscript.js:
var counter = 0;
$(document).ready(function() {
$("#InsertDB").click(function(){
var theCount= $("#theCount").val();
$.ajax({
type: "POST",
url: "insert.php",
data: "theCount=" + theCount,
success: function(data) {
alert("Added to DB");
}
});
});
$("#addMe").click(function(){
counter++;
$("#theCount").text(counter);
});
});
insert.php:
<?php
include "db.php";
$theCount=$_POST['theCount'];
$sql = "INSERT INTO `mat`( `polse`)
VALUES ('$theCount')";
if (mysqli_query($conn, $sql)) {
echo "Craig is Satoshi";
}
else {
echo "Error";
}
mysqli_close($conn);
?>
#theCount is a <div>:
<div id="theCount"></div>
And a <div> doesn't have a value, so this won't work:
var theCount= $("#theCount").val();
Instead, get the text of the element:
var theCount= $("#theCount").text();
Much in the same way that you already set the text of the element:
$("#theCount").text(counter);
I want to make form html look like this :
<script>
function name()
{
var name = $('#name').val();
var url_send = 'send.php';
$.ajax({
url : url_send,
data : 'name='+name,
type : 'POST',
dataType: 'html',
success : function(pesan){
$("#result").html(pesan);
},
});
}
</script>
<script src="assets/bootstrap-sweetalert.js"></script>
<script src="assets/sweetalert.js"></script>
<link rel="stylesheet" href="assets/sweet-alert.css">
<form action="#" method="post">
<label>Name</label>
<input type="text" name="name"><br>
<input type="submit" onclick="name();">
<div id="result"></div>
and this is send.php
<?php
$name = $_POST['name'];
if($name) {
echo 'success';
} else {
echo 'failed';
}
?>
And the problem is,how i show SweatAlert modal,when result of php is Success Sweatalert will show Success Modal.And when failed it will show Failed Modal ?
Now what must i edit to my script?
I hope you are expecting code with jquery ajax;
See the code below;
index.php
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<link rel="stylesheet" type="text/css" href="sweetalert-master/dist/sweetalert.css">
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="sweetalert-master/dist/sweetalert.min.js"></script>
<script>
$(document).ready(function () {
$("#submit").on("click", function(e){
e.preventDefault();
var name_val = $("#name").val();
$.post( "send.php", {name_send:name_val}, function(data){
if (data == 'success'){
swal("Good job!", "Nice work!", "success");
return false;
}else{
swal("Here's a message!", "Please type a name");
}
});
});
});
</script>
</head>
<body>
<form>
<label>Name</label>
<input type="text" name="name" id="name"><br>
<input type="submit" id="submit">
</form>
</body>
</html>
send.php
<?php
$name = $_POST['name_send'];
if($name) {
echo 'success';
} else {
echo 'failed';
}
?>
$.ajax({
url : url_send,
data : {name: name},
type : 'POST',
dataType: 'html',
success : function(pesan){
$("#result").html(pesan);
},
});
in your ajax code you placed data : 'name='+name, , please have a look at what i did , maybe that helps
Jquery ajax accepts object in data parameter data: {key:value}. In the question we are using string as a parameter to data of jquery ajax
Basically I've got a AJAX Login page that is working and everything but when I successfully login I want it to redirect to a page without it reloading I'm not sure how its done as I am very new to langauge. Many thanks
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="jquery.js"></script>
<link rel="stylesheet" href="styles.css" type="text/css" />
<title>Popup Login</title>
<script type="text/javascript">
$(document).ready(function(){
$("#login_a").click(function(){
$("#shadow").fadeIn("normal");
$("#login_form").fadeIn("normal");
$("#user_name").focus();
});
$("#cancel_hide").click(function(){
$("#login_form").fadeOut("normal");
$("#shadow").fadeOut();
});
$("#login").click(function(){
username=$("#user_name").val();
password=$("#password").val();
$.ajax({
type: "POST",
url: "login.php",
data: "name="+username+"&pwd="+password,
success: function(html){
if(html=='1')
{
$("#login_form").fadeOut("normal");
$("#shadow").fadeOut();
$("#profile").html("<a href='logout.php' id='logout'>Logout</a>");
}
else
{
$("#add_err").html("Wrong username or password");
}
},
beforeSend:function()
{
$("#add_err").html("Loading...")
}
});
return false;
});
});
</script>
</head>
<body>
<?php session_start(); ?>
<div id="profile">
<?php if(isset($_SESSION['user_name'])){
?>
<a href='logout.php' id='logout'>Logout</a>
<?php }else {?>
<a id="login_a" href="#">login</a>
<?php } ?>
</div>
<div id="login_form">
<div class="err" id="add_err"></div>
<form action="login.php">
<label>User Name:</label>
<input type="text" id="user_name" name="user_name" />
<label>Password:</label>
<input type="password" id="password" name="password" />
<label></label><br/>
<input type="submit" id="login" value="Login" />
<input type="button" id="cancel_hide" value="Cancel" />
</form>
</div>
<div id="shadow" class="popup"></div>
</body>
</html>
login.php
<?php
session_start();
$username = $_POST['name'];
$password = $_POST['pwd'];
$mysqli=mysqli_connect('');
$query = "SELECT * FROM user WHERE username='$username' AND password='$password'";
$result = mysqli_query($mysqli,$query)or die(mysqli_error());
$num_row = mysqli_num_rows($result);
$row=mysqli_fetch_array($result);
if( $num_row >=1 ) {
echo '1';
$_SESSION['user_name']=$row['username'];
}
else{
echo 'false';
}
?>
When I successfully login I want it to redirect to a page without it reloading
As long as the page to be redirected to is on the same domain, or you can set CORS headers properly, and you can deal with relative links, then in your success callback you can replace the entire page's HTML with that of the new page.
You can start another ajax call for your new page, and use document.write() to obliterate the existing HTML like so:
...
success: function(html){
if(html=='1') {
$("#login_form").fadeOut("normal");
$("#shadow").fadeOut(400, function(){
// Completely replace the page's HTML
$.ajax({
type: 'GET',
url: 'http://example.com/logged-in',
async: false, // You want this synchronous
success: function(data) {
// This code block replaces your current page seamlessly
document.open();
document.write(data);
document.close();
},
error: function(e){
alert(e.message);
}
});
});
}
...
Here is a demo:
$("#bye").fadeOut(1400, function(){
$.ajax({
type: 'GET',
url: 'http://enable-cors.org/',
async: false,
success: function(data) {
// This is a quick-n-dirty hack
// to get the relative links working for the demo
var base_href = '<base href="http://enable-cors.org/">';
document.open();
document.write(base_href + data);
document.close();
},
error: function(e){
alert(e.message);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="bye"><h1>Logged in Successfully</h1></div>
I searched through the forums anything that help me and nothing is working... I'm trying to do a login on Phonegap using a AJAX call to a PHP file located in a remote server but the PHP file is not returning anything... I post my codes:
The AJAX call:
$.ajax({
type: "POST",
data: 'userMail='+user+'&userPassword='+password,
dataType : 'html',
url : "http://www.eega.nl/app/login.php",
crossDomain: true,
async: false,
success: function(data){
if(data!='error'){
window.localStorage["userId"] = data;
location.href = "schedule.html";
}else{
alert("failed login");
location.href = "index.html";
}
},
error: function(){
alert("error");
window.open('schedule.html');
}
});
The PHP file:
<?php
include 'connect.php';
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS, REQUEST');
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Max-Age: 3628800');
$data = array();
$userMail = $_POST["userMail"];
$userPassword = $_POST["userPassword"];
$cryptpass = md5($userPassword);
$sql = "SELECT tr_adr_id FROM elo_users WHERE email = '" . $userMail . "' AND crypt = '" . $cryptpass . "'";
//mysql_real_escape_string($userMail),
//mysql_real_escape_string($cryptpass));
$result = mysqli_query($sql);
//if($data = mysql_fetch_array($result)){
while($row = mysqli_fetch_array($result)) {
$data = $row['tr_adr_id'];
}
echo $data["tr_adr_id"];
mysqli_free_result($data);
mysqli_close();
?>
I tried to do the AJAX call with POST and GET and in the PHP file with the $_POST, $_GET and $_REQUEST and nothing of that worked for me...
Thank you in advance! I still working on that trying to figure out what is wrong in my code...
EDITED:
The index.html:
<!DOCTYPE HTML>
<html>
<head>
<meta name="viewport" content="width=320" user-scalable="no" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/bootstrap-theme.min.css">
<link rel="stylesheet" href="css/docs.min.css">
<link rel="stylesheet" href="css/jquery.mobile-1.4.3.min.css">
<link rel="stylesheet" href="css/general.css">
<script type="text/javascript" src="js/jquery-1.11.1.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/functions.js"></script>
<title>EegaApp</title>
<script type="text/javascript" charset="utf-8" src="cordova.js"></script>
</head>
<body>
<script type="text/javascript" src="js/jquery.mobile-1.4.3.min.js"></script>
<div id="eegaLogo">
<img class="bottom" src="images/eega_logo_loading.jpg"/>
<img class="top" src="images/eega_logo.jpg"/>
</div>
www.eega.nl
<!--*********Login form********-->
<form id="loginForm" class="form-signin" role="form" style="width:50%; text-align: center; margin-top: 55%; margin-left: 25%;" method="post">
<input id="userMail" name="userMail" class="form-control" type="email" autofocus="" required="" placeholder="Email"></input>
<input id="userPassword" name="userPassword" class="form-control" type="password" required="" placeholder="Password"></input>
<div>
<input type="checkbox" name="checkbox-0" id="checkbox-mini-0" class="custom" data-mini="true" />
<label for="checkbox-mini-0">Remember me</label>
</div>
<button type="submit" data-mini="true" onClick="login()">Login</button>
</form>
<!--*********End form*********-->
<!--THIS LINE IS JUST FOR TEST-->
schedule
<!--*********Footer*********-->
<div id="footer">
<script>
function openExternal(elem) {
window.open(elem.href, "_system");
return false; // Prevent execution of the default onClick handler
}
</script>
<a class="col-xs-4" href="http://9292.nl/#" target="_blank" onClick="javascript:return openExternal(this)"><img id="footer-icon" src="images/9292_icon.jpg" style="width: 60px; height: 60px;"/></a>
<a class="col-xs-4" ><img id="footer-icon" src="images/maps_icon.jpg" style="width: 60px; height: 60px;"/></a>
<a class="col-xs-4" ><img id="footer-icon" src="images/call_icon2.jpg" style="width: 60px; height: 60px;"/></a>
</div>
<!--********End footer*********-->
</body>
You are doing things a little difficult for yourself the way everything is laid out. Here are a few examples that will help you fix your code.
Notice how the data:{} is laid out. Notice how the success is laid out.
$.ajax({
type: "GET",
url: "../ajax.php",
dataType: "json",
data: {
type: "getLineComments",
saleId: $(this).attr('saleId'),
lineId: $(this).attr('lineId')
},
success: function (json) {
$content.empty();
$content.slideToggle(500, function () {
if(json.Row !== undefined && json.Row.length > 0)
{
for(var i=0;i < json.Row.length;i++)
{
var item = json.Row[i];
//console.log(item);
//console.log(item.REMARK_LIN);
$content.append("<li>" + item.REMARK_LIN + "</li>");
}
}
else
{
if(json.Row !== undefined)
$content.append("<li>" + json.Row.REMARK_LIN + "</li>");
else
$content.append("<li>No Comments</li>");
}
$header.text(function () {
//change text based on condition
return $content.is(":visible") ? "Collapse Line Remarks" : "Expand Line Remarks";
});
});
},
error: function(e)
{
console.log(e);
}
});
Also one thing that will help you quite a bit is using the php function json_encode - http://php.net/manual/en/function.json-encode.php
For example
echo json_encode(mysqli_fetch_array($result));
This question already has answers here:
jQuery and CORS
(3 answers)
Closed 8 years ago.
hey guys i am unable to fetch cross domain json data here is my code below which doesn't work
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.getJSON demo</title>
<style>
img {
height: 100px;
float: left;
}
</style>
<script src="js/jquery.min.js"></script>
</head>
<body>
<div id="images"></div>
<script>
(function() {
var furl= "http://192.168.2.36/gemadmin/display.php?callback=?";
$.getJSON( furl)
.done(function( data ) {
console.log(data);
});
})();
</script>
</body>
</html>
and this code works properly since its just localhost
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.getJSON demo</title>
<style>
img {
height: 100px;
float: left;
}
</style>
<script src="js/jquery.min.js"></script>
</head>
<body>
<div id="images"></div>
<script>
(function() {
var furl= "http://localhost/gemadmin/display.php";
$.getJSON( furl)
.done(function( data ) {
console.log(data);
});
})();
</script>
</body>
</html>
why is the first version not working ? and what is the solution to make it work?
server code(display.php)
<?php
include 'config.php';
$sql = "select * from menu;";
$result= $mysqli->query($sql);
$data = $result->fetch_all( MYSQLI_ASSOC );
header('Content-Type: application/json');
echo json_encode( $data );
?>
The answer:
Found the answer instead of $.getJSON() use $.get and do a json parse
example
jQuery.getJSON demo
img {
height: 100px;
float: left;
}
<div id="images"></div>
<script>
(function() {
var furl= "http://localhost/gemadmin/display.php?callback=?";
$.get( furl)
.done(function( data ) {
var obj = JSON.parse(data);
console.log(obj);
});
})();
</script>
</body>
</html>
don't forget to add 'callback=?'to your url
Use Like This
$.ajax({
url: "URL",
type: "POST",
contentType: "application/json;charset=utf-8",
data: JSON.stringify("YOUR JSON"),
dataType: "json",
success: function (response) {
alert(response);
},
error: function (x, e) {
alert('Failed');
alert(x.responseText);
alert(x.status);
}
});
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.getJSON("demo_ajax_json.js",function(result){
$.each(result, function(i, field){
$("div").append(field + " ");
});
});
});
});
</script>
</head>
<body>
<button>Get JSON data</button>
<div></div>
</body>
</html>
demo_ajax_json.js
{
"firstName": "John",
"lastName": "Doe",
"age": 25
}
A simple example