AJAX Replace Span String with External Date String - javascript

Good morning,
I have Livestamp.js on my website and an external .php file that parses a date string from the VATSIM data file.
The code for the PHP file is:
<?php
require_once './vendor/autoload.php';
$logFile = './vendor/skymeyer/vatsimphp/app/logsusers.log';
$vatsim = new \Vatsimphp\VatsimData();
$vatsim->setConfig('cacheOnly', true);
$vatsim->setConfig('logFile', $logFile);
if ($vatsim->loadData()) {
$info = $vatsim->getGeneralInfo()->toArray();
echo "{$info['update']}";
} else {
echo "Cannot load data";
}
?>
This is my code for pulling the date string from the external file into my website:
<script type="text/javascript">
function get_update() {
$.ajax({
type: 'POST',
url: 'update.php',
data: {
request: 'true'
},
success: function(reply) {
$('.data-livestamp').html("" + reply + "");
}
});
}
$(document).ready(function() {
get_update();
});
</script>
The Livestamp.js code for the HTML has to look something like this: <span data-livestamp="1454063536"></span>
How can I change the AJAX code to either replace the example date string with the data string from my PHP file or to insert the contents of the PHP file in between the quotation marks in the <span data-livestamp="1454063536"></span> line?
Thanks very much in advance.

simply you can do this !
success: function(reply) {
$("[data-livestamp]").data('livestamp', reply)
}

Try this:
success: function(reply) {
$("[data-livestamp]").attr('data-livestamp', reply)
}

I worked the answer out:
<script type="text/javascript">
function get_update() {
$.ajax({
type: 'POST',
url: 'update.php',
data: {
request: 'true'
},
success: function() {
$('#update').livestamp(new Date());
}
});
}
$(document).ready(function() {
get_update();
});
</script>

Related

Laravel 8 and ajax

Never used ajax before and I'm quite new to Laravel as well.
I'm running a function where it gets the information from the form and i have no idea how to do the request with the information that I need.
function myFunction() {
var x = document.getElementById("frm1");
//console.log(x.elements.personSearch.value);
var details = x.elements.personSearch.value;
document.getElementById("personValidation").innerHTML = "";
if (
!details.match(
/\b[a-zA-Z ]*\\[a-zA-Z ]*\\[0-3][0-9][0-1][0-9][0-9]{4}\b/g
)
) {
document.getElementById("personValidation").innerHTML =
"Your search does not match the required format";
return;
}
$.ajax({
url: "/api/pnc/person",
type: "POST",
dataType: "json",
success:function(amount) {
console.log(amount);
}
});
``` Javascript
public function person(Request $request)
{
$request->validate([
'search' => 'required'
]);
return "test";
}
You need to Follow 3 steps for Ajax Request-
1. Setup Ajax Header bottom of your code. I mean before </html> add below code.
<script>
$.ajaxSetup({
headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
</script>
2. If you don't use Header Then you can use.
<script>
$(document).ready(function () {
$("#your_form_id").submit(function(e) {
var details = $('#details_input_id').val();
if(your_condition){
$.ajax({
url: "api/url",
type: "post",
data: {"_token": "{{csrf_token()}}", 'details':details},
success:function(data){
//Your response
if(data){
consol.log("Print what you want to print");
}
}
});
}else{
alert("Your search does not match the required format")
}
});
});
</script>
3. Your Route will be like ajax url
Route::post('api/url', [YourController::class, 'functionName'])->name('functionName');

How to assign javascript value to php variable without using cookie

I am recieving data form controller in ajax. Then I should assign it to php variable but without using cookies. How to do it?
My code:
$.ajax({
url: '/controller/action',
type: 'post',
data: {
id: id,
},
success: function(data){
if (data) {
//here I should assign data to php variable
}
if (!data) {
console.log('no data');
}
}
});
try using this demo code
<script>
if (data) {
var a =data;
<?php $abc = "<script>document.write(a)</script>"?>
}
if (!data) {
console.log('no data');
}
</script>
<?php
echo $abc;
?>
you can create a link with JavaScript that includes a GET variable. ones clicked php gets involved
code:
http://example.org/index.php?q1=blablabla
Php code:
if(isset($_GET['q1']) {$ff = $_GET['q1']}

Ajax POST in Javascript is not working

I searched my problem but I cannot find any solution.
I guess it's really simple and I missing something really easy...but I'm stucked.
I have an html page with a php generated list of links, like that:
<?php
$val = "'"my file with spaces.txt"'";
echo 'click me';
?>
Where $val is different each time, depending on a SQL query.
And the file name is showed correctly, since I hover the mouse on the link and I can read on the bottom-left of the browser:
javascript:post_filename('my file with spaces.txt');
Clicking this link will call this javascript function:
<script type="text/javascript">
function post_filename(val){
$.ajax
({
url: 'myurl.php',
data: { filename: val },
type: 'POST'
});
}
</script>
But clicking on it, does nothing! No errors, just nothing happens.
With the dev tools (F12 on Chrome) I see that it's all ok... just the mypage.php doesn't show... is a page with graphics, part of the site!
I'm stucked... please help me!
Thanks so much
Your AJAX call was invalid, data should be either a String, Array or Object. Object is probably what you're after here from the start you've made
<script type="text/javascript">
function post_filename(val){
$.ajax
({
url: 'myurl.php',
data: {
filename: val
},
type: 'POST';
});
}
</script>
Try making data an object:
<script type="text/javascript">
function post_filename(val){
$.ajax
({
url: 'myurl.php',
data: { filename: val },
type: 'POST';
});
}
</script>
I tried your code, but I made some some revisions for it to work ( I just notice you had a semicolon after type, I believe that should be a comma, that the reason why you have unexpected token) my codes works using this:
<?php
$val = "'hello.txt'";
echo 'click me';
?>
on javascript
$('.click').click(function() {
$.ajax
({
url: 'myurl.php',
data: { filename: $(this).attr('file') },
type: 'POST'
})
.done(function (data) {
//check the output from your url
console.log(data);
})
.fail(function (jqXHR, textStatus) {
console.log("Request failed: " + textStatus);
});
});

ajax with json how to

I try AJAX and JSON.
I have got this very simple scripts. Could you help me to get it work?
html file
<div class="esemeny">
<h2>Event</h2>
<p></p>
<button>click</button>
</div>
json file, I call it eventresult.json
{name: whatever,
}
and the javascript file
$(function(){
$('button').on('click', function(){
$.ajax('/javascript/eventresult.json', {
dataType: 'json',
success: function(result){
var esemeny = $('.esemeny');
esemeny.find('p').html(result.name);
}
});
});
});
Thank you
You didn't tell what did want to do exactly, anyway I think, this what you want to do:
JSON:
{
name: "whatever"
}
JS:
$(function(){
$('button').on('click', function(){
$.ajax('/javascript/eventresult.json', {
dataType: 'json',
type: 'GET' // you want to get content
success: function(result){
var esemeny = $('.esemeny');
esemeny.find('p').html(result.name);
}
});
});
});
Hope that helps a bit
I guess your JSON should look like
{
name: "whatever"
}
Mind the double quotes and the unnecessary comma.
Well,you actually didn't descripte the question clearly.You can have a debug that can be seen from your firebug console,like this:
$.ajax('/javascript/eventresult.json', {
dataType: 'json',
success: function(result){
var esemeny = $('.esemeny');
esemeny.find('p').html(result.name);
}
}).fail(function(jqXHR, textStatus, errorThrown){console.log(textStatus+':'+ errorThrown)})
Here are some suggestions that should make it work:
Make sure you include the necessary script files in your html file:
<head>
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
</head>
<div class="esemeny">
<h2>Event</h2>
<p></p>
<button>click</button>
</div>
<script src="javascript.js"></script>
Make sure you use valid json in the json file
{
"name": "whatever"
}
Use .click() function in your javascript:
$(function() {
$('button').click(function() {
$.ajax('/trials/eventresult.json', {
dataType: 'json',
success: function(result) {
var esemeny = $('.esemeny');
esemeny.find('p').html(result.name);
}
});
});
});

Jquery ajax call a php script with require once

I have an ajax request like :
$.ajax({
type: "GET",
url: "services/Test.class.php",
data: "call=getTest",
success: function (data) {
alert(data);
},
error: function (response) {
alert("Error getting php file");
}
});
So, in my class ( Test.class.php ), I have a getTest() function and a require_once('OtherPHP'),
When I test this code, I have an error in require once :
No such file or directory
in my alert(data)
how can I fix it?
It seems like you've included a wrong path of OtherPHP file in Test.class.php. Use file_exists() function to make sure that given path really exists before including/requiring in Test.class.php
if (file_exists(`OtherPHP.php`)) {
require_once(`OtherPHP.php`)
} else {
echo "The file `OtherPHP.php` does not exist";
}
You cant able to call the class directly from ajax,
create new php file say test.php
in test.php
include("Test.class.php");
$test = new Test();
$test ->getTest(); //print the getTest outpout here
Javascript:
$.ajax({
type: "GET",
url: "test.php",
.....

Categories

Resources